java.lang.IllegalArgumentException:未找到命名查询(实体管理器未创建NamedQuery) [英] java.lang.IllegalArgumentException: Named query not found.(Entity Manager not creating NamedQuery)

查看:119
本文介绍了java.lang.IllegalArgumentException:未找到命名查询(实体管理器未创建NamedQuery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是hibernate 4.1.5.Final和Spring 3.1.2 Release和Jboss 7.1。我已经使用@NamedQuery注解在类中编写了所有命名查询,但实体管理器未创建命名查询。我发布stacktrace和context.xml

  09:58:49,695错误[stderr](http-localhost-127.0.0.1 -8080-2)java.lang.IllegalArgumentException:未找到命名查询:validateLoginHash 
09:58:49,770错误[stderr](http-localhost-127.0.0.1-8080-2)位于org.hibernate.ejb。 AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:642)
09:58:49,772错误[stderr](http-localhost-127.0.0.1-8080-2)at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)
09:58:49,774错误[stderr](http-localhost-127.0.0.1-8080-2)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
09:58:49,777错误[ 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- (扩展入口) ityManagerCreator.java:365)

09:58:49,782错误[stderr](http-localhost-127.0.0.1-8080-2)at $ Proxy30.createNamedQuery(Unknown Source)

09:58:49,784错误[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错误[stderr]( http-localhost-127.0.0.1-8080-2)at java.lang.reflect.Method.invoke(Method.java:597)

09:58:49,790错误[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 =jpaDialectclass =org.springframework.orm.jpa.vendor.HibernateJpaDialect /> 
< bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>


< property name =dataSourceref =dataSource/>
< property name =jpaDialectref =jpaDialect/>
< property name =packagesToScanvalue =com.project.entities/>


< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =showSqlvalue =false/>
< property name =generateDdlvalue =false/>
< property name =databasePlatformvalue =org.hibernate.dialect.MySQLDialect/>
< / bean>
< / property>

<! - < property name =persistenceUnitNamevalue =Project/> - >
< property name =persistenceXmlLocationvalue =classpath:META-INF / jpa-persistence.xml/>

< property name =loadTimeWeaver>
< bean
class =org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver/>
< / property>

< / bean>

< bean id =dataSourceclass =org.springframework.jndi.JndiObjectFactoryBean>
< property name =jndiNamevalue =java:jboss / datasources / MySqlDS/>
< / bean>


< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< property name =dataSourceref =dataSource/>
< / bean>

jpa-persistence.xml $ b

 <持久性> 
< persistence-unit name =Projecttransaction-type =RESOURCE_LOCAL>


< provider> org.hibernate.ejb.HibernatePersistence< / provider>
<非-jta-data-source> java:jboss / datasources / MySqlDS< / non-jta-data-source>
<! - <属性> - >
<! - < property name =jboss.as.jpa.providerModulevalue =hibernate3-bundled/> - >
<! - < / properties> - >

< / persistence-unit>

< /持久性>

DBNamedQuery.java

  @Entity 
@NamedQueries({

@NamedQuery(name = ...,query = ...),
@NamedQuery(name = ...,query = ...),

.....更多命名查询

})

public class DBNamedQuery {


}


解决方案

 我已经使用@NamedQuery注解将类中的所有命名查询写入了

在上面的陈述中,你没有清楚地提到你所指的班级类型?你需要在Entity类(用@Entity注解注解的类)中编写命名查询。



更新:
我对你的类感到困惑 DBNamedQuery 。你说你正在使用一个类来放置所有命名查询。我的理解是你正在使用这个类为你的应用程序的所有实体编写命名查询。如果这是正确的,你怎么能在你的类 DBNamedQuery 上使用 @Entity 注释,因为它不是一个真正的jpa实体?


包含 @NamedQuery 注解的类应该是一个托管实体。而我怀疑你的类 DBNamedQuery 不是。



为了确定问题,我建议检查日志,如果是一个管理的实体。如果你不能这样做,那么 EntityManger 会给你和API检查在运行时 contains(java.lang.Object entity)

在相关说明中,如果您使用注释,那么JPA命名查询是jpa实体的一部分。使用xml可让您灵活地存储在单独的文件中。


I am using hibernate 4.1.5.Final and Spring 3.1.2 Release and Jboss 7.1 . I have written all named queries in a class with @NamedQuery annotation but entity manager not creating named query . i am posting the stacktrace and 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

You have not mentioned clearly the type of class that you are referring in the above statement? You would need to write named queries in Entity class (class annotated with @Entity annotation).

UPDATE: I am somewhat now confused about your class DBNamedQuery. You said you are using one class to put all named queries. My understanding is you are using this class to write named queries for all the entities of your application. If that is correct how can you use @Entity annotation on your class DBNamedQuery because it is not really a jpa entity?

A class that contains @NamedQuery annotation should be a managed entity. And I suspect your class DBNamedQuery is not.

To identify the problem I suggest check in the logs if that is a manged entity. If you cannot do that then EntityManger gives you and API to check that during runtime contains(java.lang.Object entity).

On a related note, if you are using annotation then for JPA Named Queries are part of jpa entity. Using xml gives you a flexibility to store in a separate file.

这篇关于java.lang.IllegalArgumentException:未找到命名查询(实体管理器未创建NamedQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆