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

2019-08-19 15:33发布

问题:

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(*).