简体   繁体   中英

JPA CascadeType.ALL not deleting child

I have 2 classes in java with relation parent-child and I have a problem with the delete child. when I delete a child the function return true but when I check database nothing happens, the child still not deleted.

this is my class parent : Engagement.java

@OneToMany(fetch=FetchType.EAGER, mappedBy="parent", cascade=CascadeType.ALL)
 private Collection<Sub_Engagement> subs_engs;

this is my class child: Sub_Engagement.java

@ManyToOne
 @JoinColumn(name="parent")
 private Engagement parent;

with this code POST/GET/PATCH work fine but DELETE not working.

I tried a solution like that:

@OneToMany(fetch=FetchType.EAGER, mappedBy="parent", orphanRemoval = true, cascade = { CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH, CascadeType.REMOVE})
 private Collection<Sub_Engagement> subs_engs;

and DELETE works but PATCH/PUT not working when I try update a child. Thanks in advance :)

Andronicus had it right, that really should work. It's weird that you have an either-or situation with you updates and deletes.

Double check your persistence.xml. Is there something that overrides the annotations?

I also recommend to enable tracing and check the sql queries actually executed. Is the delete statement logged? Is your transaction actually committed?

Can we look at the code you are using for removing the entities? Are you using entitymanager's remove() method or executing some custom jpql?

Also, are you using a CMT (for example EJBs) or are you handling the transactions yourself via JTA?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM