我正在使用 Spring Data Neo4j 建模一个非常简单的用例:我想要拥有通过友谊关系连接的人员。这是我的代码(这里省略了 getter 和 setter):
@NodeEntity
public class Person {
@GraphId
private Long id;
private String name;
private String password;
@RelatedTo(type = "FRIEND_OF", direction = Direction.BOTH)
private Set<Person> friends = new HashSet<Person>();
public Person() {};
public Person(String name, String password) {
this.name = name;
this.password = password;
}
public void befriend(Person person) {
this.friends.add(person);
}
Run Code Online (Sandbox Code Playgroud)
最终,我使用以下方法来使用我的人员:
@Transactional
private void populateTwoPersons() {
Person person1 = new Person("Alice", "pw1");
Person person2 = new Person("Bob", "pw2");
List<Person> persons = Arrays.asList(person1, …Run Code Online (Sandbox Code Playgroud)