I've been playing with it today, and the best solution seems to be to not use HibernateUtil. The reason it was created was to solve many of the problems that are taken care of by org.jboss.hibernate.session.HibernateContext. Usage of HibernateContext results in less code. The same amount in the code using Hibernate, minus the whole HibernateUtil class.
Highly recommended reading has been provided by JBoss already: http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossHibernate2
The upshot of this is to just be sure to call session.flush(); before closing the session.
When using HibernateContext is closing the session even necessary?
Any examples. Can someone provide any class similar to HibernateUtil?
In Hibernate3 I think that you should use SessionFactory.getCurrentSession() instead of HibernateContext. I think that will do the same thing in application server independent way.
But the problem that I see here is that neither of these will work if you are not inside a transaction. And I would like to keep the code similar whether I'm inside transaction or not. And it feels unnecessary overhead to have transaction in every select method.
One solution that came to my mind is that create a HibernateUtil that uses SessionFactory.getCurrentSession(), traps exception that happens if transaction is not running, and then creates and returns ThreadLocal session instead.
The closeSession() method would have to close the session if it is ThreadLocal session and do nothing if it's Transaction related session. Don't know would this work, but I'm going to try...