简体   繁体   English

在JPQL中选择NEW

[英]SELECT NEW in JPQL

I'm having an issue with a SELECT NEW query. 我遇到了SELECT NEW查询的问题。

Query q = em.createQuery(
           "SELECT NEW com.bcbst.odstats.ejb.RangeStats(a.folderName, SUM(a.hits)) " +  
           "FROM ODStat a GROUP BY a.folderName");
return q.getResultList();

I get the following stacktrace when I try to run this query. 当我尝试运行此查询时,我得到以下stacktrace RangeStats does have public methods, the FQN is accurate, in fact RangeStats resides in the same package as the class making the query. RangeStats确实有公共方法,FQN是准确的,实际上RangeStats与创建查询的类位于同一个包中。 I've done queries on ODStat before, so I'm also confident that a.folderName and a.hits is OK. 我以前在ODStat上做过查询,所以我也相信a.folderName和a.hits是可以的。

I'm developing with RAD 7.5, WAS 7, OpenJPA 1.2.1, MS SQL JDBC Driver 1.2. 我正在开发RAD 7.5,WAS 7,OpenJPA 1.2.1,MS SQL JDBC Driver 1.2。

    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:896)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1527)
Caused by: javax.ejb.EJBException: See nested exception; nested exception is: 
<openjpa-1.2.1-SNAPSHOT-r422266:686069 nonfatal user error> 
org.apache.openjpa.persistence.ArgumentException: Result type "class 
com.bcbst.odstats.ejb.RangeStats" does not have any public fields or setter methods 
for the projection or aggregate result element "jpqlalias1", nor does it have a 
generic put(Object,Object) method that can be used, nor does it have a public 
constructor that takes the types [class java.lang.String, long].

Caused by: <openjpa-1.2.1-SNAPSHOT-r422266:686069 nonfatal user error> 
org.apache.openjpa.persistence.ArgumentException: Result type "class 
com.bcbst.odstats.ejb.RangeStats" does not have any public fields or setter methods 
for the projection or aggregate result element "jpqlalias1", nor does it have a 
generic put(Object,Object) method that can be used, nor does it have a public 
constructor that takes the types [class java.lang.String, long].

at org.apache.openjpa.kernel.ResultPacker.<init>(ResultPacker.java:139)
at org.apache.openjpa.kernel.ResultPacker.<init>(ResultPacker.java:102)
at org.apache.openjpa.kernel.QueryImpl.getResultPacker(QueryImpl.java:1262)
at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1212)
at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:990)
at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:805)

Found the problem. 发现了问题。 The constructor accepted (String, int) when it needed (String, long) . 构造函数在需要时接受(String, int) (String, long)

Follow me 跟着我

 public Company(String name, int iva, String businessArea, String coreBusiness) {
    this.name = name;
    this.iva = iva;
    this.businessArea = businessArea;
    this.coreBusiness = coreBusiness;
    }

Query query = session.createQuery("select new Company (name, iva, businessArea, coreBusiness)  from Company");


obs : org hibernate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM