Cannot create TypedQuery for query with more than one return

jpa
14,859

Solution 1

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

Solution:

Having a Query like this:

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;
       }
    ....
    }

Finally, modify your query

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

You are indicating how the objectes will be created.

Solution 2

This seems to be this bug: https://hibernate.onjira.com/browse/HHH-6304

It is apparently fixed in version 4.1.5.

Share:
14,859

Related videos on Youtube

saru
Author by

saru

Updated on June 04, 2022

Comments

  • saru
    saru almost 2 years

    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();
    

    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.