父子关系的实施

时间:2013-12-03 23:55:47

标签: java jpa database-design

我正在尝试实施以下内容 enter image description here

1个约会可以有0到多个子约会

subappointment有1个父约会

这是我尝试过的:

private Appointment parentAppointment;

@ManyToMany(cascade = CascadeType.ALL )
private List<Appointment> childrenAppointments;

我也尝试了一些JoinColumn注释,但它没有用。


编辑:

我尝试过Pace的解决方案 - &gt;

@ManyToOne
private Appointment parentAppointment;

@OneToMany(cascade = CascadeType.ALL, mappedBy="parentAppointment")
private List<Appointment> childrenAppointments;

现在我遇到了这个错误: ERROR: column "appointment_id" appears twice in primary key constraint

it(Play!Framework)尝试使用此脚本创建表:

create table appointment_appointment (
appointment_id                 integer not null,
appointment_id                 integer not null,
constraint pk_appointment_appointment primary key (appointment_id, appointment_id))
;

EDIT2:

修正:

    @ManyToOne
@JoinColumn(name = "parent_appointment")
private Appointment parentAppointment;

@OneToMany( mappedBy="parentAppointment", cascade = CascadeType.ALL)
private List<Appointment> childrenAppointments;

    public void setParentAppointment(Appointment ap){
    this.parentAppointment = ap;
}
public void addChildAppointment(Appointment ap){
    this.childrenAppointments.add(ap);
}

1 个答案:

答案 0 :(得分:1)

如果没有更多关于什么不起作用的细节,那么我无能为力。正确的注释,我是这样做的,这与标准的关系是:

@ManyToOne
private Appointment parentAppointment;

@OneToMany(cascade = CascadeType.ALL, mappedBy="parentAppointment")
private List<Appointment> childrenAppointments;

由于子约会只有1个父级,因此这将是一对多而不是多对多。在数据库中,上述设置将在约会表上为父约会创建单个列。对于没有父级的约会,此列可以为null。