File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate multiple one-to-many relationships to the same class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate multiple one-to-many relationships to the same class" Watch "Hibernate multiple one-to-many relationships to the same class" New topic
Author

Hibernate multiple one-to-many relationships to the same class

Ram Raju
Greenhorn

Joined: Mar 11, 2003
Posts: 17
I have multiple one-to-many relationships to the same class from a class. Is there a way to Map it?

<class name="A" table="A">
...
<set name="x">
<key column="a"/>
<one-to-many class="B"/>
</set>

<set name="y">
<key column="a"/>
<one-to-many class="B"/>
</set>

<set name="z">
<key column="a"/>
<one-to-many class="B"/>
</set>
</class>
--------------------------------------------
class B {
public String value;
}
--------------------------------------------
I just use B to save a list of options for different attributes of A. Can I map using one generic class B or do I need to have a different class for each attribute in A (Bx, By, Bz). Is there a better way to address the whole issue?

Thanks in advance
Ram
[ March 14, 2006: Message edited by: Ram Raju ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Mapping it as you have done is OK. You need three different properties, each a Set of the same class. Hibernate will only load a class B one in a session, no matter how many sets it appears in.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ram Raju
Greenhorn

Joined: Mar 11, 2003
Posts: 17
Paul,

Thankyou for the response. I have a problem with the mapping I posted. All the entries in table B for a particular A are loaded into all the sets (x, y, z). I guess this is because there is nothing in the mapping to diffrentiate b/w various sets.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I guess this is because there is nothing in the mapping to diffrentiate b/w various sets.

No, there is something to differentate between the sets - each has a different name. However, each have the same key. So you have three sets, all describing the same relationship.
Ram Raju
Greenhorn

Joined: Mar 11, 2003
Posts: 17
Paul,

I am not sure if I described my problem correctly. I want the values in x, y, z to be different.

x -> "alpha", "beta"
y -> "gamma"

but with current mapping, all values are getting loaded into all the attributes.

x -> "alpha", "beta", "gamma"
y -> "alpha", "beta", "gamma"

Since the database structure for table B is

------------------
id | value | a_id
------------------
1 | alpha | 1
2 | beta | 1
3 | gamma | 1

How would it know what to load in x and what to load in y.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hibernate is loading the data just as you have defined it. a_id == 1, so a valid set of table B objects for any of your mapped sets is {alpha, beta, gamma}. The way your data model is defined the only way you will restrict the set is if a_id is different for each set. And that can't happen for the same record, since a_id is an FK to the PK of table A.
Ram Raju
Greenhorn

Joined: Mar 11, 2003
Posts: 17
I agree with you. So can you please suggest on how to implement this.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I think we are talking at cross purposes.

Given your data model, you have one set of table B objects per table A, and there is no way round that. You can't use Hibernate to subvert the constraints of the relational model. I think you problem is you think you've got a one-to-many relationship and you really got a many-to-many relationship. How do you currently get this data via SQL?
Ram Raju
Greenhorn

Joined: Mar 11, 2003
Posts: 17
Well this is new development. Data model changes are still welcome.

my goal -> [B]
------------------------------------
id | value | type | a_id
------------------------------------
1 | alpha | x | 1
2 | beta | x | 1
3 | gamma | y | 1

and hibernate should take care of populating the object x/y based on the type. I need to know if I can do this and how to write the mapping for that.

Thanks
Ram
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Ah now we've got a polymorphic mapping, which make a little more sense. In theory, it would be possible to have a set of sub classes of "TableB" which Hibernate could create based on a discriminator. However, the FK constraint would still cause all table B records to be returned for all your table A sets. Its just that this time you'd have more than on type of object in your set. So, as your data model stands you still can't get what you are after.

Suppose you have your set of values {alpha, beta, gamma} contained in one table and your tableA properties in another, to implement the relationship you want you'll need a third table to implement the many-to-many relationship. So:



This would give you your many to many.
Sebastian Hennebrueder
Ranch Hand

Joined: Sep 09, 2004
Posts: 49
Hello,
The relation from A is set as a foreign key field in B.
So you must define in B to which relation in A you are referring
=> composite-id
B
(type, a_id, other field)

Have a look in the Hibernate reference (or in my book) and search for composite-id and formula. There is an example.
Best Regards Sebastian


Things get always more complicated as expected.<br />Author of eBook Hibernate 3 Developer Guide by example<br />Tutorials about Hibernate, EJB, Struts, JSF <a href="http://www.laliluna.de" target="_blank" rel="nofollow">www.laliluna.de</a>
Ram Raju
Greenhorn

Joined: Mar 11, 2003
Posts: 17
Thankyou for your help.
 
Consider Paul's rocket mass heater.
 
subject: Hibernate multiple one-to-many relationships to the same class
 
Similar Threads
adding to a table with no primary keys
the many vs one sides of a cascaded hibernate object...
How to specify relationship in mapping file
Hibernate - Legacy Database Question
Help with mapping set - Hibernate