2011-02-08 42 views
10

我與同時防止我的JPA註解實體被自動註冊一個Hibernate/JPA配置問題難住了:如何使用JPA/Hibernate的自動註冊實體:未知實體

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中,語言表如下所示:

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

而且我persis tence.xml樣子:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 

    <persistence-unit name="crm"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 

     <properties> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> 
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> 
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost/crm"/> 
      <property name="hibernate.connection.username" value="crmuser"/> 
      <property name="hibernate.connection.password" value="mypass"/> 
      <property name="hibernate.c3p0.min_size" value="5"/> 
      <property name="hibernate.c3p0.max_size" value="20"/> 
      <property name="hibernate.c3p0.idleTestPeriod" value="30"/> 
      <property name="hibernate.c3p0.timeout" value="0"/> 
      <property name="hibernate.c3p0.max_statements" value="0"/> 
      <property name="hibernate.format_sql" value="true"/> 
      <property name="hibernate.query.jpaql_strict_compliance" value="false"/> 
      <property name="hibernate.validator.apply_to_ddl" value="false"/> 
      <property name="hibernate.validator.autoregister_listeners" value="false"/> 
      <property name="hibernate.archive.autodetection" value="class, hbm"/> 
      <property name="hibernate.hbm2ddl.auto" value="create"/> 
     </properties> 
    </persistence-unit> 

</persistence> 

編輯:這裏就是我如何得到我的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(); 
} 

回答

11

它結果相當簡單:列出cla直接在persistence.xml文件中。 armandino和MikelRascher都引導我回答這個問題,儘管間接地給了他們這個答案。

這是我現在使用persistence.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 

    <persistence-unit name="crm"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 

     <class>com.example.Language</class> 

     <properties> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> 
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> 
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost/crm"/> 
      <property name="hibernate.connection.username" value="myuser"/> 
      <property name="hibernate.connection.password" value="mypass"/> 
      <property name="hibernate.c3p0.min_size" value="5"/> 
      <property name="hibernate.c3p0.max_size" value="20"/> 
      <property name="hibernate.c3p0.idleTestPeriod" value="30"/> 
      <property name="hibernate.c3p0.timeout" value="0"/> 
      <property name="hibernate.c3p0.max_statements" value="0"/> 
      <!--property name="hibernate.show_sql" value="true"/>--> 
      <property name="hibernate.format_sql" value="true"/> 
      <property name="hibernate.query.jpaql_strict_compliance" value="false"/> 
      <property name="hibernate.validator.apply_to_ddl" value="false"/> 
      <property name="hibernate.validator.autoregister_listeners" value="false"/> 
      <property name="hibernate.archive.autodetection" value="class, hbm"/> 
      <property name="hibernate.hbm2ddl.auto" value="create"/> 
     </properties> 
    </persistence-unit> 

</persistence> 
1

你如何構建你的實體管理器?

你應該看看INFO級別log4j消息從休眠通過log4j.properties設置這樣的:

# 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如果你需要更多的信息。

ALSO 當您創建實體管理器時,您沒有提到持久性單元的名稱。也許這並不重要:

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

嗨邁克,我說我的代碼用於獲取的EntityManager我的問題上面。我根本沒有在日誌信息中看到我的班級名稱。謝謝。 – 2011-02-09 00:09:55

4

更新

這裏有一個更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應該是這個樣子:

<?xml version="1.0" encoding="UTF-8" ?> 
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" 
    version="1.0"> 
    <package>org.example</package> 
    <entity class="org.example.MyEntity"/> 
    <entity class="org.example.AnotherEntity"/> 
</entity-mappings> 
+2

嗯,這似乎是Hibernate特有的。當我嘗試這個時得到:引起:org.hibernate.HibernateException:找不到/hibernate.cfg.xml。有沒有更通用的JPA方式來做到這一點?謝謝! – 2011-02-09 02:17:07

+0

`Ejb3Configuration`已棄用,並在Hibernate 5+中刪除。 – Stephan 2016-10-18 15:22:46

相關問題