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]