gpt4 book ai didi

java - OneToMany 主键列错误

转载 作者:行者123 更新时间:2023-11-30 05:46:54 26 4
gpt4 key购买 nike

我在应用程序中新创建的表有问题。我想在仪表板 ID 上设置主键,但我遇到了关系问题。为什么我得到这个?我有 OneToMany、一个仪表板、许多组。我应该怎么办?它出什么问题了?在 Dashboard_groups 表中,我将 groups_id 作为 PK,并且我想要仪表板 ID。

@ManyToOne(targetEntity = Dashboard.class, fetch = FetchType.EAGER)
@JoinTable(name = "dashboard_groups", joinColumns = {@JoinColumn(name = "groups_id")},
inverseJoinColumns = {@JoinColumn(name = "dashboard_id", referencedColumnName = "id")})
@JsonBackReference(value = "group- dashboard")
private Dashboard dashboard;
@OneToMany(mappedBy = "dashboard", cascade = CascadeType.ALL)
@JsonManagedReference(value = "groups- dashboard")
private List<Group> groups= new ArrayList<>();


最佳答案

最简单的方法是不使用连接表。只需在中使用外键即可。

@Entity
class Dashboard {

@OneToMany(mappedBy = "dashboard", cascade = CascadeType.ALL)
private List<Group> groups;

}

@Entity
class Group{

@ManyToOne(fetch = FetchType.LAZY)
private Dashboard dashboard;

}

如果您想使用连接表,则不应使用mappedBy

@Entity
class Dashboard {

@OneToMany(cascade = CascadeType.ALL)
private List<Group> groups;

}

@Entity
class Group{


}

关于java - OneToMany 主键列错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54671798/

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