Hibernate – If Cascade.Persist and Cascade.Remove are used together in the parent entity in the HiberNet, Cascade deletes don’t work.

I have a table relationship definition as shown below, I am trying to cascade persistence and delete from Entity2 (table 2) means deleting a record from table2 should delete the entry in table 3, and Saving the data in table2 also keeps the related data in table3.

oneToMany oneToMany

Table 1 (Entity 1) (parent)—— –>Table 2(Entity 2)(Child to Table1)———>Table 3(Child to Table2)(Entity3)

oneToMany oneToMany

Table 1 (parents)——–> Table 4 (Entity 4) (Child to Table1)———> Table 5 (Child to Table 4) (Entity 5)

Table1 is table2 and table4 Common parent of

Problem statement:

1) If I use CascadeType.REMOVE in Entity1 and use {CascadeType.PERSIST,CascadeType in its child nodes (Entity2 and Entity4) .REMOVE), then save the record in table2 and delete the record from table2 will be cascaded to its child Entity3 (table3), but save the record to table1 does not cascade from its child (Table2 and table4), because I am not in Use cascade.persist in Entity1

2) If I use (CascadeType.PERSIST, CascadeType.REMOVE) in Entity1 and use (CascadeType.PERSIST, CascadeType.REMOVE) in its child nodes (Entity2 and Entity4) }, the record in the saved table2 will be cascaded to its child Entity3 (table3) but it is not deleted when trying to delete the record from table2, and it is not cascaded to its child Entity3 (table3), no exception… (The delete query was not executed from the log, but in the first case, I can see the delete query being executed)

Can you help me find out where I am wrong…. Why CascadeType .PERSIST added to Entity1 (Table1) will Does le2 delete records affect?

Sample code below:

Form 1{

....

@OneToMany (cascade={CascadeType.PERSIST,CascadeType.REMOVE,mappedBy=....})
public Set getTable2() {
return table2;
}

< p>}

Table 2 {

....

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(....)
public Table1 getTable1() {
return table1
}


@OneToMany(cascade={CascadeType .PERSIST,CascadeType.REMOVE,mappedBy=....})
public Set getTable3() {
return table3;
}

}

Table 3{

....

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn( ....)
public Table2 getTable2() {
return table2;

}

}

This article seems to fully describe your problem. If what the author says is still true (the article is from 2010), the problem lies in JPA and There is a mismatch between the behavior expected by Hibernate when persisting. If you are using an older Hibernate implementation, I first try to upgrade to a newer version because I have never seen this problem in recent versions. If you can’t Upgrade (for example, you are limited to using the Hibernate version provided by the application server), or the upgrade does not work, then Replacing JPA cascading annotations with Hibernate cascading annotations may solve your problem. Using Hibernate annotations is not a good solution, because now your code is not implementation-independent, but it is feasible if it works.

What version of Hibernate / JPA / app server are you using?

I have a table relationship definition as shown below, I am trying to cascade persistence and delete from Entity2 (table 2) means deleting records from table2 should delete table3 And save the data in table2 and also keep the related data in table3.

oneToMany oneToMany

Table 1 (Entity 1)( Father)————>Table 2(Child to Table1)———>Table 3(Child to Table2)(Entity3)

oneToMany oneToMany

Table 1 (parents)——-> Table 4 (Entity 4) (Child to Table1)——-> Table 5 (Child to Table 4) (Entity 5)

Table1 Is the common parent of table2 and table4

Problem statement:

1) If I use CascadeType.REMOVE in Entity1 and use {CascadeType in its child nodes (Entity2 and Entity4) .PERSIST,CascadeType.REMOVE), then save the record in table2 and delete the record from table2 will be cascaded to its child Entity3 (table3), but the record saved to table1 is not cascaded from its child (Table2 and table4), Because I did not use cascade.persist in Entity1

2) If I use (CascadeType.PERSIST, CascadeType.REMOVE) in Entity1 and use (CascadeType.PERSIST in its child nodes (Entity2 and Entity4) ,CascadeType.REMOVE), the record in the saved table2 will be cascaded to its child Entity3 (table3) but it is not deleted when trying to delete the record from table2, and it is not cascaded to its child Entity3 (table3), also No exceptions… (No delete query was executed from the log, but in the first case, I can see the delete query being executed)

Can you help me find out where I am wrong… .Why adding CascadeType.PERSIST to Entity1(Table1) will affect the deletion of records from table2?

Sample code below:

Form 1{

....

@OneToMany (cascade={CascadeType.PERSIST,CascadeType.REMOVE,mappedBy=....})
public Set getTable2() {
return table2;
}

< p>}

Table 2 {

....

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(....)
public Table1 getTable1() {
return table1
}


@OneToMany(cascade={CascadeType .PERSIST,CascadeType.REMOVE,mappedBy=....})
public Set getTable3() {
return table3;
}

}

Table 3{

....

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn( ....)
public Table2 getTable2() {
return table2;

}

}

This article seems to fully describe your problem. If what the author says is still true (the article is from 2010), the problem is that there is a gap between the behavior expected by JPA and Hibernate when persisting Does not match. If you are using an older Hibernate implementation, I first try to upgrade to a newer version because I have never seen this problem in recent versions. If you can’t upgrade (e.g. you are limited to using the application server provided Hibernate version), or the upgrade does not work, then replacing JPA cascade annotations with Hibernate cascade annotations may solve your problem. Using Hibernate annotations is not a good solution, because now Your code is not implementation-independent, but it works if it works.

What version of Hibernate/JPA/app server are you using?

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4464 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.