我为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>
问题内容: 能否请您告诉我,我如何才能很好地为Hibernate实体启用Spring自动装配? 假设我有一个实体,并希望在那里有邮件发送者: 有没有比做的更好的方法 在我的DAO中? 谢谢! 问题答案: 有可能的!(这是Spring Roo中的默认样式!) 您所需要做的就是将@Configurable批注添加到您的实体。在配置中并使用AspectJ编织激活注释。 Spring参考中有一章:7.8.
问题内容: 一个运行了几个月的应用程序已经停止使用已经有几个月历史的JPA 批注。在进行集成测试时,我看到许多“ ”类型错误。 我不清楚这里出了什么问题。 我没有文件,因为我正在使用hibernate实体管理器。由于我仅使用注释,因此我的实体没有.hbm.xml文件。我的文件很小,可以正常保存。 我显然丢失了一些东西,但不能动弹。 我正在使用hibernate批注3.2.1,hibernate-e
问题内容: 如果您正在使用xml映射,那么liquibase是hibernate的 hbm2ddl_auto 属性的完美替代方案。但是我使用的是JPA注释(hibernate注释)。那么可以使用liquibase吗? 问题答案: 是的,Liquibase使用了hibernate的元数据类,无论您使用xml映射还是注释,它们都是相同的。您确实需要一个hibernate配置文件来指向liquibase
问题内容: 当我使用@Entity注释类并尝试解决依赖关系时,我可以在两个不同的包javax.persistence.Entity和org.hibernate.annotations.Entity中选择包。 javax包是JPA的实体注释,但是为什么会有休眠的实体注释,它与JPA的注释有区别?仅仅是允许定义更多属性的扩展吗? 问题答案: 具有一些尚未标准化的额外属性。仅当直接使用hibernate
问题内容: 我有一堂非常简单的课: 我用这个主要的称呼它: 我不断收到此MappingException: 我的HibernateUtil是: 我看过这么多重复的作品,没有人有任何想法,但这些解决方案似乎对我不起作用。这是我的hibernate.cfg.xml: 问题答案: 您应该使用代替,然后调用 将您的班级添加到映射的班级列表中。
问题内容: 我有两个JPA实体:和。延伸。两者之间存在ManyToMany关系,并被描述为 “一个VirtualLot可以包含VirtualFiles” 和 “一个VirtualFile可以与多个VirtualLots关联” 。实体定义如下: 在这里,我要总结使用弹簧数据JPA库包含在一个给定的VirtualLot的VirtualFiles的大小:。该查询定义如下: 我的问题是Hibernate