-->
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.  [ 9 posts ] 
Author Message
 Post subject: Is there an easier way to load graphs than my example?
PostPosted: Tue Aug 17, 2004 8:15 pm 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
Hi guys,

I'm wondering if there is a cleaner way to initialize object graphs. This is what I'm doing now:

Code:
            UserRole userRole = (UserRole) reflectable;

            if( loadPrivileges ) {
               Hibernate.initialize( userRole.getPrivileges() );

               if( loadPages ) {
                  Iterator i = userRole.getPrivileges().iterator();
                  while( i.hasNext() ) {
                     UserPrivilege privilege = (UserPrivilege) i.next();
                     Hibernate.initialize( privilege.getPages() );
                  }
               }
            }


This is kind of verbose for something sort of simple, so I take it I'm doing something wrong (like I don't know about a feature, etc.). I'd appreciate it if you could give me some tips. Thanks so much.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 11:11 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
First off, can you post your mapping and java code?

Secondly, it seems that (as an example from your examples), getPrivileges returns a Collection. Lets say for the sake of argument, it's a List. Then why not map it as a List?

Then you could just say
Code:
    session.doLoad(user, id);


Or somesuch equivalent in your (static) Hibernate class.

_________________
--DP


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 7:36 am 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
dplass wrote:
First off, can you post your mapping and java code?

Secondly, it seems that (as an example from your examples), getPrivileges returns a Collection. Lets say for the sake of argument, it's a List. Then why not map it as a List?

Then you could just say
Code:
    session.doLoad(user, id);


Or somesuch equivalent in your (static) Hibernate class.


Well, I figure the structure of the objects is pretty self explanatory. I use lazy-load for all many-to-many relationships. Here, a role contains privileges and a privilege contains pages.

Secondly, that is Hibernate's static class, not mine. It's a method that is meant to eagerly fetch collections marked as lazy in the xml mapping files.

Also, there is nothing here that says it's a list. In fact, it makes sense that privileges would be modeled as a set since privileges usually don't apply in any order (or at least in my application anyway). Besides, loading strategies are pretty independant of all of these details anyway - a collection is a collection.

Of well, I guess I'll keep doing it this way unless I find a better way.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 7:39 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
show mapping file

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 7:43 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Guys, don't make it complex: He has a collection of entities and each entity again has a collection. He likes to fetch it all. This will be 1 outer-join select for the root entity and the first collection, and N selects for each secondary collection:

First:
"from Role r left join fetch r.privileges" (or use Criteria FetchMode)

Second:
iterate collection r.privileges: Hibernate.initialize(privilege.getPages());
OR
just let Hibernate load it once it is first accessed

You may use a batch-size on the privilege mapping to optimize the lazy loading.

Thats it, it doesn't get better than this.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 7:45 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Ups, wrong: batch size on the _pages_ collection mapping...

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 8:51 am 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
christian wrote:
just let Hibernate load it once it is first accessed


Is what I was going for. That's how I plan on doing it in my forthcoming rollout of Hibernate. Why make things hard? You guys did all the hard word (thanks, BTW) for us!

_________________
--DP


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 8:52 am 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
Oops, I meant

s/word/work/

_________________
--DP


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 10:19 am 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
Thanks Christian. That's exactly what I wanted to know.


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