Neo4j spring - 在相同类型的实体之间创建双向关系
Neo4j spring - Create a 2 way relationship between same type of entities
我们想要的关系类型如下
创建 (jack:Person {name:"Jack"})-[:KNOWS]->(jill:Person {name:"Jill"})
与杰克,吉尔
创建(杰克)<-[:知道]-(吉尔);
下面我们如何建模?
@Node
public class User {
@Id
@GeneratedValue
private UUID id;
private String name;
private String phoneNumber;
private String email;
private boolean isActive;
@JsonIgnore
@Relationship(type = "Knows", direction = Relationship.Direction.OUTGOING)
private Set<Knows> knows;
@JsonIgnore
@Relationship(type = "Knows", direction = Relationship.Direction.INCOMING)
private Set<Knows> knownBy;
public User() {
}
public void knows(Knows to) {
if (knows == null) {
knows = new HashSet<>();
}
knows.add(to);
}
public void knownBy(Knows from) {
if (knownBy == null) {
knownBy = new HashSet<>();
}
knownBy.add(from);
}
}
@RelationshipProperties
public class Knows {
private String as;
@CreatedDate
private Instant createdAt;
@JsonIgnore
@TargetNode
private User User;
}
保存用户的代码片段
User user1 = userRepository.findById(id1);
User user2 = userRepository.findById(id2);
Knows knows = new Knows("as", Instant.now(), user1);
Knows knownBy = new Knows("as", Instant.now(), user2);
user2.knows(knows);
user1.knownBy(knownBy);
userRepository.save(user2);
有了这个,我们在保存用户实体的同时得到了 Whosebug。
我们想知道这是正确的建模方法还是有更好的方法?
首先要注意的是:在将双向关系映射到数据库时,SDN 中存在一个错误。
要跟踪的票是 https://jira.spring.io/browse/DATAGRAPH-1469
下个版本就很确定了
给你场景我会建议清理关系并只创建 OUTGOING
关系,如
@Node
public class User {
// ... id etc.
@Relationship("KNOWS")
private List<Knows> knows;
}
在设置值时,两边都需要填充:
user1.knows(user2);
user2.knows(user1);
鉴于该错误在 SDN 6.0.3 发布之前一直存在,我建议要么单向工作,只连接一个方向的 Users
或 link Users
直接。
因此,您将无权访问关系中的属性。
@Node
public class User {
// ... id etc.
@Relationship("KNOWS")
private List<User> knows;
}
我们想要的关系类型如下 创建 (jack:Person {name:"Jack"})-[:KNOWS]->(jill:Person {name:"Jill"}) 与杰克,吉尔 创建(杰克)<-[:知道]-(吉尔);
下面我们如何建模?
@Node
public class User {
@Id
@GeneratedValue
private UUID id;
private String name;
private String phoneNumber;
private String email;
private boolean isActive;
@JsonIgnore
@Relationship(type = "Knows", direction = Relationship.Direction.OUTGOING)
private Set<Knows> knows;
@JsonIgnore
@Relationship(type = "Knows", direction = Relationship.Direction.INCOMING)
private Set<Knows> knownBy;
public User() {
}
public void knows(Knows to) {
if (knows == null) {
knows = new HashSet<>();
}
knows.add(to);
}
public void knownBy(Knows from) {
if (knownBy == null) {
knownBy = new HashSet<>();
}
knownBy.add(from);
}
}
@RelationshipProperties
public class Knows {
private String as;
@CreatedDate
private Instant createdAt;
@JsonIgnore
@TargetNode
private User User;
}
保存用户的代码片段
User user1 = userRepository.findById(id1);
User user2 = userRepository.findById(id2);
Knows knows = new Knows("as", Instant.now(), user1);
Knows knownBy = new Knows("as", Instant.now(), user2);
user2.knows(knows);
user1.knownBy(knownBy);
userRepository.save(user2);
有了这个,我们在保存用户实体的同时得到了 Whosebug。 我们想知道这是正确的建模方法还是有更好的方法?
首先要注意的是:在将双向关系映射到数据库时,SDN 中存在一个错误。 要跟踪的票是 https://jira.spring.io/browse/DATAGRAPH-1469 下个版本就很确定了
给你场景我会建议清理关系并只创建 OUTGOING
关系,如
@Node
public class User {
// ... id etc.
@Relationship("KNOWS")
private List<Knows> knows;
}
在设置值时,两边都需要填充:
user1.knows(user2);
user2.knows(user1);
鉴于该错误在 SDN 6.0.3 发布之前一直存在,我建议要么单向工作,只连接一个方向的 Users
或 link Users
直接。
因此,您将无权访问关系中的属性。
@Node
public class User {
// ... id etc.
@Relationship("KNOWS")
private List<User> knows;
}