gpt4 book ai didi

java - 如何使用 JPA/Hibernate : Unknown entity 自动注册实体

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:51:44 24 4
gpt4 key购买 nike

我遇到了一个 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 中,语言表如下所示:

+-------------+----------+------+-----+---------+-------+
| 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 看起来像:

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

最佳答案

事实证明这很简单:直接在 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>

关于java - 如何使用 JPA/Hibernate : Unknown entity 自动注册实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4939732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com