Hello everybody,
I have a question about the saving and/or updating mechanism of hibernate.
In my application I have a lot of tables which are all mapped bidirectional.
In most cases I have a <one-to-many> assosiation on the one of the sides and a <set> on the other side.
The <one-to-many> side usually holds the foreign key.
It can happen that I save a pojo containing a <set> to the database where the set is null. This does not conflict with any of the databse constrains.
Later on (long after the origianl session has been committed) I construct the pojos for the set and add them to the set.
When I commit this pojo now using
Code:
session.saveOrUpdate(pojo)
I expect the the part of the pojo which is already persistant is updated (cause these table entries already exist) and the newly created pojos are inserted.
Interestingly this doesnt happen in my application. Hibernate wants to do an update on an not existing table entry which fails of course.
To make it clearer here a quick example:
I have Persons and phone numbers.
A person doesnt have to have a phone number, but it can have many.
An already "registered" person buys a phone and wants to register the phone numbner on its name.
In this case I have the Person pojo and say something like
Code:
PhoneNumber number = new PhoneNumber(new Long(234896423986489)); //lets say only the actual phone number is PK
if(person.getPhoneNumbers == null){ //create a new list of numbers
person.setPhoneNumbers(new HashSet());
person.getPhoneNumbers().add(number);
}
else{ //append to existing list
person.getPhoneNumbers().add(number);
}
number.setPerson(person); //set the person for the phone number
Now, lets say that the Persons ID was "33".
In this case my hibernate creates the sql:
Update PHONENUMBER set PERSON_ID=33 where NUMBER=234896423986489
This is not right, cause there is no entry with NUMBER=234896423986489
Do you know what I'm trying to say?
Do you understand my problem?
Sorry I havent included any mappings, cause I did this quick and dirty from scratch.
I'll include some mapping files if you weant me to do so, but I think my problem is comprehensible without them.
Thank you very much!
Stefan