How to write a query in hibernate for count(*) and + (addition)

ⅰ亾dé卋堺 提交于 2019-12-25 03:18:04

问题


I want to execute the below query in Hibernate?

(select count(*) from login where emailid='something') + (select count(*) from user where nameid='something')

1st part OK:

Query query = session.createQuery(
        "select count(*) from LoginClass login where login.emailid=:email);
query.setString("email", "something");
Long count = (Long)query.uniqueResult();

2nd part OK:

Query query2 = session.createQuery(
        "select count(*) from UserClass login where name.nameid=:name);
query2.setString("name", "something");
Long count2 = (Long)query2.uniqueResult();

My question is for hibernate (HQL) not SQL.

but I want use only one query. When I use session.createSQLQuery I can use + (addition) between the select count(*)

EDIT 1:

UNION do not work (do not add)

SELECT COUNT(*) FROM LoginClass return 85

SELECT COUNT(*) FROM UserClass return 12

SELECT COUNT(*) FROM LoginClass UNION SELECT COUNT(*) FROM UserClass return same response of 1st (so 85 again).

EDIT 2:

This post is not a duplicate of Hibernate count from multi tables

select DISTINCT is not a addition. The result contain [85, 12] (not a long 97)

My question is for + char in (HQL) doing a addition, not multiple count(*).

来源:https://stackoverflow.com/questions/49529470/how-to-write-a-query-in-hibernate-for-count-and-addition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!