-->
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.  [ 3 posts ] 
Author Message
 Post subject: Multiple Children of a Given Parent Table?
PostPosted: Wed Nov 23, 2005 4:49 pm 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
I have a situation where I have multiple possible parent objects, all of which could potentially contain the same sort of child objects.

Example:

public class person {
private string id;
private string name;
private Set Cars;
}

public class dealership {
private string id;
private string name;
private Set cars;
}

public class corporation {
private string id;
private string name;
private Set cars;
}

public class car {
private string id;
private int owner_class; // one of person, dealership, corporation
}

What's the best way to map this using hibernate? Right now I'm using:

<class name="Owner" table="owner" mutable="false">
<cache usage="read-only" />
<id name="id" type="string" length="32">
<column name="id" length="32" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" type="string"/>
<set name="cars" table="cars">
<key>
<column name="owner_id" length="32"></column>
</key>
<many-to-many class="Car">
<column name="car_id" length="32"/>
</many-to-many>
</set>
</class>

<class name="Corporation" table="corporation" mutable="false">
<cache usage="read-only" />
<id name="id" type="string" length="32">
<column name="id" length="32" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" type="string"/>
<set name="cars" table="cars">
<key>
<column name="owner_id" length="32"></column>
</key>
<many-to-many class="Car">
<column name="car_id" length="32"/>
</many-to-many>
</set>
</class>

This doesn't seem to work though because hibernate tries to put multiple foreign key constraints on the owner_id column of the join table.

Can anyone suggest a viable solution?


Top
 Profile  
 
 Post subject: many-to-many?
PostPosted: Wed Nov 23, 2005 7:03 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I'm inferring from the owner_class member of Car that you don't intend to track ownership history: a car has one owner in the DB. If that's the case, then why are you using many-to-many relationships? Shouldn't they be one-to-many? That should fix your FK issue, as the relationship can then be made unidirectional. See the collection examples section of the reference docs; you should be able to extend the last two examples to get what you want.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 3:01 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
What about having a carOwner superclass for person, dealership and corporation?

HTH
Ernst


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