我的代码中有这个类
@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/