本文介绍了在hibernate中使用createSQLQuery获取count(*)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个sql查询,我只想在数据库。
我在整个应用程序中使用hibernate,所以我宁愿使用hibernate来调用这个sql查询。



在下面的例子中,我想获得count + name,但是在我使用createSQLQuery()时,无法找到如何获取这些信息。



我看到了解决方法,人们只需要得到一个

 <$ c $  c> SELECT count(*),a.name as count FROM user a 
WHERE a.user_id IN(SELECT b.user_id FROM user b)
GROUP BY a.name
HAVING COUNT *)2和5之间;

fyi,如果我直接在数据库上调用它, / p>

  1,John 
2,Donald
1,Ralph
... $ b b


解决方案

>

我在初始查询时遇到的问题是,count是一个保留字:P
当我将名称更改为其他工作时。


I have several sql queries that I simply want to fire at the database.I am using hibernate throughout the whole application, so i would prefer to use hibernate to call this sql queries.

In the example below i want to get count + name, but cant figure out how to get that info when i use createSQLQuery().

I have seen workarounds where people only need to get out a single "count()" from the result, but in this case I am using count() + a column as ouput

SELECT count(*), a.name as count FROM user a
WHERE a.user_id IN (SELECT b.user_id FROM user b)
GROUP BY a.name
HAVING COUNT(*) BETWEEN 2 AND 5;

fyi, the above query would deliver a result like this if i call it directly on the database:

1, John
2, Donald
1, Ralph
...
解决方案

cheers for the info Thomas, worked wonderful for generating objects

the problem i had with my initial query was that "count" was a reserved word :Pwhen i changed the name to something else it worked.

这篇关于在hibernate中使用createSQLQuery获取count(*)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 03:48