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