-->
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: Are bidirectional associations sensible?
PostPosted: Wed Oct 05, 2005 9:27 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:20 am
Posts: 40
Location: Vienna
Hi,

I am experiencing troubles with bidirectional associations. I use scaffolding code as proposed in Hibernate in Action (page 108):
Code:
   dealUnit.setTgrunit(tgrunit);
   tgrunit.getDrddealunits().add(dealUnit);


However, as there may be lots of dealUnits to one tgrunit, the second line of code loads MANY dealUnits into memory, which I do no really need.

The following is now stated in the Hibernate FAQ:

Quote:
Why does Hibernate always initialize a collection when I only want to add or remove an element?

Unfortunately the collections API defines method return values that may only be computed by hitting the database. There are three exceptions to this: Hibernate can add to a <bag>, <idbag> or <list> declared with inverse="true" without initializing the collection; the return value must always be true.

If you want to avoid extra database traffic (ie. in performance critical code), refactor your model to use only many-to-one associations. This is almost always possible. Then use queries in place of collection access.


So I see three possible sollutions:
- use bags instead of maps
- make the association unidirectional
- just remove the second line of my code above

I think that solution 3 is not really elegant, although it works. However, can anybody tell me what his experience with this problem is? It seems to me that making the association unidirectional will in most cases be the right thing to do, because in most cases there will be more than just a few items on the n side, and it will hardly ever be sensible to work with collections with thousands of items.

regards
Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 6:01 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
In refining my Hibernate-persisted data model, I found many cases where the advice in the advice you posted makes sense:

Quote:
If you want to avoid extra database traffic (ie. in performance critical code), refactor your model to use only many-to-one associations. This is almost always possible. Then use queries in place of collection access.


Initially everything was laid out in my head as one-to-manys; lots of my entities had Sets or Lists on them. Most of the time (only you can decide for sure), the unidirectional solution (many-to-one) is the best choice because so much of the time when you load the parent, you don't even touch the children. This is almost certainly the case if there are LOTS of children.

So I would do away with the collection that's attached to TgrUnit and retrieve the DealUnits with a separate query, thus avoiding all the potential hassles of managing both ends of a bidirectional relationship.

Just my 2 cents.

-Chris


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.