I am relatively new to hibernate, hence my apologies if the question turns out to be a dumb one...
I am trying to persist data in a following scenario:
Here is some background information:
TableObject1 <TO1>: is a pure table which hold new data
TableObject2 <TO2>: is a relationship table with Foreign Key pointing to <TO1>
A sample illustration of code is:
TO1 : Property1, Property2, Set <TO2> setTO2
TO2 : PK1, Property1, TO1
Now, initially using session.save(TO1 instanceTO1). I am able to persist the instanceTO1 data in to the corresponding table <TO1>.
Now I want to persist TO2 data into database table <TO2>. But the approach I am taking is to populate TO2 using instanceTO1.setTO2 property of TO1 class. So the property setTO2 of TO1 has a multiple TO2 items which would technically be insert statements into Database.
Problem: After I persist TO1 without populating setTO2 property, it updates table <TO1> in DB. Now, since hibernate checks for the changes in a persisted state object i.e. instanceTO1 of TO1 here. I expect it to do inserts into TO2 because I populate setTO2 property of TO1 after I persist it and this can happen at some point later. This is not happening when I am trying it out. Where am I going wrong?? session.saveOrUpdate(instanceTO1) does the inserts after i update it with TO2 sets. But I want to know the handling mechanism of hibernate and hence this question.
Thanks once again for answering my question in advance.
|