Search code examples
databasehibernatecriteriaprojection

criteria.setProjection(Projections.rowCount()) with Hibernate produces unexpected result


We are using hibernate 3.3 for our application and facing this weired problem in which

Criteria criteria = getSession().createCriteria(table.class);

criteria.setProjection(Projections.rowCount()); returns 4 rows, 3 having values '0' and 1 with correct value.

Because of this issue criteria.uniqueResult(); throws an exception saying org.hibernate.NonUniqueResultException: query did not return a unique result: 4

Any help would be highly appreciated.

Thanks in advance.


Solution

  • I got the reason myself!

    There are multiple classes (3 exactly) which are inheriting the table we are firing this query on("table"). So, because of this our query gets fired to all 4 tables.

    Resolved a lot of our problems.

    Wowww!!! What a relief it is. :)