I have a table relation defined as show below and I am trying to cascade persistance and delete from Entity2(Table2) mean delete recor from table2 should remove the entry in table3 and saving data in table2 persist the related data in table3 as well.
oneToMany oneToMany
Table1(Entity1) (parent) --------> Table2(Entity2) (Child to Table1) ---------> Table3(Child to Table2)(Entity3)
oneToMany oneToMany
Table1 (parent) --------> Table4(Entity4) (Child to Table1) ---------> Table5(Child to Table4)(Entity5)
The Table1 is common parent to table2 and table4
Problem statement:
1) If I use CascadeType.REMOVE in Entity1 and {CascadeType.PERSIST,CascadeType.REMOVE} in its children (Entity2 and Entity4) then saving the record in table2 and deleting the record from table2 is getting cascaded to its child Entity3 (table3), but saving record to table1 is not getting cascade from to its children (Table2 and table4) as I am not using the cascade.persist in Entity1
2) If I use {CascadeType.PERSIST, CascadeType.REMOVE} in Entity1 and {CascadeType.PERSIST,CascadeType.REMOVE} in its children (Entity2 and Entity4) then saving the record in table2 is getting cascaded to its child Entity3 (table3) but on trying to delete the record from table2 is not getting deleted and also it is not getting cascaded to its child Entity3 (table3) and there is no exception either..... (From the log there is no delete query being executed, but in first case I can see the delete query being executed)
Could you help me out to find where I am going wrong.... Why adding CascadeType.PERSIST to Entity1 (Table1) will have impact on the deleting the record from table2?
Below Sample code :
Table1{
....
@OneToMany(cascade={CascadeType.PERSIST,CascadeType.REMOVE,mappedBy=....})
public Set<Table2> getTable2() {
return table2;
}
}
Table2{
....
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(....)
public Table1 getTable1() {
return table1
}
@OneToMany(cascade={CascadeType.PERSIST,CascadeType.REMOVE,mappedBy=....})
public Set<Table3> getTable3() {
return table3;
}
}
Table3{
....
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(....)
public Table2 getTable2() {
return table2;
}
}