-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to intercept lazy loading?
PostPosted: Fri May 08, 2009 10:35 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
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());


Top
 Profile  
 
 Post subject: Re: How to intercept lazy loading?
PostPosted: Mon May 11, 2009 8:04 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi,

in the meantime, I have found this:
Best Practices for Thick-Client Applications

With this patch:
Hibernate Lazy Loading patch

Is this the only way?

Hibernate and Swing demo app
Seems only suitable for new swing apps.

Greetings Michael


Top
 Profile  
 
 Post subject: Re: How to intercept lazy loading?
PostPosted: Tue May 12, 2009 6:00 am 
Newbie

Joined: Thu Nov 15, 2007 3:17 am
Posts: 19
You can easily realize Lazy Loading on detached entities by using an AOP framework like Spring. Just put an aspect around the getters of your entities. The aspect catches the LazyInitializationException and - in this case reattach the entity to an open session and fetch the property.

Here is an example I wrote long time ago for remote lazy loading:
http://www.java-forum.org/allgemeines-e ... ework.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.