-->
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: two many-to-one relations between two classes.
PostPosted: Tue Oct 11, 2005 5:47 pm 
Newbie

Joined: Mon Jul 25, 2005 12:59 pm
Posts: 13
Hi,
I have the following situation:
there is PERSONS table with CURRENT_ADDRESSID, which is a foreign key to ADDERESSES table. ADDRESSES table has a foreign key PERSONID to PERSONS table. This foreign key has also NOT NULL constraint and FOREIGN KEY constraint.
I model all these as many-to-one currentAddress relation between Person and Address, and as many-to-one "person" relation from Address to Person.
The first one is actually a one-to-one relation, but it should be modelled as many-to-one according to the Hibernate reference.
The second relation is a real many-to-one.
If Person becomes a new address, a new Address instance is created and set to currentAddress property of Person. Old Addess record remains in the database and must still point to its owning Person. This is done because of the historizaition requirement for addresses.
Now I want to create a new Person with an Address:

Code:
person = new Person(...);
person.setAddress(new Address(...));
session.save(person);


In Person.setAddess there is the following code:

Code:
this.address = address;
this.addess.setPerson(this);


And the database throws an exception about violating ADDRESSES.PERSONID NOT NULL constraint, when Hibernate tries to insert the Address record.
When I use not-null="true" for Adderess.person, Hibernate throws an exception: not-null property references a null or transient value. And I understand it, because it persists the address first and then the person (many-to-one Person-Address).
As soon as I remove the NOT NULL constraint, everything works.
Are there any ways to model my situation without removing the constraints?
Or may be there is something conceptually different solution for implementing the Person-Address-Person relation?

Best regards,
Sergiy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 11, 2005 7:55 pm 
Newbie

Joined: Wed Oct 05, 2005 3:36 am
Posts: 15
Its actually possible that many people could live at a given address.
Conversly over time a person could live at many address.

Hence you could model the main relationship as many-to-many by adding an extra link table.

Then the current address relationship is just a simple one (address) to many (people).

I am not sure if this will solve your issues however at least all the database constrains match the problem domain.


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.