HQL return List<MyClass> instead List<Object[]>

John :

In my HQL:

@Query("SELECT count(a.age) as age, count(w.weight) as weight from animals a inner join weight_table w on a.id = w.id")

I want return it as: List<MyObject> instead of List<Object[]>

I have this class:

public class MyObject {
     private int age;
     private int weight;

     // getters setters all args constructor
}

is there possible to cast this in my HQL using something like this:

SELECT new.com.cs.MyObject(age, weight) count(a.age) as age, count(w.weight) as weight from animals a inner join weight_table w on a.id = w.id
Alan Sereb :

You can use projection:

@Query("SELECT count(a.age) as age, count(w.weight) as weight from animals a inner join weight_table w on a.id = w.id")
public List<MyObject> myMethodNameDescribingFunctionality();

where MyObject could be an interface:

public interface MyObject {
    @Value("#{target.age}")
    int age();

    @Value("#{target.weight}")
    int weight;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165747&siteId=1