I've seen more or less similar posts around on this forum. Yet I would like to add one thing to the following 'problem'.
In Hibernate 3 by default association objects to a class are loaded lazily, thus a CGLIB proxy is used.
If you use the hibernate3 template and then use the hibernateTemplate.load(..) to get a single object, using a primary key that does not exist (hibernate would throw ObjectNotFoundException in case without proxies).
The "let us transform the ugly hibernate exception into a nice spring exception" code doesn't work. That is because a proxy is returned by session.load from Hibernate and it is never ever touched again by the HibernateTemplate's load method. Resulting a non-existing object as proxy is returned. Touching it later in one of the Dao's would result in the above-mentioned hibernate exception.. (or later in the front-end causing lazy exceptions). Not something for instance the front-end web-app. is expecting normally.
I must add that this is the case in an inheritance relation, where one class inherits another and I am loading the subclass (maybe not relevant, but just that you know).
Anyway in the execute(HibernateCallback ...) method the following code is used:
Touching the result object in any way, would still cause the expected hibernate exception to be thrown, so that it is nicely wrapped inside a spring exception.
Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session));
Object result = action.doInHibernate(sessionToExpose);
Let me know what you think,