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