Java 如何在JPA中使用命名查询而不必在persistence.xml中列出实体?
我能够检索和持久化实体,而无需将其列在Java 如何在JPA中使用命名查询而不必在persistence.xml中列出实体?,java,maven,tomcat,jpa,openjpa,Java,Maven,Tomcat,Jpa,Openjpa,我能够检索和持久化实体,而无需将其列在persistence.xml中,但如果我想使用@NamedQuery,除非我将其列在persistence.xml中,否则它无法工作。有办法解决这个问题吗 我正在使用Tomcat8和OpenJPA2.4.2 persistence.xml <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns
persistence.xml
中,但如果我想使用@NamedQuery
,除非我将其列在persistence.xml
中,否则它无法工作。有办法解决这个问题吗
我正在使用Tomcat8和OpenJPA2.4.2
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence
version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<!-- Used for persisting events, authentication pieces -->
<persistence-unit name="PushPersistence" transaction-type="RESOURCE_LOCAL">
<!-- Use Apache's OpenJPA -->
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<!-- Our backing store is our data source from context.xml -->
<non-jta-data-source>java:comp/env/jdbc/webservice</non-jta-data-source>
<!--
Without this, I can load and persist the entity but
CANNOT use Named Queries
-->
<class>push.authentication.AuthCredentials</class>
<!-- Don't require listing the managed classes here -->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="openjpa.DynamicEnhancementAgent" value="true"/>
<property name="openjpa.RuntimeUnenhancedClasses" value="supported"/>
<property name="openjpa.Log" value="SQL=TRACE"/>
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72, PrintParameters=true, MaxActive=10, MaxIdle=5, MinIdle=2, MaxWait=60000"/>
</properties>
</persistence-unit>
</persistence>
我阅读了EntityManager接口,它说如果查询没有用给定的名称定义,或者如果发现查询字符串无效,或者如果发现查询结果不可分配给指定的类型,并且如果根据代码片段获取IllegalArgumentException,它将抛出IllegalArgumentException,我认为第三个是你的情况下,尝试使用
EntityManager.createNamedQuery(java.lang.String name);
而不是
EntityManager.createNamedQuery(java.lang.String name,
java.lang.Class<T> resultClass);
EntityManager.createNamedQuery(java.lang.String名称,
java.lang.Class resultClass);
看看你是否得到同样的结果
EntityManager.createNamedQuery(java.lang.String name);
EntityManager.createNamedQuery(java.lang.String name,
java.lang.Class<T> resultClass);