Reputation: 815
The third database command from bottom-up is DELETE FROM Person_Phone WHERE Person_id = 1 why delete person id instead of directly deleting phone id? especially since that is what the java code explicitly does with person.getPhones().remove( phone1 ) . PS. I copied the code below from this link http://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#associations
//this is Java code
Person person = new Person();
Phone phone1 = new Phone( "123-456-7890" );
Phone phone2 = new Phone( "321-654-0987" );
person.getPhones().add( phone1 );
person.getPhones().add( phone2 );
entityManager.persist( person );
entityManager.flush();
person.getPhones().remove( phone1 );
//this is the database generated code
INSERT INTO Person
( id )
VALUES ( 1 )
INSERT INTO Phone
( number, id )
VALUES ( '123-456-7890', 2 )
INSERT INTO Phone
( number, id )
VALUES ( '321-654-0987', 3 )
INSERT INTO Person_Phone
( Person_id, phones_id )
VALUES ( 1, 2 )
INSERT INTO Person_Phone
( Person_id, phones_id )
VALUES ( 1, 3 )
DELETE FROM Person_Phone
WHERE Person_id = 1
INSERT INTO Person_Phone
( Person_id, phones_id )
VALUES ( 1, 3 )
DELETE FROM Phone
WHERE id = 2
Upvotes: 0
Views: 22
Reputation: 4475
in the link you provided all the associations have a cascade property defined, which mean they will follow cascadetype value in it whenever an event is triggered(insert, update, delete ...), so as they all have set to cascadeType.All which means whenever a parent get removed (e.g insert) all his child will be removed (e.g insert) too,
here is a good article if you want to understand more about this behavior
Upvotes: 1