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.  [ 5 posts ] 
Author Message
 Post subject: Many one-to-one on the same persistent entity
PostPosted: Tue Dec 02, 2003 2:41 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
I persisting a graph with Hibernate 2.1. I have few objects that need to belong to only one of many objects at a time. In other words:

A (1) ---- (1) C OR B (1) ---- (1) C

If casacading is needed this will behave like:

A (1) ---- (1) C (1) ---- (1) B

meaning that C will belong to BOTH A and B at the same time.

One way to design these associations is using many-to-many:

A (*) ---- (*) C (*) ---- (*) B

Is there a better way to model these associations?

mota


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 7:43 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I'm not sure I understand your question correctly, but why many-to-one mapping from A to C (and many-to-one from B to C) doesn't work?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 12:56 am 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
In my design C needs to belong to either A or B. This means that C holds the foreign keys from A and B. If I need Hibernate to handle cascading (save or update) both these froeign keys should be not null. Therefore C must belong to BOTH A AND B at the same time. I need C to belong to EITHER A or B and have cascading.

This corresponds to the following case:
Customer has an Address
Company has an Address
Order has an Address

the same persistent entity Address can belong to a Customer, a Company, OR an Order. I also need cascading save/update from a parent to Address. One way to handle this is to use many-to-many instead of one-to-one. Is there a better suggestion?

mota


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 2:05 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Look, some time I had a similar problem - Customer, Employee and alot of other entities had a Location. Not sure if it was a correct way, but I didn't specify foreign keys from Location to all other tables. All other entities had foreign key to Location.

Yes, in this scenario Location did not know which object it belongs to. But it wasn't necessary in my case.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 2:10 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Btw, if you need reverse mapping (from Address to Order or Company), you may consider using <any> mapping.

But again, this isn't really necessary if all you need is cascading updates from Company/Order to Address.

In my case Employee mapping looks like:

Code:
        <many-to-one
            name="location"
            column="LOCATION_ID"
            class="Location"
            cascade="all"
        />


And Location class does not know anything about Employee/Company


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