当我尝试执行下一个HQL查询时,我得到下一个错误:
org.hibernate.hql.internal.ast.QuerySyntaxException:无法找到 类[package.CountItemLike]上的适当构造函数[SELECT NEW package.CountItemLike(ll.itemId,COUNT(ll.itemId))FROM package.ItemLike AS ll GROUP BY ll.itemId]
在我的DAO课程中
@Override
public List<CountItemLike> countItemLikes() {
String hql = "SELECT NEW package.CountItemLike"
+ "(ll.itemId, COUNT(ll.itemId)) "
+ "FROM ItemLike AS ll "
+ "GROUP BY ll.itemId";
Query query = this.getCurrentSession().createQuery(hql); // ERROR IS HERE
return (List<CountItemLike>) query.list();
}
POJO
public class CountItemLike {
private int itemId;
private int likes;
public CountItemLike(int itemId, int likes){
this.itemId = itemId;
this.likes = likes;
}
public int getItemId() {
return itemId;
}
public int getLikes() {
return likes;
}
}
答案 0 :(得分:7)
使用聚合函数count
时,返回值为Long。这就是适用的构造函数的原因:
public CountItemLike(int itemId, Long likes){
this.itemId = itemId;
this.likes = likes.intValue();
}
或:
public CountItemLike(int itemId, long likes){
this.itemId = itemId;
this.likes = (int) likes;
}
也许ItemLike.itemId和int
之间还存在类型不匹配,但是从给定的代码中看不到。