Spring Data Neo4j:复杂关系不持久
Spring Data Neo4j: Complex relationships not persisting
同时在节点之间添加多个关系时,只会创建其中的一些。在下面的示例中,对 makeUser(...)
的调用仅填充了一些关系。
主要
@Transactional
void clearDatabase() {
session.execute("MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r");
}
void createPeople() {
Person mark = new Person("Mark");
mark.password = "mark123";
people.save(mark);
Organisation noxRentals = new Organisation("Nox Rentals");
organisations.save(noxRentals);
makeUser(noxRentals, mark, "Administrator", Right.ADMINISTRATE);
makeUser(noxRentals, richard, "Administrator", Right.ADMINISTRATE);
makeUser(classicVillas, mark, "Administrator", Right.ADMINISTRATE);
makeUser(classicVillas, richard, "Administrator", Right.ADMINISTRATE);
makeUser(classicVillas, charlotte, "Reservations", Right.LOGIN, Right.SEND_QUOTES);
}
@Transactional
void makeUser (Organisation organisation, Person person, String role, Right...rights) {
IsUser account = organisation.addUser(person, role);
account.addRights(rights);
organisations.save(organisation);
}
void run() {
clearDatabase();
createPeople();
}
结果(注意Nox没有关系):
Organisation.java
@NodeEntity
public class Organisation extends NitroBaseEntity {
@Relationship(type = "IsUser", direction = Relationship.INCOMING)
Set<IsUser> users = new HashSet<>();
public IsUser addUser(Person person, String role) {
IsUser user = new IsUser(person, this, role);
this.users.add(user);
return user;
}
}
Person.java
@NodeEntity
public class Person extends NitroBaseEntity {
@Property
String password;
@Relationship(type = "IsUser", direction = Relationship.OUTGOING)
Set<IsUser> users = new HashSet<>();
public Set<IsUser> getUserAccounts() {
return this.users;
}
}
IsUser.java
@RelationshipEntity
public class IsUser {
@GraphId
Long id;
@StartNode
public Person person;
@EndNode
public Organisation organisation;
@Property
public String role;
@Property
public Set<Right> rights = new HashSet<>();
public IsUser (Person person, Organisation organisation, String role) {
this.person = person;
this.organisation = organisation;
this.role = role;
}
}
少了两个东西-
- 类型也必须在
@RelationshipEntity
上指定,像这样 @RelationshipEntity(type = "IsUser")
- 在
Organisation.addUser()
中,也将 IsUser 添加到 Person,类似于 person.users.add(user);
。实体必须可以从两端导航。
同时在节点之间添加多个关系时,只会创建其中的一些。在下面的示例中,对 makeUser(...)
的调用仅填充了一些关系。
主要
@Transactional
void clearDatabase() {
session.execute("MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r");
}
void createPeople() {
Person mark = new Person("Mark");
mark.password = "mark123";
people.save(mark);
Organisation noxRentals = new Organisation("Nox Rentals");
organisations.save(noxRentals);
makeUser(noxRentals, mark, "Administrator", Right.ADMINISTRATE);
makeUser(noxRentals, richard, "Administrator", Right.ADMINISTRATE);
makeUser(classicVillas, mark, "Administrator", Right.ADMINISTRATE);
makeUser(classicVillas, richard, "Administrator", Right.ADMINISTRATE);
makeUser(classicVillas, charlotte, "Reservations", Right.LOGIN, Right.SEND_QUOTES);
}
@Transactional
void makeUser (Organisation organisation, Person person, String role, Right...rights) {
IsUser account = organisation.addUser(person, role);
account.addRights(rights);
organisations.save(organisation);
}
void run() {
clearDatabase();
createPeople();
}
结果(注意Nox没有关系):
Organisation.java
@NodeEntity
public class Organisation extends NitroBaseEntity {
@Relationship(type = "IsUser", direction = Relationship.INCOMING)
Set<IsUser> users = new HashSet<>();
public IsUser addUser(Person person, String role) {
IsUser user = new IsUser(person, this, role);
this.users.add(user);
return user;
}
}
Person.java
@NodeEntity
public class Person extends NitroBaseEntity {
@Property
String password;
@Relationship(type = "IsUser", direction = Relationship.OUTGOING)
Set<IsUser> users = new HashSet<>();
public Set<IsUser> getUserAccounts() {
return this.users;
}
}
IsUser.java
@RelationshipEntity
public class IsUser {
@GraphId
Long id;
@StartNode
public Person person;
@EndNode
public Organisation organisation;
@Property
public String role;
@Property
public Set<Right> rights = new HashSet<>();
public IsUser (Person person, Organisation organisation, String role) {
this.person = person;
this.organisation = organisation;
this.role = role;
}
}
少了两个东西-
- 类型也必须在
@RelationshipEntity
上指定,像这样@RelationshipEntity(type = "IsUser")
- 在
Organisation.addUser()
中,也将 IsUser 添加到 Person,类似于person.users.add(user);
。实体必须可以从两端导航。