Hibernate 4 (Jboss as 7) cannot automatically detect an object and insert it into PersistenceUnit, throw an exception to an unknown entity

I encounter a similar problem with this How to automatically register objects with JPA / Hibernate: unknown object .

I use jboss as 7, hibernate 4 (which come with jboss as 7), spring 3.0.5. I annotate an entity class using @Entity . And using org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean for gen entityManager. and below is the definition of 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> 

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 the database, for example:

 entityManager.persist(myEntity); 

It throws an exception, saying that "MyEntity" is an unknown object.

In Spring 3..0.5 + hierbnate 3.6.6.final + jboss as 7 Accessing the database , Matt told me to add an element to my persistence.xml file, and the problem is resolved. But the problem is that in another project I use a similar configuration (the same definition of persistence.xml and a similar definition of bean), and everything goes well, the exception throws an unknown entity . And, as I recall, <class> not required in the persistence.xml file, since jboss / hibernate scans the class with @Entity annotation and adds it to the PU.

I turn on the TRACE level log, and in two jboss projects like 7 it seems like creating two persistenceUnit that 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] 
+4
source share
2 answers

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

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

+1
source

I have the same problem with you. Could you try using this property in your org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean

 <property name="packagesToScan" value="mypackage" /> 
+1
source

All Articles