Hibernate allows use to efficiently query relationships from a database via lazy-loading. However, relationships which are lazily-loaded can not be accessed if the hibernate session goes out of view. #{Album.artist.name}
Because of lazy loading, if you try to do the preceding relationship access in your EL (web page) statement, a lazy load error will be thrown. This is because if you are using JSF or some other non spring friendly web layer, and your session will cut out in the service layer.
To allow the above EL expression to take place, you can add the following filter to your web.xml.
However, because the default spring open session in view filter never flushes, you can never save anything in your web pages. To enable the session in view filter to flush, use the following class which extends the original spring session in view filter.
package wookets.utility;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
/
* @author wookets and an unknown person
/
public class OpenSessionInViewFilter extends org.springframework.orm.hibernate3.support.OpenSessionInViewFilter {
/
* we do a different flushmode than in the codebase here
*/
protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.COMMIT);
return session;
}
/**
* we do an explicit flush here just in case we do not have an automated flush
*/
protected void closeSession(Session session, SessionFactory factory) {
session.flush();
super.closeSession(session, factory);
}
}
I haven’t yet spent time dealing with lazy loads in remoting instances, so I’m not sure how that will all turn out. Hopefully, EJB3 and the Hibernate Entity Manager will solve these problems.
Links to other pages:
http://www.hibernate.org/43.html
Btw, if anyone can point me to the person that originally wrote this useful class, I will gladely give them credit.
Spring Hibernate Open Session In View Filter
Published
by map[name:Sean Wesenberg uri:https://plus.google.com/111523202047342274226]