Foreign Key conflict while entity deletion

Hi,

Below is the code where I need to set the “on Delete” option to “Cascade” for foreign key “subID”, I have gone with Delete policy and had set @OnDelete(DeletePolicy.CASCADE) but it wasnt working for me.
I had only set above “OnDelete” annotation , what else I am missing here?
Do I need to change/add anything in DDL?

I am getting below error:

image

Here is the “RestClassIns” code it is referring in above error:

public class RestClassIns extends BaseIntegerIdEntity {
    private static final long serialVersionUID = 941392292260840758L;

    @Lob
    @Column(name = "\"Restriction\"")
    protected String restriction;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "\"SubID\"")
    protected UnderwritersIns subID;

    @Column(name = "\"OrderNum\"")
    protected Integer orderNum;

    public void setRestriction(String restriction) {
        this.restriction = restriction;
    }

    public String getRestriction() {
        return restriction;
    }

    public void setSubID(UnderwritersIns subID) {
        this.subID = subID;
    }

    public UnderwritersIns getSubID() {
        return subID;
    }

    public void setOrderNum(Integer orderNum) {
        this.orderNum = orderNum;
    }

    public Integer getOrderNum() {
        return orderNum;
    }


}

**

And here is the main code where “SubID” is being used as foreign key

:**

public class UnderwritersIns extends BaseIntegerIdEntity {
    private static final long serialVersionUID = -3460063408051135600L;

    @Column(name = "\"UndFirst\"")
    protected String undFirst;

    @NotNull
    @Column(name = "BSIC", nullable = false)
    protected Boolean bsic = false;

    @NotNull
    @Column(name = "SAFM", nullable = false)
    protected Boolean safm = false;

    @NotNull
    @Column(name = "LIC", nullable = false)
    protected Boolean lic = false;

    @NotNull
    @Column(name = "SAIC", nullable = false)
    protected Boolean saic = false;

    @OneToMany(mappedBy = "subID")
    @OrderBy("ord ASC")
    protected List<GRSIndusIns> grsIndustries;

    @OneToMany(mappedBy = "subID")
    @OrderBy("orderNum ASC")
    protected List<RestClassIns> businessRestrictions;

    @OneToMany(mappedBy = "subID")
    @OrderBy("order ASC")
    protected List<LOBsIns> coverageRestrictions;

    @OneToMany(mappedBy = "subID")
    @OrderBy("type ASC, order ASC")
    protected List<CorpRTSUndIns> corporateRestrictions;

    @OneToMany(mappedBy = "subID")
    @OrderBy("order ASC")
    protected List<AuthorizedProgramsIns> authorizedProgramStructures;

Thanks,
Saurabh

Hi,
could you please clarify, what exactly you want to delete?
For example, if you want to delete all RestClassIns entity instances which refer to the deleted UnderwritersIns entity instance, you have to add the @OnDeleteInverse(DeletePolicy.CASCADE) to the foreign key “subID”

I need to delete all entity having foreign key “subID” . So when I delete any record it should delete all corresponding foreign key exist in any entity.
For ex: these entities in below code - GRSIndusIns, RestClassIns, LOBsIns, CorpRTSUndIns
And where this @OnDeleteInverse(DeletePolicy.CASCADE) need to added. In which class?
I mean where it mentioned @OneToMany or where it mentioned @ManyToOne in my case.

    @OneToMany(mappedBy = "subID")
    @OrderBy("ord ASC")
    protected List<GRSIndusIns> grsIndustries;

    @OneToMany(mappedBy = "subID")
    @OrderBy("orderNum ASC")
    protected List<RestClassIns> businessRestrictions;

    @OneToMany(mappedBy = "subID")
    @OrderBy("order ASC")
    protected List<LOBsIns> coverageRestrictions;

    @OneToMany(mappedBy = "subID")
    @OrderBy("type ASC, order ASC")
    protected List<CorpRTSUndIns> corporateRestrictions;

Let’s assume that you have a Foo entity instance and three Bar entity instances related to the Foo entity instance. Do you expect that Bar entities will be deleted along with the Foo entity, or only the Foo entity reference should be removed?

If you need the Bar entity instances to be deleted, then add @OnDeleteInverse(DeletePolicy.CASCADE) to the Bar class.

Otherwise, add the @OnDeleteInverse(DeletePolicy.UNLINK) to the Bar class.

ok, I expect to delete Bar entity along with Foo entity.
And I have added @OnDeleteInverse(DeletePolicy.CASCADE) in all the Bar entities but I am getting below error while deploy.

BUILD FAILED in 7s
8 actionable tasks: 1 executed, 7 up-to-date
[07:39:37.508] Task 'deploy, start' failed
org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_161\bin\java.exe'' finished with non-zero exit value 1

You should add this annotation to the foreign key “subID” attribute in the Bar class.

yes, Lets say for me Bar class is "RestClassIns"
So it should be like below right?

public class RestClassIns extends BaseIntegerIdEntity {
    private static final long serialVersionUID = 941392292260840758L;

    @Lob
    @Column(name = "\"Restriction\"")
    protected String restriction;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "\"SubID\"")
    @OnDeleteInverse(DeletePolicy.CASCADE)
    protected UnderwritersIns subID;

Yes, that’s right. Check, that you have been imported the annotation to the class as following:
import com.haulmont.cuba.core.entity.annotation.OnDeleteInverse;
import com.haulmont.cuba.core.global.DeletePolicy;

I am sorry still I am getting same exception:

SQLServerException: The DELETE statement conflicted with the REFERENCE constraint "FK_RestClassInsUW". The conflict occurred in database "LOA", table "dbo.RestClassIns", column 'SubID'.

Btw , I am using external SQL server for db , I hope thats not the cause.
Also Cuba Platform Version- 6.8.7