我正在使用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中
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
JPA-的persistence.xml
org.hibernate.ejb.HibernatePersistence
java:jboss/datasources/MySqlDS
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感到困惑.您说您正在使用一个类来放置所有命名查询.我的理解是您正在使用此类为应用程序的所有实体编写命名查询.如果这是正确的,你如何在类DBNamedQuery上使用@Entity注释,因为它实际上不是一个jpa实体?
包含@NamedQuery注释的类应该是一个托管实体.我怀疑你的类DBNamedQuery不是.
要确定问题,我建议检查日志,如果这是一个manged实体.如果你不能这样做,那么EntityManger会给你和API来检查运行时是否包含(java.lang.Object实体).
在相关的说明中,如果您使用的是注释,那么JPA Named Queries是jpa实体的一部分.使用xml可以灵活地存储在单独的文件中.
标签:java,spring,jpa,hibernate,persistence
来源: https://codeday.me/bug/20190725/1536142.html