gpt4 book ai didi

java.lang.IllegalArgumentException : Named query not found.(实体管理器未创建 NamedQuery)

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:13:03 24 4
gpt4 key购买 nike

我正在使用 hibernate 4.1.5.Final 和 Spring 3.1.2 Release 以及 Jboss 7.1。我已经在一个带有 @NamedQuery 注释的类中编写了所有命名查询,但实体管理器没有创建命名查询。我正在发布堆栈跟踪和 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 有点困惑。您说您正在使用一个类来放置所有命名查询。我的理解是您正在使用此类为应用程序的所有实体编写命名查询。如果这是正确的,你怎么能在你的类 DBNamedQuery 上使用 @Entity 注释,因为它实际上不是一个 jpa 实体?

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

要确定问题,我建议检查日志是否是托管实体。如果您不能这样做,那么 EntityManger 会为您提供 API 以在运行时检查 contains(java.lang.Object entity)

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

关于java.lang.IllegalArgumentException : Named query not found.(实体管理器未创建 NamedQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14152582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com