gpt4 book ai didi

java - 父子关系实现

转载 作者:行者123 更新时间:2023-12-01 13:46:08 28 4
gpt4 key购买 nike

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

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

子约会有 1 个父约会

这是我尝试过的:

private Appointment parentAppointment;

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

我也尝试过一些 JoinColumn 注释,但没有成功。

<小时/>

编辑:

我尝试过佩斯的解决方案 ->

@ManyToOne
private Appointment parentAppointment;

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

现在我遇到了这个错误:错误:列“appointment_id”在主键约束中出现两次

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))
;
<小时/>

编辑2:

修复者:

    @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);
}

最佳答案

如果没有更多有关无效的详细信息,我无能为力。如果我这样做并且具有标准关系,正确的注释将是:

@ManyToOne
private Appointment parentAppointment;

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

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

关于java - 父子关系实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20364192/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com