您必须向 Record 添加一个指向相关 RecordNotification 的属性。然后您可以将其标记为级联删除。像这样:
@Entity
public class Record {
@Id
private int id;
@OneToOne(mappedBy="record", cascade={CascadeType.REMOVE})
private RecordNotification notification;
public Record(int id) {
this.id = id;
}
protected Record() {}
}
@Entity
public class RecordNotification {
@Id
private int id;
@OneToOne
private Record record;
public RecordNotification(int id, Record record) {
this.id = id;
this.record = record;
}
protected RecordNotification() {}
}
生成的 DDL 为:
create table Record (
id integer not null,
primary key (id)
);
create table RecordNotification (
id integer not null,
record_id integer,
primary key (id)
);
alter table RecordNotification
add constraint FKE88313FC6EE389C1
foreign key (record_id)
references Record;
我已经验证这可行:创建 aRecord
和 a RecordNotification
,提交,然后删除 theRecord
并注意到RecordNotification
消失了。
我使用了 Hibernate 4.1.4.Final。如果这对您不起作用,那么我怀疑 EclipseLink 中存在错误或不当行为。