views:

15

answers:

1

I have a class A which has set of class B. class A and class B has their own table. When i insert class A it also insert class B. When i update class B, Hibernate deletes everything from class B which belongs to that instance of A and then insert all values in current set B. I want that if only 2 new values are added in set b, then it only insert that 2 values and leave the rest. I try saveorupdate but it is not working.

A: 

Hibernate usually behaves like this when you clear the set before adding the items, or when you replace the set with a new instance. In both cases, it deletes the items in one sql statement. It is basically an optimization when the list had been built up from scratch.

Make sure that you only remove items which you don't need anymore.

Stefan Steinegger
Currently i am getting class A from database and then compare new values of A with previous value of A. I add ,remove entries from B in my code by comparing old set and new set. But it is painful to write code for each and every set. Therefore i am looking for another solution
dmay
write a helper class with some set based tools.
Stefan Steinegger