Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate 4 (Jboss as 7) can't auto detect entity and insert into PersistenceUnit, cause `unknown entity` exception
    text
    copied!<p>I meet a similar problem with this <a href="https://stackoverflow.com/questions/4939732/how-to-auto-register-entities-with-jpa-hibernate-unknown-entity">How to auto-register entities with JPA/Hibernate: Unknown entity</a> . </p> <p>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 <code>@Entity</code>. And using <code>org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean</code> to gen the entityManager. and below is the bean definition:</p> <pre><code>&lt;bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt; &lt;property name="jndiName" value="java:jboss/datasources/appsubmission" /&gt; &lt;/bean&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;property name="persistenceUnitName" value="app_sub_jpa" /&gt; &lt;property name="jpaVendorAdapter"&gt; &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt; &lt;property name="showSql" value="false" /&gt; &lt;/bean&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>and below is the persistence.xml file:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;persistence-unit name="app_sub_jpa"&gt; &lt;description&gt;Hibernate for JPA&lt;/description&gt; &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt; &lt;non-jta-data-source&gt;java:jboss/datasources/myds&lt;/non-jta-data-source&gt; &lt;properties&gt; &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;/persistence&gt; </code></pre> <p>And everything goes well until I try to access DB, like:</p> <pre><code>entityManager.persist(myEntity); </code></pre> <p>It throws an exception, saying that 'MyEntity' is unknown entity.</p> <p>In <a href="https://stackoverflow.com/questions/6853112/spring-3-0-5-hierbnate-3-6-6-final-jboss-as-7-database-access">Spring 3..0.5 + hierbnate 3.6.6.final + jboss as 7 Database access</a> , Matt told me to add element in my persistence.xml file, and the problem is solved. <strong>But the problem is, in another project, I use the similar config(same persistence.xml and similar bean definition), and everything goes well, no <code>unknown entity</code> exception is thrown. And as I remeber, the <code>&lt;class&gt;</code> is not necessary in persistence.xml file, as jboss/hibernate will scan class with <code>@Entity</code> annotation and add it into PU.</strong></p> <p>I enable the TRACE level log, and in the two projects, jboss as 7 seems like create two persistenceUnit, which are the same.</p> <pre><code>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] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload