Hibernate is performing unwanted SELECTs on call to saveOrUpdate
- by digiarnie
Let's say I have a House entity which maps to many Person entities. I then load an existing House which has 20 occupants.
beginTransaction();
House house = houseDao.find(1L);
commitTransaction();
Later in the code, I can then add a new Person to the House:
...
List<Person> people = house.getPeople();
people.add(new Person("Dilbert"));
....
When I make the call:
session.saveOrUpdate(house);
Hibernate performs 21 queries: 1 to SELECT the House and 20 to SELECT each existing Person in the House.
I'm sure it's a small issue on my part, however, what should I do so that I can add a new Person to the house without having such a heavy hit on the database in this situation?
This is all done within the same session.