title: How to auto-register entities with JPA/Hibernate: Unknown entity
body: <p>I'm stumped with a Hibernate/JPA configuration issue that's preventing my JPA-annotated entities from being automatically registered:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Unknown entity: com.example.crm.server.model.Language
      at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:671)
      at com.example.crm.server.model.Language.persist(Language.java:64)
      at com.example.crm.server.LanguageTest.testPersistAndRemove(LanguageTest.java:32)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  </code></pre>
  
  <p>In my entity class I have:</p>
  
  <pre><code>@Entity
  @Table(name="Languages")
  public class Language implements Serializable
  {
      @Id
      private Long id;
      private String name;
      // etc...
  }
  </code></pre>
  
  <p>And in MySQL, the Languages table looks like:</p>
  
  <pre><code>+-------------+----------+------+-----+---------+-------+
  | Field       | Type     | Null | Key | Default | Extra |
  +-------------+----------+------+-----+---------+-------+
  | Language_ID | int(11)  | NO   | PRI | NULL    |       | 
  | Name        | char(18) | YES  |     | NULL    |       | 
  +-------------+----------+------+-----+---------+-------+
  2 rows in set (0.00 sec)
  </code></pre>
  
  <p>And my persistence.xml looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
               version="1.0"&gt;
  
      &lt;persistence-unit name="crm"&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
  
          &lt;properties&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/&gt;
              &lt;property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/&gt;
              &lt;property name="hibernate.connection.url" value="jdbc:mysql://localhost/crm"/&gt;
              &lt;property name="hibernate.connection.username" value="crmuser"/&gt;
              &lt;property name="hibernate.connection.password" value="mypass"/&gt;
              &lt;property name="hibernate.c3p0.min_size" value="5"/&gt;
              &lt;property name="hibernate.c3p0.max_size" value="20"/&gt;
              &lt;property name="hibernate.c3p0.idleTestPeriod" value="30"/&gt;
              &lt;property name="hibernate.c3p0.timeout" value="0"/&gt;
              &lt;property name="hibernate.c3p0.max_statements" value="0"/&gt;
              &lt;property name="hibernate.format_sql" value="true"/&gt;
              &lt;property name="hibernate.query.jpaql_strict_compliance" value="false"/&gt;
              &lt;property name="hibernate.validator.apply_to_ddl" value="false"/&gt;
              &lt;property name="hibernate.validator.autoregister_listeners" value="false"/&gt;
              &lt;property name="hibernate.archive.autodetection" value="class, hbm"/&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="create"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>EDIT</strong>: here's how I'm getting my EntityManager and persisting:</p>
  
  <blockquote>
  <pre><code>public void persist()
  {
      EntityManager em = entityManager();
      try
      {
          em.getTransaction().begin();
          em.persist(this);
          em.getTransaction().commit();
      }
      finally
      {
          em.close();
      }
  }
  
  public static EntityManager entityManager()
  {
      return EMF.get().createEntityManager();
  }
  </code></pre>
  </blockquote>