HibernateTemplate's createQuery is not like the other convenience methods in HibernateTemplate: It is intended for use within a HibernateCallback - its javadoc briefly states this. That's why it takes a Session as parameter and why it throws HibernateException.
Code:
public Query createQuery([b]Session session[/b], String queryString) [b]throws HibernateException[/b];
It creates a Query that is aware of a transaction timeout; if you don't use such timeouts, it is equivalent to the Hibernate Session's createQuery.
Code:
List result = getHibernateTemplate().executeFind(
new HibernateCallback() {
Object doInHibernate(Session session) throws HibernateException {
Query query = getHibernateTemplate().createQuery("...");
// Query query = session.createQuery("...");
query...;
return query.list();
}
}
);
In general, HibernateTemplate's convenience methods are mainly intended for one-step data access operations. Any more complex operations, like building a Query or Criteria, or performing a sequence of steps, should be implemented in a HibernateCallback.
Juergen