-->
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.  [ 2 posts ] 
Author Message
 Post subject: Preload Pattern implementation using an event listener
PostPosted: Tue Apr 15, 2008 3:49 am 
Newbie

Joined: Fri Apr 11, 2008 10:42 am
Posts: 2
For our projects I developed a so called preload pattern. In short, with the preload pattern the client tells the backend which entities to preload via an additional parameter. At this point I don't want to discuss the pros and cons of the preload pattern. For us the pattern works perfectly well for all kind of GUIs.
Below is the most important code fragment of a new implementation. My question is if the usage of Hibernate.initialize() within the onPostLoad() method is ok or could there be any drawbacks which I don't overlook at the moment?

Code:
public class PreloadEventListener extends DefaultPostLoadEventListener {

  private static ThreadLocal<Preload[]> preloadsThreadLocal = new ThreadLocal<Preload[]>();

  public static void setPreloads(Preload[] preloads) {
    preloadsThreadLocal.set(preloads);
  }

  public static void clearPreloads() {
    preloadsThreadLocal.set(null);
  }

  protected Object callGetter(Object entity, Preload preload) {

    try {
      // Call the getter on the entity via reflection
      return preload.callGetter(entity);
    } catch (Exception ex) {
      String msg = "Can't invoke getter for property: " + preload.getProperty();
      throw new PreloadException(msg, ex);
    }
  }

  public void onPostLoad(PostLoadEvent event) {

    Object entity = event.getEntity();

    Preload[] preloads = preloadsThreadLocal.get();

    if( preloads != null ) {
      for (Preload preload : preloads) {
        if (preload.getModelClass().isInstance(entity)) {
          Object getterResult = callGetter(entity, preload);
          Hibernate.initialize(getterResult);
        }
      }
    }

    super.onPostLoad(event);
  }
}


Preload machanism is activated in the GenericDAO like so:
Code:
...
PreloadEventListener.setPreloads(preloads);
result = crit.list();
PreloadEventListener.clearPreloads();
...


If somebody is interested in the full code please let me know.


Top
 Profile  
 
 Post subject: Re: Preload Pattern implementation using an event listener
PostPosted: Mon Oct 01, 2012 5:02 am 
Newbie

Joined: Mon Oct 01, 2012 5:00 am
Posts: 1
Hi, thank you for sharing your code.

It would be very nice to know the content of the method "return preload.callGetter(entity);"
Could you please post the Preload class?

Is this still the way you do it? Is your code already used in production.

Thanks again
~Ingo


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.