Hibernate 4(Jboss as 7)无法自动检测实体并插入PersistenceUnit,导致`unknown entity`异常

我遇到了这个How to auto-register entities with JPA/Hibernate: Unknown entity的类似问题.

我使用jboss作为7,hibernate 4(随jboss一起出现为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 Database access年,Matt告诉我在我的persistence.xml文件中添加元素,问题就解决了.但问题是,在另一个项目中,我使用类似的配置(相同的persistence.xml和类似的bean定义),一切顺利,没有引发未知的实体异常.正如我记得的那样,< class>在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]

解决方法:

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

如果你的persistence.xml在别处,你需要添加< class>手动元素,或指定< jar-file>包含实体.

上一篇:java – 改进Jboss 4.2非常慢的启动


下一篇:Java apache FTPServer和common.net FTPClient