gpt4 book ai didi

hibernate - QuerySyntaxException 在 Hibernate 中定位适当的构造函数

转载 作者:行者123 更新时间:2023-12-01 09:57:17 24 4
gpt4 key购买 nike

当我尝试执行下一个 HQL 查询时,出现下一个错误:

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]

在我的 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;
}

}

最佳答案

当使用聚合函数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 之间还有类型不匹配,但是从给定的代码中看不到这一点。

关于hibernate - QuerySyntaxException 在 Hibernate 中定位适当的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22207468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com