2 回答

TA贡献1842条经验 获得超12个赞
您可以使用投影:
@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();
其中MyObject可能是一个接口:
public interface MyObject {
@Value("#{target.age}")
int age();
@Value("#{target.weight}")
int weight;
}

TA贡献1872条经验 获得超3个赞
为了使用自定义类,我首先使用为这些属性创建一个构造函数
public class MyObject {
private int age;
private int weight;
public MyObject(int age , int weight) {
this.age=age;
this.weight = weight;
}
}
之后,在 hql 中你可以按照相同的值顺序调用这个构造函数
@Query("SELECT new com.packagename.MyObject(count(a.age), count(w.weight)) from animals a inner join weight_table w on a.id = w.id")
您将从 MyObject 对象返回一个列表
添加回答
举报