Hibernate is performing unwanted SELECTs on call to saveOrUpdate

Posted by digiarnie on Stack Overflow See other posts from Stack Overflow or by digiarnie
Published on 2011-01-05T02:15:21Z Indexed on 2011/01/05 7:53 UTC
Read the original article Hit count: 178

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate