如何使用JPA / Hibernate自动注册实体:未知实体

我遇到了Hibernate / JPA配置问题,这会阻止我的JPA注释实体被自动注册:

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) 

在我的实体课中,我有:

 @Entity @Table(name="Languages") public class Language implements Serializable { @Id private Long id; private String name; // etc... } 

在MySQL中,Languages表看起来像:

 +-------------+----------+------+-----+---------+-------+ | 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) 

我的persistence.xml看起来像:

    org.hibernate.ejb.HibernatePersistence                     

编辑 :这是我如何获得我的EntityManager并坚持:

 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(); } 

事实certificate这很简单:直接在persistence.xml文件中列出类。 armandino和MikelRascher都引导我回答这个问题,虽然是间接的,但对他们来说是道具。

这是我现在使用的persistence.xml:

    org.hibernate.ejb.HibernatePersistence com.example.Language                      

更新

这是一个更像JPA的方法:

 Ejb3Configuration ejb3Configuration = new Ejb3Configuration(); ejb3Configuration.addResource("META-INF/orm.xml"); ejb3Configuration.configure("persistence.xml"); EntityManagerFactory factory = ejb3Configuration.buildEntityManagerFactory(); EntityManager em = factory.createEntityManager(); 

orm.xml看起来应该是这样的:

   org.example    

你是如何构建实体经理的?

您应该通过在log4j.properties中设置来查看来自hibernate的INFO级别log4j消息:

 # Hibernate logging options (INFO only shows startup messages) log4j.logger.org.hibernate=INFO # Log JDBC bind parameter runtime arguments log4j.logger.org.hibernate.type=INFO 

您应该在消息中看到您的课程:

 15:39:37,519 INFO Version:156 - Hibernate Commons Annotations 3.2.0.Final 15:39:37,527 INFO Environment:148 - Hibernate 3.6.0.Final 15:39:37,529 INFO Environment:148 - hibernate.properties not found 15:39:37,532 INFO Environment:148 - Bytecode provider name : javassist 15:39:37,535 INFO Environment:148 - using JDK 1.4 java.sql.Timestamp handling 15:39:37,588 INFO Version:156 - Hibernate EntityManager 3.6.0.Final 15:39:38,036 INFO AnnotationBinder:156 - Binding entity from annotated class: com.example.crm.server.model.Language 

如果您需要更多信息,请转到DEBUG。

另外 ,在创建实体管理器时,您没有提到持久性单元的名称。 也许那不重要:

 EntityManagerFactory emf = Persistence.createEntityManagerFactory("crm"); EntityManager em = emf.createEntityManager();