gpt4 book ai didi

java - 具有一组自身的 Hibernate 映射实体

转载 作者:搜寻专家 更新时间:2023-11-01 03:46:47 24 4
gpt4 key购买 nike

我的代码中有这个类

@Entity(name = "Point")
@Table(name = "Point")
public class Point extends com.lsikh.unlmaps.base.Entity<Integer>{
private Integer id;
...
...
private Set<Point> connections = new HashSet<Point >();

我需要将 Point 实体与该 Set 中的许多其他 Point 对象相关联并检索。

这是我在数据库中的表

  CREATE
TABLE Point(
id INTEGER UNIQUE AUTO_INCREMENT NOT NULL,
...
...
PRIMARY KEY (id )
);

CREATE
TABLE Connections(
idA INTEGER NOT NULL,
idB INTEGER NOT NULL,
FOREIGN KEY (idA ) REFERENCES Point (id),
FOREIGN KEY (idB ) REFERENCES Point(id)
);

这是执行此操作的正确 hibernate 符号集。我觉得可以

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "Connections", joinColumns = {
@JoinColumn(name = "id") }, inverseJoinColumns = {
@JoinColumn(name = "idA") })
public Set<Point> getConnections() {
return connections;
}

但是我在制作 map 时遇到了问题,我不确定。

最佳答案

根据 javadoc,joinColumns:

The foreign key columns of the join table which reference the primary table of the entity owning the association

因此假设 idA 是连接表中拥有实体的 ID,映射应该是:

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "Connections", joinColumns = {
@JoinColumn(name = "idA") }, inverseJoinColumns = {
@JoinColumn(name = "idB") })
public Set<Point> getConnections() {
return connections;
}

关于java - 具有一组自身的 Hibernate 映射实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48207610/

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