2 回答

TA貢獻1842條經驗 獲得超13個贊
您可以使用投影:
@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條經驗 獲得超4個贊
為了使用自定義類,我首先使用為這些屬性創建一個構造函數
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 對象返回一個列表
添加回答
舉報