在 Spring Data Neo4J 中保存关系的正确方法是什么?
What is the correct way to save relationships in Spring Data Neo4J?
使用 Spring Boot 和 Neo4J,我创建了两个 @NodeEntity
。他们是 User
和 Right
。在我的模型中,当您在用户和权限之间创建关系时,我将其称为 Privilege
我无法保存 @RelationshipEntity
、Privilege
(来自 @NodeEntity
或 RelationshipEntity
)。
示例代码
User.java(由接口 UserRepository 扩展 GraphRepository 支持)
@NodeEntity
public class User {
@Autowired Neo4jTemplate template;
@GraphId Long id;
String fullName;
@Indexed(unique=true) String email;
@Fetch @RelatedTo(type="HAS_RIGHT")
Set<Right> rights;
public void addRight(Right r) {
Privilege p = new Privilege (this, r)
template.save(p) // This always throws a NullPointerException
}
/*** Getters and Setters ***/
}
Right.java(由接口 RightRepository 扩展 GraphRepository 支持)
@NodeEntity
public class Right {
@GraphId Long id;
String name;
/*** Getters and Setters ***/
}
Privilege.java(不受存储库接口支持)- 问题 CLASS
@RelationshipEntity(type="HAS_RIGHT")
public class Privilege {
@Autowired
Neo4jTemplate template; // This is always null
@GraphId Long id;
@StartNode User user;
@EndNode Right right;
public Privilege() {}
public Privilege(User user, Right right) {
this.user = user;
this.right = right;
}
public void save() {
template.save(this); // Always throws a NullPointerException
}
}
在我的测试用例中,我可以调用(这有效):
User user = userRepository.findByEmail("admin@noxgroup.co.za");
Right adminRight = rightRepository.findByName("ADMINISTRATOR");
Privilege adminPrivilege = new Privilege(user, adminRight);
template.save(adminPrivilege);
但我更愿意打电话(这不起作用):
User user = userRepository.findByEmail("admin@noxgroup.co.za");
User.addRight (rightRepository.findByName("ADMINISTRATOR"));
但我也无法从任一 NodeEntities 中访问模板。
您可以使用 Neo4jTemplate 或 Neo4jOperations 创建关系。
改成构造函数注入。
@Autowired
public Constructor(Neo4jOperations operations) {
this.neo4jOperations = operations;
}
通过调用存储库获得两个节点后,执行如下操作:
Relation relation = neo4jOperations.createRelationshipBetween(user, right, Relationship.class, "RELATION_NAME", true);
neo4jOperations.save(relation);
我的错误是我手动实例化了 class,因此它不是 spring 管理的。将 @Component 注释添加到 class 并要求 Spring 给我 class 解决了问题。
@Component // This is the line that saved the day!
@RelationshipEntity(type="HAS_RIGHT")
public class Privilege {
@Autowired
Neo4jOperations operations;
@GraphId Long id;
@StartNode User user;
@EndNode Right right;
public Privilege() {
;;
}
public void createRelationship(User user, Right right) {
this.user = user;
this.right = right;
this.save();
}
/*** Getters and Setters ***/
}
然后实例化它:
...
@Autowired ApplicationContext applicationContext;
...
Privilege privilege = applicationContext.getBean(Privilege.class);
privilege.createRelationship(user, ADMINISTRATOR);
使用 Spring Boot 和 Neo4J,我创建了两个 @NodeEntity
。他们是 User
和 Right
。在我的模型中,当您在用户和权限之间创建关系时,我将其称为 Privilege
我无法保存 @RelationshipEntity
、Privilege
(来自 @NodeEntity
或 RelationshipEntity
)。
示例代码
User.java(由接口 UserRepository 扩展 GraphRepository 支持)
@NodeEntity
public class User {
@Autowired Neo4jTemplate template;
@GraphId Long id;
String fullName;
@Indexed(unique=true) String email;
@Fetch @RelatedTo(type="HAS_RIGHT")
Set<Right> rights;
public void addRight(Right r) {
Privilege p = new Privilege (this, r)
template.save(p) // This always throws a NullPointerException
}
/*** Getters and Setters ***/
}
Right.java(由接口 RightRepository 扩展 GraphRepository 支持)
@NodeEntity
public class Right {
@GraphId Long id;
String name;
/*** Getters and Setters ***/
}
Privilege.java(不受存储库接口支持)- 问题 CLASS
@RelationshipEntity(type="HAS_RIGHT")
public class Privilege {
@Autowired
Neo4jTemplate template; // This is always null
@GraphId Long id;
@StartNode User user;
@EndNode Right right;
public Privilege() {}
public Privilege(User user, Right right) {
this.user = user;
this.right = right;
}
public void save() {
template.save(this); // Always throws a NullPointerException
}
}
在我的测试用例中,我可以调用(这有效):
User user = userRepository.findByEmail("admin@noxgroup.co.za");
Right adminRight = rightRepository.findByName("ADMINISTRATOR");
Privilege adminPrivilege = new Privilege(user, adminRight);
template.save(adminPrivilege);
但我更愿意打电话(这不起作用):
User user = userRepository.findByEmail("admin@noxgroup.co.za");
User.addRight (rightRepository.findByName("ADMINISTRATOR"));
但我也无法从任一 NodeEntities 中访问模板。
您可以使用 Neo4jTemplate 或 Neo4jOperations 创建关系。
改成构造函数注入。
@Autowired
public Constructor(Neo4jOperations operations) {
this.neo4jOperations = operations;
}
通过调用存储库获得两个节点后,执行如下操作:
Relation relation = neo4jOperations.createRelationshipBetween(user, right, Relationship.class, "RELATION_NAME", true);
neo4jOperations.save(relation);
我的错误是我手动实例化了 class,因此它不是 spring 管理的。将 @Component 注释添加到 class 并要求 Spring 给我 class 解决了问题。
@Component // This is the line that saved the day!
@RelationshipEntity(type="HAS_RIGHT")
public class Privilege {
@Autowired
Neo4jOperations operations;
@GraphId Long id;
@StartNode User user;
@EndNode Right right;
public Privilege() {
;;
}
public void createRelationship(User user, Right right) {
this.user = user;
this.right = right;
this.save();
}
/*** Getters and Setters ***/
}
然后实例化它:
...
@Autowired ApplicationContext applicationContext;
...
Privilege privilege = applicationContext.getBean(Privilege.class);
privilege.createRelationship(user, ADMINISTRATOR);