Reputation: 4111
When I try to do the next one HQL query i get next error:
org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [package.CountItemLike] [SELECT NEW package.CountItemLike(ll.itemId, COUNT(ll.itemId)) FROM package.ItemLike AS ll GROUP BY ll.itemId]
In my DAO class
@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;
}
}
Upvotes: 3
Views: 6369
Reputation: 42084
Returned value is Long when aggregate function count
is used. That's why applicable constructors are:
public CountItemLike(int itemId, Long likes){
this.itemId = itemId;
this.likes = likes.intValue();
}
or:
public CountItemLike(int itemId, long likes){
this.itemId = itemId;
this.likes = (int) likes;
}
Maybe there is additionally type mismatch between ItemLike.itemId and int
, but that cannot be seen from the given code.
Upvotes: 7