如何使用JPA / Hibernate自动注册实体:未知实体 [英] How to auto-register entities with JPA/Hibernate: Unknown entity

查看:112
本文介绍了如何使用JPA / Hibernate自动注册实体:未知实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  

我很难忍受一个Hibernate / JPA配置问题,它阻止了我的JPA注释实体被自动注册: java.lang.IllegalArgumentException:未知实体: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)


$ b

在我的实体类中,我有:

  @Table(name =Languages)
public class语言实现Serializable
{
@Id
private L ong id;
私人字符串名称;
// etc ...
}

而在MySQL中,语言表看起来像:

  + ------------- + ------- --- + ------ + ----- + --------- + ------- + 
|字段|类型|空| Key |默认|额外|
+ ------------- + ---------- + ------ + ----- + ------- - + ------- +
| Language_ID | int(11)| NO | PRI | NULL | |
|名称| char(18)|是| | NULL | |
+ ------------- + ---------- + ------ + ----- + ------- - + ------- +
2行(0.00秒)



<

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

<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< property name =hibernate.connection.driver_classvalue =com.mysql.jdbc.Driver/>
< property name =hibernate.connection.urlvalue =jdbc:mysql:// localhost / crm/>
< property name =hibernate.connection.usernamevalue =crmuser/>
< property name =hibernate.connection.passwordvalue =mypass/>
< property name =hibernate.c3p0.min_sizevalue =5/>
< property name =hibernate.c3p0.max_sizevalue =20/>
< property name =hibernate.c3p0.idleTestPeriodvalue =30/>
< property name =hibernate.c3p0.timeoutvalue =0/>
< property name =hibernate.c3p0.max_statementsvalue =0/>
< property name =hibernate.format_sqlvalue =true/>
< property name =hibernate.query.jpaql_strict_compliancevalue =false/>
< property name =hibernate.validator.apply_to_ddlvalue =false/>
< property name =hibernate.validator.autoregister_listenersvalue =false/>
< property name =hibernate.archive.autodetectionvalue =class,hbm/>
< property name =hibernate.hbm2ddl.autovalue =create/>
< / properties>
< / persistence-unit>

< /持久性>

编辑:以下是我得到我的EntityManager并坚持:


  public void persist()
{
EntityManager em = entityManager() ;
尝试
{
em.getTransaction()。begin();
em.persist(this);
em.getTransaction()。commit();
}
finally
{
em.close();



public static EntityManager entityManager()
{
return EMF.get()。createEntityManager();
}



解决方案

<结果很简单:直接在persistence.xml文件中列出类。 armandino和MikelRascher都引导我回答这个问题,尽管间接地给了他们这个答案。



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

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

<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< property name =hibernate.connection.driver_classvalue =com.mysql.jdbc.Driver/>
< property name =hibernate.connection.urlvalue =jdbc:mysql:// localhost / crm/>
< property name =hibernate.connection.usernamevalue =myuser/>
< property name =hibernate.connection.passwordvalue =mypass/>
< property name =hibernate.c3p0.min_sizevalue =5/>
< property name =hibernate.c3p0.max_sizevalue =20/>
< property name =hibernate.c3p0.idleTestPeriodvalue =30/>
< property name =hibernate.c3p0.timeoutvalue =0/>
< property name =hibernate.c3p0.max_statementsvalue =0/>
<! - property name =hibernate.show_sqlvalue =true/> - >
< property name =hibernate.format_sqlvalue =true/>
< property name =hibernate.query.jpaql_strict_compliancevalue =false/>
< property name =hibernate.validator.apply_to_ddlvalue =false/>
< property name =hibernate.validator.autoregister_listenersvalue =false/>
< property name =hibernate.archive.autodetectionvalue =class,hbm/>
< property name =hibernate.hbm2ddl.autovalue =create/>
< / properties>
< / persistence-unit>

< /持久性>


I'm stumped with a Hibernate/JPA configuration issue that's preventing my JPA-annotated entities from being automatically registered:

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)

In my entity class I have:

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

And in MySQL, the Languages table looks like:

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

And my persistence.xml looks like:

<?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>

EDIT: here's how I'm getting my EntityManager and persisting:

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

解决方案

It turned out to be fairly simple: list the classes directly in the persistence.xml file. Both armandino and MikelRascher led me to this answer, though indirectly, so props to them.

Here is the persistence.xml I'm using now:

<?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>

这篇关于如何使用JPA / Hibernate自动注册实体:未知实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆