在 SDN4 中,我希望保留一个 @RelationshipEntity
,它不是 @NodeEntity
的属性。
示例:
@NodeEntity
public class User{
Long id;
}
@RelationshipEntity(type="FOLLOWS")
public class Follows{
@GraphId private Long relationshipId;
@StartNode private User follower;
@EndNode private User followee;
@Property private Date from;
public Follows(){}
public Follows(User u1, User u2){
this.follower = u1;
this.followee = u2;
}
}
@Repository
interface FollowsRepository extends GraphRepository<Follows>{}
然后像这样坚持 Follows
@Relationship
...
followsRepository.save(new Follows(user1, user2));
...
但是当这样做时,Relationship 并没有被持久化!!
遗憾的是如已接受的答案中所述,这(尚)不能完成(SDN 4.0.0.RELEASE)
解决方法 1
可以在 GraphRepositories
中使用 @Query
来持久化 @RelationshipEntities
。
@Query("Match (a:User), (b:User) WHERE id(a) = {0}
AND id(b) = {1} CREATE (a)-[r:FOLLOWS {date:{2}}]->(b) RETURN r ")
解决方法 2
这也可以通过将 Follows
视为 @NodeEntity
来完成,这可能不是最高效的做法但不会影响域的任何@NodeEntities
,也不影响服务层并且您在加载和持久化实体时不必弄乱深度因素
@NodeEntity
public class User{
Long id;
}
@NodeEntity
public class Follows{
private Long Id;
@Relationship(type="FOLLOWER")
private User follower;
@Relationship(type="FOLLOWEE")
private User followee;
private Date from;
public Follows(){}
public Follows(User u1, User u2){
this.follower = u1;
this.followee = u2;
}
}
....
//Placed in userService
Follows createFollowsRelationship(User u1, User u2){
return followsRepository.save(new Follows(user1, user2));
}
.....
最佳答案
目前,当关系实体未被参与节点实体引用时,您无法直接持久化关系实体。 您必须保存起始节点并确保它具有对关系实体的引用。
将对关系实体的持久化方式进行一些增强,但不会在下一个版本中进行。
关于java - 保留不是 NodeEntity 属性的 RelationshipEntity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33146401/