Hibernate entity manager version:3.3.2.GA
I have the following entities:
Code:
@Entity
public class Parent implements Serializable {
@Id
public long id;
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
Set<Child> children;
}
@Entity
public class Child implements Serializable {
@Id
long id;
@ManyToOne
Parent parent;
}
and execute the following code
Code:
Parent parent = em.getReference(Parent.class, 1L);
em.remove(parent);
em.flush();
it generates SQL queries:
Code:
Hibernate: select parent0_.id as id35_0_ from Parent parent0_ where parent0_.id=?
Hibernate: select children0_.parent_id as parent2_1_, children0_.id as id1_, children0_.id as id20_0_, children0_.parent_id as parent2_20_0_ from Child children0_ where children0_.parent_id=?
Hibernate: delete from Child where id=?
Hibernate: delete from Child where id=?
Hibernate: delete from Child where id=?
Hibernate: delete from Child where id=?
Hibernate: delete from Parent where id=?
Why select queries are needed and why 4
Code:
delete from Child where id=?
instead of 1
Code:
delete from Child where parent_id=?