我正在使用hibernate 4.1.5.Final和Spring 3.1.2 Release和Jboss 7.1。我已经在具有@NamedQuery批注的类中编写了所有命名查询,但是实体管理器未创建命名查询。我发布了stacktrace和context.xml
09:58:49,695 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) java.lang.IllegalArgumentException: Named query not found: validateLoginHash
09:58:49,770 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at org.hibernate.ejb.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:642)
09:58:49,772 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)
09:58:49,774 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
09:58:49,777 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at java.lang.reflect.Method.invoke(Method.java:597)
09:58:49,779 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
09:58:49,782 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at $Proxy30.createNamedQuery(Unknown Source)
09:58:49,784 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)
09:58:49,785 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
09:58:49,788 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at java.lang.reflect.Method.invoke(Method.java:597)
09:58:49,790 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
09:58:49,793 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at $Proxy30.createNamedQuery(Unknown Source)
ApplicationContext.xml
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect" ref="jpaDialect"/>
<property name="packagesToScan" value="com.project.entities"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
<!-- <property name="persistenceUnitName" value="Project" /> -->
<property name="persistenceXmlLocation" value="classpath:META-INF/jpa-persistence.xml"/>
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jboss/datasources/MySqlDS"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
jpa-persistence.xml
<persistence>
<persistence-unit name="Project" transaction-type="RESOURCE_LOCAL" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:jboss/datasources/MySqlDS</non-jta-data-source>
<!-- <properties> -->
<!-- <property name="jboss.as.jpa.providerModule" value="hibernate3-bundled" /> -->
<!-- </properties> -->
</persistence-unit>
</persistence>
DBNamedQuery.java
@Entity
@NamedQueries( {
@NamedQuery(name = ... , query = ... ),
@NamedQuery(name = ..., query = ...),
.....More named queries
})
public class DBNamedQuery {
}
最佳答案
I have written all named queries in a class with @NamedQuery annotation
您没有在上面的陈述中明确提到您要引用的类的类型吗?您将需要在Entity类(用@Entity注释注释的类)中编写命名查询。
更新:
我现在对您的类
DBNamedQuery
感到困惑。您说您正在使用一个类来放置所有命名查询。我的理解是您正在使用此类为应用程序的所有实体编写命名查询。如果那是正确的,因为它不是真正的jpa实体,如何在类@Entity
上使用DBNamedQuery
注释?包含
@NamedQuery
批注的类应为托管实体。而且我怀疑您的类DBNamedQuery
不是。为了确定问题,我建议在日志中检查这是否是受管理的实体。如果您不能做到这一点,那么
EntityManger
将为您和API提供在运行时检查contains(java.lang.Object entity)
的方法。与此相关的是,如果您使用注释,则对于JPA,命名查询是jpa实体的一部分。使用xml可让您灵活地存储在单独的文件中。