0

I have two entity classes which have a OneToOne Bidirectional connection along with @CascadeOnDelete Tag. The DDLs generated from them do not have the on delete cascade.

@Entity(name = "Test")
@CascadeOnDelete
public class Test {

    @Id
    @GeneratedValue
    private long id;

    public long getId() {
        return id;
    }

    public void setId(long newId) {
        this.id = newId;
    }
    
    @OneToOne(cascade = { CascadeType.ALL }, orphanRemoval=true, mappedBy = "test")
    @CascadeOnDelete
    Test1 test1 = null;

    public Test1 getTest() {
        return test1;
    }

    public void setTest(Test1 test) {
        this.test1 = test;
    }
}


@Entity(name = "Test1")
@CascadeOnDelete
public class Test1 {

    @Id
    @GeneratedValue
    private long id;

    public long getId() {
        return id;
    }

    public void setId(long newId) {
        this.id = newId;
    }
    
    @OneToOne
    @JoinColumn(name = "test1")
    @CascadeOnDelete
    Test test = null;
    
    public Test getTest() {
        return test;
    }

    public void setTest(Test test) {
        this.test = test;
    }

    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }

    String s;
}

Generates DDLs as

CREATE TABLE TEST (ID BIGINT NOT NULL, PRIMARY KEY (ID));
CREATE TABLE TEST1 (ID BIGINT NOT NULL, S VARCHAR(255), test1 BIGINT, PRIMARY KEY (ID));
ALTER TABLE TEST1 ADD CONSTRAINT FK_TEST1_test1 FOREIGN KEY (test1) REFERENCES TEST (ID);
CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(15), PRIMARY KEY (SEQ_NAME));
INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 0);

Why is there no ON DELETE CASCADE; at the end of Alter table? For OneToMany, it works without problem.

Similar(yet older version) question(also unanswered): Eclipselink - @CascadeOnDelete doesn't work on @OneToOne relations

SSharma
  • 951
  • 6
  • 15
  • What happens if you remove `@CascadeOnDelete` from `test1` side? And why do you have `@JoinColumn(name = "test1")`? If you just remove that will there be column `test_id` that would be more clear? – pirho Jun 24 '20 at 07:10
  • If I remove that i will get `test_id`, correct, but I need this foreign key to be named as test1. And removing `@CascadeOnDelete` one either side has no result change. – SSharma Jun 24 '20 at 07:28
  • Why do you have the annotation on the class? The examples that have that are using it to clear a secondary table - it shouldn't be there if this entity has only one table. As James mentioned, it is a bug, but doesn't look like anyone filed it - you might try the latest version to be sure though. – Chris Jun 25 '20 at 00:42
  • I have the very latest version 2.7.7 https://www.eclipse.org/eclipselink/releases/ – SSharma Jun 25 '20 at 11:02

0 Answers0