如何在Hibernate中进行SELECT查询包括子查询COUNT(*) [英] How to make a SELECT query in Hibernate includes Subquery COUNT(*)
本文介绍了如何在Hibernate中进行SELECT查询包括子查询COUNT(*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们有类别 - 商品 一对多关系。我想这样做
Let's say we have a Category - Items one-to-many relation. I would like to do this
SELECT c.*,
(SELECT COUNT(*) FROM items i WHERE i.catId=c.id)
AS itemCount
FROM category c
假设我们有一个Hibernate POJO类别。
And let's say we have a Hibernate POJO "class Category".
我的第一个问题是我真的无法弄清楚从那个查询我得到一个List< Category>对象吧?我怎样才能访问itemCount?因为没有 Category.getItemCount()
My first question is I really couldn't figure out that from that query I get a List<Category> object right? And how can I access the "itemCount"? Because there's no Category.getItemCount()
其次,如何编写Criteria查询?
And secondly, how can I write the Criteria query?
谢谢
推荐答案
似乎这就是我正在寻找的答案(进入POJO):
Seems like this is the answer I was looking for (into to POJO):
@Formula(value="(SELECT COUNT(*) FROM Items i WHERE i.id = id)")
@Basic(fetch=FetchType.EAGER)
public Integer getItemCount() {
return this.taskCount;
}
这篇关于如何在Hibernate中进行SELECT查询包括子查询COUNT(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文