Hibernate 4(Jboss as 7)无法自动检测实体并插入到 PersistenceUnit 中,导致“未知实体”异常

发布于 2024-12-03 08:07:54 字数 3851 浏览 4 评论 0原文

我遇到了类似的问题 How to auto-register具有 JPA/Hibernate 的实体:未知实体

我使用 jboss as 7、hibernate 4(与 jboss as 7 一起提供)、spring 3.0.5 。 我用 @Entity 注释我的实体类。 并使用 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean 来生成entityManager。下面是 bean 定义:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:jboss/datasources/appsubmission" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="app_sub_jpa" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
            </bean>
        </property>
</bean>

下面是 persistence.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0"
    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_2_0.xsd">

    <persistence-unit name="app_sub_jpa">
        <description>Hibernate for JPA</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:jboss/datasources/myds</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        </properties>
    </persistence-unit>
</persistence>

一切都很顺利,直到我尝试访问数据库,例如:

entityManager.persist(myEntity);

它抛出一个异常,说“MyEntity”是未知实体。

在 Spring 3 中。 .0.5 + hierbnate 3.6.6.final + jboss as 7 数据库访问 ,Matt 告诉我在 persistence.xml 文件中添加元素,问题就解决了。 但问题是,在另一个项目中,我使用类似的配置(相同的 persistence.xml 和类似的 bean 定义),一切都很顺利,没有抛出 未知实体 异常。我记得,persistence.xml 文件中不需要 ,因为 jboss/hibernate 将使用 @Entity 注释扫描类并将其添加到 PU 中。

我启用了TRACE级别的日志,在两个项目中,jboss as 7似乎创建了两个persistenceUnit,它们是相同的。

PersistenceUnitMetadata(version=2.0) [
    name: app_sub_jpa
    jtaDataSource: null
    nonJtaDataSource: java:jboss/datasources/myds
    transactionType: JTA
    provider: org.hibernate.ejb.HibernatePersistence
    classes[
    ]
    packages[
    ]
    mappingFiles[
    ]
    jarFiles[
    ]
    validation-mode: AUTO
    shared-cache-mode: UNSPECIFIED
    properties[
        hibernate.dialect: org.hibernate.dialect.MySQLDialect
    ]]

11:23:45,127 DEBUG [org.hibernate.ejb.Ejb3Configuration] (MSC service thread 1-3) Processing PersistenceUnitInfo [
    name: app_sub_jpa
    persistence provider classname: org.hibernate.ejb.HibernatePersistence
    classloader: ModuleClassLoader for Module "deployment.admin.war:main" from Service Module Loader
    Temporary classloader: org.jboss.as.jpa.classloader.TempClassLoader@28e13c84
    excludeUnlistedClasses: false
    JTA datasource: null
    Non JTA datasource: org.jboss.jca.adapters.jdbc.WrapperDataSource@5b4c1313
    Transaction type: JTA
    PU root URL: vfs:/D:/Server/jboss-as-7.0.0.Final/bin/content/admin.war/WEB-INF/classes/
    Shared Cache Mode: UNSPECIFIED
    Validation Mode: AUTO
    Jar files URLs []
    Managed classes names []
    Mapping files names []
    Properties [
        hibernate.dialect: org.hibernate.dialect.MySQLDialect]

I meet a similar problem with this How to auto-register entities with JPA/Hibernate: Unknown entity .

I am using jboss as 7, hibernate 4( which come along with jboss as 7), spring 3.0.5 .
I annotate my entity class with @Entity.
And using org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean to gen the entityManager. and below is the bean definition:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:jboss/datasources/appsubmission" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="app_sub_jpa" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
            </bean>
        </property>
</bean>

and below is the persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0"
    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_2_0.xsd">

    <persistence-unit name="app_sub_jpa">
        <description>Hibernate for JPA</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:jboss/datasources/myds</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        </properties>
    </persistence-unit>
</persistence>

And everything goes well until I try to access DB, like:

entityManager.persist(myEntity);

It throws an exception, saying that 'MyEntity' is unknown entity.

In Spring 3..0.5 + hierbnate 3.6.6.final + jboss as 7 Database access , Matt told me to add element in my persistence.xml file, and the problem is solved. But the problem is, in another project, I use the similar config(same persistence.xml and similar bean definition), and everything goes well, no unknown entity exception is thrown. And as I remeber, the <class> is not necessary in persistence.xml file, as jboss/hibernate will scan class with @Entity annotation and add it into PU.

I enable the TRACE level log, and in the two projects, jboss as 7 seems like create two persistenceUnit, which are the same.

PersistenceUnitMetadata(version=2.0) [
    name: app_sub_jpa
    jtaDataSource: null
    nonJtaDataSource: java:jboss/datasources/myds
    transactionType: JTA
    provider: org.hibernate.ejb.HibernatePersistence
    classes[
    ]
    packages[
    ]
    mappingFiles[
    ]
    jarFiles[
    ]
    validation-mode: AUTO
    shared-cache-mode: UNSPECIFIED
    properties[
        hibernate.dialect: org.hibernate.dialect.MySQLDialect
    ]]

11:23:45,127 DEBUG [org.hibernate.ejb.Ejb3Configuration] (MSC service thread 1-3) Processing PersistenceUnitInfo [
    name: app_sub_jpa
    persistence provider classname: org.hibernate.ejb.HibernatePersistence
    classloader: ModuleClassLoader for Module "deployment.admin.war:main" from Service Module Loader
    Temporary classloader: org.jboss.as.jpa.classloader.TempClassLoader@28e13c84
    excludeUnlistedClasses: false
    JTA datasource: null
    Non JTA datasource: org.jboss.jca.adapters.jdbc.WrapperDataSource@5b4c1313
    Transaction type: JTA
    PU root URL: vfs:/D:/Server/jboss-as-7.0.0.Final/bin/content/admin.war/WEB-INF/classes/
    Shared Cache Mode: UNSPECIFIED
    Validation Mode: AUTO
    Jar files URLs []
    Managed classes names []
    Mapping files names []
    Properties [
        hibernate.dialect: org.hibernate.dialect.MySQLDialect]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲念泪 2024-12-10 08:07:54

Hibernate 在 {persistenceUnit.root}/META-INF/persistence.xml 中找到 persistence.xml,因此它应该扫描 {persistenceUnit.root} 来查找带注释的类。

如果您的 persistence.xml 位于其他位置,则需要手动添加 元素,或指定包含实体的

Hibernate finds the persistence.xml in {persistenceUnit.root}/META-INF/persistence.xml, so it should scan {persistenceUnit.root} for the annotated classes.

If your persistence.xml is elsewhere you will need to add the <class> elements manually, or specify a <jar-file> that contains the Entities.

绾颜 2024-12-10 08:07:54

我有同样的问题,你可以尝试在你的 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean 中使用这个属性吗

<property name="packagesToScan" value="mypackage" />    

I have the same problem with it, Could you tried use this propertie in your org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean

<property name="packagesToScan" value="mypackage" />    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文