Java 持久化不是节点属性的RelationshipEntity
在SDN4中,我希望保留一个Java 持久化不是节点属性的RelationshipEntity,java,neo4j,spring-data-neo4j,spring-data-neo4j-4,Java,Neo4j,Spring Data Neo4j,Spring Data Neo4j 4,在SDN4中,我希望保留一个@RelationshipEntity,它不是@NodeEntity的属性。 例如: @NodeEntity public class User{ Long id; } @RelationshipEntity(type="FOLLOWS") public class Follows{ @GraphId private Long relationshipId; @StartNode private User follower; @E
@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>{}
但这样做的时候,这种关系并没有持续下去
遗憾的是正如公认答案中所述,这(尚未)是无法做到的(SDN 4.0.0.版本)
解决方法1
可以使用图形位置
中的@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
来实现,这可能不是执行最有效的方法,但是@NodeEntity
不会影响域的任何@NodeEntity
,在加载和持久化实体时,您也不必处理深度因子
@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));
}
.....
目前,如果没有从参与节点实体引用关系实体,则无法直接持久化该关系实体。 您必须保存开始节点,并确保它具有对关系实体的引用
在关系实体如何持久化方面会有一些增强,但不会在下一个版本中出现。“会有一些增强…”你是说这会被修复吗?@Xipo你的意思是你的问题涉及一个bug,而不是一个功能请求吗?我认为这是一个bug,因为1。以前支持上述内容,现在不是2。当试图以这种方式保持关系时,不会抛出异常或错误(这根本不起作用)3。《参考(特别是迁移)指南》没有提到应该预期的内容。我错了吗?@Xipo它按照SDN 4的设计工作。SDN4期望您的对象模型与图形模型一致。同意这没有很好地记录,并且文档已经为下一版本更新。同时,更多地讨论对象model@Luanne我对这个问题进行了编辑,这样就不会暗示它涉及到一个bug。虽然
关系
是域的实体
,但它们不应该像任何其他实体
一样被持久化吗?另外,我们能否了解一下未来是否支持此功能?谢谢!!!:D
@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));
}
.....