How to get row count after JOIN in Hibernate Criteria?

1k views Asked by At

I try to create request:

Criteria criteria = session.createCriteia(User.class);
criteria.setFetchMode(field, FetchMode.JOIN);
int count1 = criteria.list().size(); //340
Number count2 = (Number) criteria.setProjection(Projections.rowCount()).uniqueResult(); //279

How to return 340 without calling criteria.list().size() ?

1

There are 1 answers

0
Vicky Thakor On BEST ANSWER

I think while using Projections.rowCount() hibernate ignores

criteria.setFetchMode(field, FetchMode.JOIN);

You can use createAlias

criteria.createAlias("field","field", JoinType.LEFT_OUTER_JOIN);

Check query generated by hibernate in both the cases by using hibernate.show_sql in Hibernate configuration file

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html#configuration-optional