@PersistenceContext
实体管理器找不到在容器启动期间似乎已处理的/任何实体类。
在持久性单元设置期间,容器似乎找到并注册了实体:
2012-05-17 15:28:21,978 INFO [org.hibernate.cfg.annotations.Version] (main) Hibernate Annotations 3.4.0.GA
2012-05-17 15:28:21,997 INFO [org.hibernate.annotations.common.Version] (main) Hibernate Commons Annotations 3.1.0.GA
2012-05-17 15:28:22,004 INFO [org.hibernate.ejb.Version] (main) Hibernate EntityManager 3.4.0.GA
2012-05-17 15:28:22,039 INFO [org.hibernate.ejb.Ejb3Configuration] (main) Processing
PersistenceUnitInfo [
name: dashboardPu
...]
2012-05-17 15:28:22,069 WARN [org.hibernate.ejb.Ejb3Configuration] (main) Persistence provider caller does not implement the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null.
2012-05-17 15:28:22,146 INFO [org.hibernate.cfg.AnnotationBinder] (main) Binding entity from annotated class: scholastic.dashboard.dto.ReportRequest
2012-05-17 15:28:22,161 INFO [org.hibernate.cfg.annotations.QueryBinder] (main) Binding Named query: ReportRequest.getReportsByUser => from ReportRequest where userId = :userId
但是,稍后使用该实体的尝试失败。通过放入调试器并查看 entitymanager>factory>persistenceUnit,我已经验证了 entitymanager 确实在使用 dashboardPu(这个耳朵中有 3 个持久性单元)。
2012-05-17 15:28:54,730 DEBUG [org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter] (http-127.0.0.1-8080-2) Opening JPA EntityManager in OpenEntityManagerInViewFilter
2012-05-17 15:28:54,731 DEBUG [org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter] (http-127.0.0.1-8080-1) Using EntityManagerFactory 'dashboardEntityManagerFactory' for OpenEntityManagerInViewFilter
2012-05-17 15:28:54,731 DEBUG [org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter] (http-127.0.0.1-8080-1) Opening JPA EntityManager in OpenEntityManagerInViewFilter
2012-05-17 15:28:54,775 DEBUG [scholastic.dashboard.dao.ReportRequestDao] (http-127.0.0.1-8080-2) Get reports for secretuserid
2012-05-17 15:28:54,797 WARN [org.hibernate.hql.QuerySplitter] (http-127.0.0.1-8080-2) no persistent classes found for query class: from scholastic.dashboard.dto.ReportRequest where user_id = secretuserid
我首先让它无法使用 找到"ReportRequest.getReportsByUser"
查询em.createNamedQuery()
,然后尝试使用实体类的完整路径名进行from ReportRequest where userId = :userId
查询,然后在上面的最后一个代码块行中进行查询。
使用 JBoss 5.1、JPA 2、Hibernate 3.3 或 3.6、Spring 3.0.5。
代码片段:
@Entity
@Table(name="td_report_request")
@NamedQueries({
@NamedQuery(name=ReportRequestDao.GET_REPORTS_BY_USER,
query="from ReportRequest where userId = :userId"),
})
public class ReportRequest extends SlmsGuidAbstract {...}
...
@Repository
public class ReportRequestDao {
@PersistenceContext
private EntityManager em;
public List<ReportRequest> getReportRequests(String userId) {
// TODO uncomment this line and remove the one after when we go to JBoss >= 6
// TypedQuery<ReportRequest> query = em.createNamedQuery(GET_REPORTS_BY_USER, ReportRequest.class);
// Query query = em.createNamedQuery(GET_REPORTS_BY_USER);
Query query = em.createQuery("from scholastic.dashboard.dto.ReportRequest where user_id = " + userId);
...
<bean id="dashboardEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="lycea.ds.jndi-MySqlDS" />
<property name="persistenceUnitName" value="dashboardPu"/>
</bean>
...
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.0">
<persistence-unit name="dashboardPu" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/MySqlDS</jta-data-source>
<properties>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/dashboardPu"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.query.factory_class" value="org.hibernate.hql.ast.ASTQueryTranslatorFactory"/>
<property name="hibernate.connection.release_mode" value="auto"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>