-->
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.  [ 5 posts ] 
Author Message
 Post subject: Initialize an object programmatically with one command
PostPosted: Mon Jun 30, 2008 5:10 pm 
Newbie

Joined: Sat Mar 29, 2008 1:31 pm
Posts: 8
Hy.

Is it somehow possible to fully initialize a fetched object programmatically with one command?
Something like:
Code:
obj = session.load();
Hibernate.initializeAll(obj);

or with Hql
Code:
"select Obj from Obj left join fetch evertyhing"

or Criteria
Code:
criteria.setFetchMode("*", FetchMode.JOIN)


Background:
I use Hibernate in my RCP desktop application. There was already some discussion how to best integrate Hibernate in a fat client app. I chose the way to only have a session available for some specific operations, and after that work with the detached objects. Often lazy loading is quite handy (sometimes a must, especially adding a new object reference to another object and store the last one back to db), so the standard lazy fetch strategy is globally enabled.
But sometimes I also have to load an object, that is fully initialized, as it will become detached and fully used afterwards.
The problem is, that some objects have so many references and collections, that writing an hql query gets annoying (and error-prone), as I have to use so many times "left join fetch".
So I am looking for one command to initialize the whole object at once (one ring to rule them all ;-)).

Best regards,
Kai


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 3:16 pm 
Newbie

Joined: Sat Mar 29, 2008 1:31 pm
Posts: 8
Really not possible??


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 3:28 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
I'm pretty sure there is no ready solution for this, but it shouldn't be to difficult to write your own:

- your start with an entity and call hibernate.initialize
- now use reflection or the metainformation from the configuration to find alls properties that need initialization and continue with those.

Not trivial, but shouldn't be that difficult either.

kind regards

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 3:31 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
tanjy wrote:
Really not possible??


Of course it is: if you map all your associated objects and collections with EAGER loading. Well, you know the price. If the object graph is too deep, you must stop somewhere. And, if you map with EAGER loading, your objects will ALWAYS be loaded eagerly. It is a pitty, that you cannot say it at runtime "NO, now no EAGER load".

So, now you can choose: writing fine grained queries or do it the lazy programmer's way with EAGER mapped - with the said consequences.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 8:53 pm 
Newbie

Joined: Thu Aug 14, 2008 10:40 pm
Posts: 2
In my case all my associations are Sets so it's pretty easy:

Code:
   public static void initializeAll(Class classType, Criteria criteria){
      Field[] fields = classType.getDeclaredFields();

        for (int i = 0; i < fields.length; i++) {
         if (fields[i].getType().equals(java.util.Set.class)) {
            criteria.setFetchMode(fields[i].getName().toString(),
                  FetchMode.JOIN);
         }
      }
   }


They should really think about having something like this built-in as it's pretty useful.


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