cascade为all-delete-orphan和all区别
cascade为all-delete-orphan和all区别
Parent和Children为一对多关系。
Children child1 = new Children();
child1.setName("c1");
Children child2 = new Children();
child2.setName("c2");
Parent parent = new Parent("p1");
Set children = new HashSet(0);
parent.getChildren().add(child1);
parent.getChildren().add(child2);
child1.setParent(parent);
child2.setParent(parent);
session = HibernateServiceProvider.getSession();
tran = session.beginTransaction();
session.save(parent);
session.save(child1);
session.save(child2);
session.flush();
queryChildren(parent);
//queryParent(child1);
parent.getChildren().remove(child1);
session.flush();
//queryChildren(parent);
tran.commit();
session.close();
当<set name="children" inverse="true" cascade="all">时,通过parent.getChildren().remove(child1);只是使child1游离成为一个“孤儿”,并不能将child1持久化到数据库表中的记录也删除。但如果cascade="all-delete-orphan"(orphan为“孤儿”),则会将child1持久化到数据库表中的记录也删除掉。