0
votes

Primary Entity

public class CustomerAgreement implements Serializable {
    @OneToMany(mappedBy = "customerAgreement", orphanRemoval = true, fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
    private List<CustomerAgreementPeriod> agreementPeriods;

Child Entity:

public class CustomerAgreementPeriod  implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "CustomerAgreementPeriodSeq", sequenceName = "CUST_AGT_PERIOD_SEQ")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "CustomerAgreementPeriodSeq")
    @Column(name = "ID")
    private Long id;
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "CUSTID")
    private CustomerAgreement customerAgreement;

Main:

em.getTransaction().begin();
em.createQuery("delete from CustomerAgreement").executeUpdate();
em.getTransaction().commit();

Exception:

java.sql.SQLIntegrityConstraintViolationException: ORA-02292: integrity constraint (MASTERDATA.FK_J6TS8CGX06F90LLEMER78LFGG) violated - child record found
  • I have found in one of answers that Cascading does not work in delete JPQL..
  • To use .remove(entity) does not seems good approach?
  • To remove child using native queries and then parent query? (i have 5 childs)
  • Someone suggest to not to use cascade all with orphanremoval=true and i have changed to Cascade.Persist but then also same exception

Could anyone suggest how to remove entities in single query including child entities from 4-5 different tables having above given structure.

1

1 Answers

0
votes

No proper ways found so used Native queries like below:

public void deleteAllNative() {
        em.createNativeQuery("DELETE FROM CUST_AGT_ATT_TAB").executeUpdate();
        em.createNativeQuery("DELETE FROM CUST_AGT_CUSTOM_TAB").executeUpdate();
        em.createNativeQuery("DELETE FROM CUST_AGT_PERIODS_TAB").executeUpdate();
        em.createNativeQuery("DELETE FROM CUST_AGT_ROLES_TAB").executeUpdate();
        em.createNativeQuery("DELETE FROM CUST_AGREEMENT_TAB").executeUpdate();
        em.flush();
    }

I am sad :(