Hi there - please help me!
New to Hibernate and using in Spring container + Struts so the dependancies provide a steep learning curve.....
Getting session closed exceptions trying to us OSIV:
Want to use OSIV pattern to ensure that any Lazily loaded associated objects are available to my jsp's via Hibernate session before it's closed.
Have tried with the caveatemptor-native-061211 HibernateSessionRequestFilter with little luck - lots of LazyInitializationExceptions
Have also tried with Spring's OpenSessionInViewFilter helper filter.
This sets FlushMode to NEVER and, as I understand it, delegates or assumes to Tx boundaries will trigger management of FlushMode switching to allow save/updates etc.
We don't need Tx support yet so I've extended the Filter provided as such to a) set FlushMode.Auto on getSession() then b) flush and close session just before Http response is sent on closeSession():
Code:
public class HibernateOpenSessionInViewFilter extends OpenSessionInViewFilter {
protected Session getSession(SessionFactory sessionFactory)
throws DataAccessResourceFailureException {
Session session = super.getSession(sessionFactory);
session.setFlushMode(FlushMode.AUTO);
return session;
}
protected void closeSession(Session session, SessionFactory sessionFactory) {
session.flush();
SessionFactoryUtils.closeSession(session);
}
Seems to work well but if I mark any of my mappings as as FetchType.LAZY I get LazyInitializationException when I try and navigate to the specified association. Specifically I am told that the session is closed. Not sure what is closing it.
We are using DAOs that use the HibernateTemplate to persist ie
Code:
getHibernateTemplate().saveOrUpdate(o);
If I switch to EAGER all seems well eg following causes problem:
Code:
@OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE}, fetch=FetchType.LAZY)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
@JoinColumn(name="USER_ID",referencedColumnName="ID", nullable=false) //column on userprofile
private Set<ProfileVO> getUserProfiles() {
return userProfiles;
}
Any idea what I might be doing wrong
ps I have the following hibernate property set
<prop key="current_session_context_class">thread</prop>
All the best and thanks in advance for any insite..
[/code]