I have a unidirectional many-to-many mapping between Person and Address (see example 7.3.4 of the Hibernate Reference Documentation 3.2.2).
The following code
Code:
Person person = new Person();
session.save(person);
session.delete(person);
results in:
Code:
Hibernate: insert into Person default values
Hibernate: delete from Person where personId=?
Adding a refresh:
Code:
Person person = new Person();
session.save(person);
session.refresh(person);
session.delete(person);
gives:
Code:
Hibernate: insert into Person default values
Hibernate: select person0_.personId as personId0_0_ from Person person0_ where person0_.personId=?
Hibernate: delete from PersonAddress where personId=?
Hibernate: delete from Person where personId=?
This is more or less the problem that I face in a real-world application. A user can select objects to be deleted from a list. For (lazy) objects that were already persistent, extra delete statements are generated, but for newly created objects, only the corresponding row in the Person table is deleted.
Can somebody explain me why Hibernate behaves this way? Is there a way to have delete statements for association tables at all times?