-->
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: Get entity with all proxies (lazy properties) initialized
PostPosted: Mon Jan 19, 2009 12:39 am 
Newbie

Joined: Thu May 10, 2007 1:38 pm
Posts: 3
Hi,

I'm posting this to get some feedback.

Being fairly well-read in the use of Hibernate I'm definitely aware of the long-conversation and 'session in view' concepts. However, like several of my projects and many posts in this forum, sometimes you just have to have all lazy properties of an entity initialized on the return of some method and sometimes you just want the value-typed properties or can use lazy initialization.

SO, I was faced with writing a separate DAO method for retrieving & initializing every entity type and instead spent some time trying to come up with a method that would do it for any entity type.

One thing of note: The PropertyUtils class is just a nifty helper from Apache Commons BeanUtils library.

Here it is: What do you think? This bad for some reason? More efficient way to do it?

Code:
    @SuppressWarnings("unchecked") // We know it will be of the type passed
    public <T> T getFull(Class<T> entityType, Serializable id)
    {
        T entity = (T)getSessionFactory().getCurrentSession().get(entityType, id);
       
        ClassMetadata classMetaData = getSessionFactory().getClassMetadata(entityType);
        String[] propNames = classMetaData.getPropertyNames();

        for (int i = 0; i < propNames.length; i++){
            try {
                Hibernate.initialize(PropertyUtils.getProperty(entity, propNames[i]));
            } catch (RuntimeException re) { throw (re); }           
              catch (Exception e) { throw new RuntimeException(e); }
        }
        return entity;
    }


FYI, here's the method that just gets the entity w/o initialization:

Code:
    @SuppressWarnings("unchecked") // We know it will be of the type passed
    public <T> T get(Class<T> entityType, Serializable id)
    {
        return (T)getSessionFactory().getCurrentSession().get(entityType, id);
    }


Thanks!
Scott


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 12:48 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
This is pretty much what we're doing but you may want to consider the following enhancements.

You could handle collections in a special manner as if you had an a A with a collection of Bs and wanted to also load C you might want to try A.B.C but that should give you an exception since the property utils would try to call getC() on your collection container.

The way you have it implemented is way simpler as if you want to handle collections you actually need to break out the calls based on the "." in order to check the types as you walk your chain.

Even if you're not handling collections and want to support A.B.C for walking a many-to-one chain you might want to handle the exception where B is null. Calling PropertyUtils in that case will blow an exception IIRC and anything earlier in the chain wouldn't be loaded either.

In our case we're parsing the chain as we go so we don't run into that.

If you don't need any of the functionality above then what you have is perfect as far as I am concerned.

I would actually love to have a fetch mode in hibernate that you can specify the associations you want to force loading on with dot notation w/o having to try to force an outer join. i.e. the loading would occur when the entity is hydrating.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


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.