Hibernate version: 3.0 final
Name and version of the database you are using: MySQL 4.0.24
I would like to get the number of elements of a certain object.
The following query works fine with the classic query parser. The exception below is thrown with the new query parser.
The documentation says that COUNT(*) is a supported aggregate function in HQL.
Code
Code:
final StringBuffer buffer = new StringBuffer(200)
.append("SELECT COUNT(*) FROM Order AS order ")
.append("JOIN order.versions ")
.append("WHERE order.node = :node ");
final Session session = Li2DHibernateUtil.getSession();
final Query query = session.createQuery(buffer.toString());
query.setString("node", "12345");
final int result = ((Integer) query.uniqueResult()).intValue();
Exceptionorg.hibernate.QueryException: Unable to find SQL function: COUNT [SELECT COUNT(*) FROM de.company.project.persistence.Order AS order JOIN order.versions WHERE order.node = :node ]
PS: When searching for an alternative way getting the number of query results, I found the following peace of code in the Hibernate 3.0 documentation. Maybe this part should be removed or replace since Session.iterate is deprecated in Hibernate 3.0...?
Code:
( (Integer) session.iterate("select count(*) from ....").next() ).intValue()