Reputation: 1215
My entities. ValidationStep has a one to one relationship with documentDetail and documentDetail has a oneToMany relationship of documentValidations
ValidationStep
@Entity
public class ValidationStep {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name= "automation_information_aId", referencedColumnName= "aId")
private AutomationInformation automationInformation;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@OneToOne(cascade = { CascadeType.ALL }, orphanRemoval = true,fetch=FetchType.LAZY)
private DocumentDetail documentDetail;
}
DocumentDetail
@Entity
public class DocumentDetail {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "documentDetail_id")
private List<DocumentValidation> documentValidationList
DocumentValidation
@Entity
public class DocumentValidation implements Comparable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
My delete query
@Modifying
@Query(value = "DELETE FROM ValidationStep validationStep WHERE validationStep.automationInformation.id = :aId AND isDraft is true")
void deleteAllDraftsByAutomationInformationaID(@Param("aId") Long aId);
The parent ValidationStep is removed, however the docDetail and documentValidations are still in the database.
Upvotes: 0
Views: 989
Reputation: 691625
From the JPA specifications (paragraph 4.10: Bulk Update and Delete Operations ):
A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to related entities.
The remove cascade only works if you execute use EntityManager.remove()
to delete your entity. Not if you execute a delete query.
Upvotes: 1