My application uses Hibernate to connect to SQL Server. I recently changed my DAO function that retrieves the count from one of the tables from “return query.getResultList().get(0)” to “query.getSingleResult()”. The sql count() query is supplied via namedQuery.
When I made this change, I noticed that the SQL generated by Hibernate has now changed from
select count(test0_.TestId) as …..
to
select top 2 count(test0_.TestId) as…
Why would Hibernate translate to top 2 and not top 1 for getSingleResult()?
Is there a way to turn off Hibernate modifying my count() query to use top 2 ?
Thanks
getSingleResultthrows an exception if there is not exactly one result. This is so it can check if there are more than one rows returned.