-->
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.  [ 4 posts ] 
Author Message
 Post subject: mapping multiple associations with the same class
PostPosted: Fri May 23, 2008 1:31 pm 
Newbie

Joined: Thu Apr 08, 2004 8:46 am
Posts: 11
Location: Istanbul
Hi,
I have a mapping question.
Suppose I have a master class "Payroll" which has more than one associations with another
Code:
class "PayrollSection". Each have their role names:
class Payroll {
long id;
List<PayrollSection> shiftSections;
List<PayrollSection> paymentSections;
List<PayrollSection> deductionSections;
PayrollSection legalDeduction;
...
}


Here I have 3 unidirectional one-to-manys and 1 one-to-one. I could not figure out how to tell hibernate about their role names so hibernate can distinguish them when generating SQLs.
One solution seems like adding a property to PayrollSection as a discriminator and using a filter clause within <one-to-many> but that I could not get it to work. If that is the way to go I may elaborate my question in that direction.
Other solution seemed like adding the discriminator again and using the "where" clause with <one-to-many>. But in that case the where clause consists of what i included there and i do not know how to make it add the key equality ANDed to my discriminator clause. like
Code:
paymentsec0_.payroll=?
I may elaborate on that too if that is the direction.
I would like to know how you guys handle such a mapping requirement.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 24, 2008 4:59 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Hi laxu,

the problem is: how should Hibernate decide which of your PayrollSection objects is going into the "shiftSections" list, which into the "paymentSections" list and so on. After all, in the database table for your Payrolls there are only foreign keys to the owner object "Payroll".

So, you must have some information on the role each PayrollSection object has. I think the best solutions is to use subclasses and map these subclasses to the same database table, each subclass having a distinct discriminator. This construct then allows Hibernate to automatically fill your different lists with the correct objects.

Another approach would be using some role enumeration variable. But this means that your code is responsible to fill the queried objects into the list - Hibernate would not have this knowledge.

Carlo
----------------------------------------
please credit me if this helped you.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 24, 2008 1:24 pm 
Newbie

Joined: Sun Apr 09, 2006 8:38 am
Posts: 14
I've had this problem before and as far as I can understand this is
an "open loop" in hibernate.

ways around it are subclass (as suggested), change the collections mappings to many-to-many (will create a collection table per collection), or add a custom column with role, maintain it yourself (e.g. when adding to the collection) and write the select clause for the elements manually (which seems like a huge overkill to me).


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 26, 2008 4:27 am 
Newbie

Joined: Thu Apr 08, 2004 8:46 am
Posts: 11
Location: Istanbul
thank you guys for the solutions offered but i already had considered them yet i decided they were just workarounds rather than solutions. though i have been using hibernate since quite a long time and have gone through exotic mappings so far I just realized that I never needed such a simpler mapping yet.
i am a bit taken aback that such a mapping is not addressed by hibernate so far. It would be nice to hear developers' comments on it.


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