无法为具有多个返回的查询创建TypedQuery [英] Cannot create TypedQuery for query with more than one return

查看:126
本文介绍了无法为具有多个返回的查询创建TypedQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下JPA查询,并且得到了java.lang.IllegalArgumentException:无法为具有多个返回异常的查询创建TypedQuery.

I am using the following JPA query and i am getting the java.lang.IllegalArgumentException: Cannot create TypedQuery for query with more than one return Exception.

TypedQuery<RaBdrRating> uQuery = 
  (TypedQuery<RaBdrRating>)entityManager.createQuery("
     SELECT r.activePackage,SUM(r.duration),SUM(r.charge),COUNT(r) 
     FROM RaBdrRating r WHERE r.callType = :callType 
     and r.startDate between :startDate and :endDate 
     GROUP BY r.activePackage",RaBdrRating.class);

uQuery.setParameter("callType", model.getCallType());
uQuery.setParameter("startDate",startDate);
uQuery.setParameter("endDate",endDate);
List<RaBdrRating> listOfPackages = uQuery.getResultList();

任何人都可以告诉我我的查询中有什么问题吗.....我是JPA的新手,但我没有发现问题所在,因此在这里提出来.如果有人有主意请告诉我.

Can any one tell me what is wrong in my query.....I am new to JPA and i am not getting what is the problem and strucked up here.If any one have idea please tell me.

推荐答案

我遇到了同样的错误:无法使用请求的结果类型为返回多个以上查询的查询创建TypedQuery

I go the same error: Cannot create TypedQuery for query with more than one return using requested result type

解决方案:

具有这样的查询:

Select e.fieldA, e.fieldB, e.fieldC From Entity e

您必须使用查询中指定的参数声明一个构造函数:

You have to declare a constructor with the parameters specified on query:

    package somepackage;

    public class Entity {
    ...
       public class Entity() {}

       public class Entity(Type fieldA, Type fieldB, Type fieldC) {
           this.fieldA = fieldA;
           this.fieldB = fieldB;
           this.fieldC = fieldC;
       }
    ....
    }

最后,修改您的查询

Select NEW somepackage.Entity(e.fieldA, e.fieldB, e.fieldC) From Entity e

您正在指示将如何创建对象.

You are indicating how the objectes will be created.

这篇关于无法为具有多个返回的查询创建TypedQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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