在 Pro JPA 2 (Apress) 书中,我看到过这样的例子,
EntityManager em;
Long count = em.createNamedQuery(countQueryName, Long.class).getSingleResult();
但是,api ,希望应用于 JPA 2.0,显示语法为
createNamedQuery(String name)
Parameters:
name - the name of a query defined in metadata
只有一个参数。这本书广泛使用了该类型,那么我错过了什么?
谢谢。
最佳答案
Java EE 5 使用 JPA 1,Java EE 6 使用 JPA 2。以下是您正在寻找的方法:
createNamedQuery(java.lang.String name, java.lang.Class resultClass) :
Create an instance of
TypedQuery
for executing a Java Persistence query language named query. The select list of the query must contain only a single item, which must be assignable to the type specified by theresultClass
argument.Parameters:
name
- the name of a query defined in metadataresultClass
- the type of the query resultReturns: the new query instance
Throws:
IllegalArgumentException
- if a query has not been defined with the given name or if the query string is found to be invalid or if the query result is found to not be assignable to the specified typeSince: Java Persistence 2.0
关于java - JPA createNamedQuery 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3380532/