4

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]
Cœur
  • 37,241
  • 25
  • 195
  • 267
yzandrew
  • 785
  • 1
  • 11
  • 25

2 Answers2

2

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.

digitaljoel
  • 26,265
  • 15
  • 89
  • 115
  • I package my project into a war file. and the persistence.xml is in WEB-INF/classes/META-INF/persistence.xml. Is it the place you mention ? – yzandrew Sep 07 '11 at 20:55
  • hrm, that seems like it should work then. Are you sure you have spring configured to process the annotations? I'm away from my project so I can't reference it right now to get the exact setting. – digitaljoel Sep 07 '11 at 21:02
  • In my applicationContext.xml file, I have `` and ``. Is it enough for spring to process the annotation? – yzandrew Sep 07 '11 at 21:04
  • sorry, I meant hibernate, not spring. With them being in the same root it seems like you should be ok. You could set the jar-file to point to your classes directory as mentioned in http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/configuration.html – digitaljoel Sep 07 '11 at 21:15
  • hmm... thank you for you advice and I will give it a try. I think it should solve the problem and won't throw `unknown entity` exception. But in my other project, I use the same persistence.xml and same jboss as 7 server. Can't find the reason why one of my project can't auto detect the entity class in my PU. – yzandrew Sep 07 '11 at 21:29
  • Looks like your persistence unit base is /D:/Server/jboss-as-7.0.0.Final/bin/content/admin.war/WEB-INF/classes/ is that where your Entity annotated classes are located? – digitaljoel Sep 07 '11 at 21:36
  • :D it seems like to be jboss as 7's vfs. The location I deploy my war file is `D:\Server\jboss-as-7.0.0.Final\standalone\deployments`. The entity class with `@Entity` annotated is on `D:\Server\jboss-as-7.0.0.Final\standalone\deployments\admin.war\WEB-INF\classes\com\mycompany\myproject\jpa\entity`. The persistence.xml is on `D:\Server\jboss-as-7.0.0.Final\standalone\deployments\admin.war\WEB-INF\classes\META-INF` – yzandrew Sep 07 '11 at 21:43
  • Looks like we may have found the culprit then. You could look at the app that IS working and see if the persistence unit root is correct on that one, then figure out the difference. – digitaljoel Sep 07 '11 at 21:56
  • hmm... unluckly, the app that is working has the similar persistence unit root, which is `PU root URL: vfs:/D:/Server/jboss-as-7.0.0.Final/bin/content/AppIsWorking.war/WEB-INF/classes/`, looks like the same as the one that ISN'T working: `PU root URL: vfs:/D:/Server/jboss-as-7.0.0.Final/bin/content/admin.war/WEB-INF/classes/` – yzandrew Sep 07 '11 at 22:01
1

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" />    
Makoton
  • 443
  • 3
  • 14