Hi, is there a way to intercept lazy loading?
Interceptors and event listener doesn't seem to do the job.
What I want to do is to prevent lazy initialisation exceptions, like below. To avoid doing this for every single point, where I might use an uninitialized relation, it would be nice if there would be a single point intercept this.
The problem is that I'm changing an already existing Swing-Gui to use a database. Cause session
aren't recoverable, I decided to use the "single-session-per-request with detached objects" pattern.
Is this possible?
Thanks.
Greetings Michael
Code:
Session s = null;
try {
s = HibernateUtil.getCurrentSession();
if (!Hibernate.isInitialized(cm.getListOfPdfs())) {
s.beginTransaction();
s.update(cm);
Hibernate.initialize(cm.getListOfPdfs());
/* Prevent save, by update here! */
s.getTransaction().rollback();
}
return true;
}
catch (final HibernateException he) {
he.printStackTrace();
LOG.error(he.getMessage(), he);
return false;
}
finally {
if (s != null && s.getTransaction() != null && s.getTransaction().isActive()) {
s.getTransaction().rollback();
}
}
with this session context.
Code:
HIBERNATE_PROPERTIES.setProperty(org.hibernate.cfg.Environment.CURRENT_SESSION_CONTEXT_CLASS,
org.hibernate.context.ThreadLocalSessionContext.class.getName());