如何解决Spring bot 2.4 neo4j 6 两个节点是否有关系?
如果我有 2 个对象,它们属于一个用 @Node 注释的类(人),我怎样才能最好地找出它们是否有关系?(Friends_with)。问题是,在 Neo4j 中,只有一个对象应该“拥有”这种关系。所以只有其中一个拥有友谊。 (可以只过滤他们的两个列表来查看,但这似乎是不必要的,为什么我不能只查询友谊集合)
对我来说最合乎逻辑的是通过 personStart(为友谊的所有者保存的 id)和 personEnd(用@TargetNode 注释的友谊目标)查询友谊存储库。 那似乎行不通。通过友谊存储库(仅使用 personStart)获取 Friendship 对象,不包含 personEnd。它是空的。有趣的是,从 Person 的列表“friendsWith”中获取的好友对象确实包含与从好友库中获取的对象具有相同 id 的相同好友对象,但这里它确实包含 personEnd 对象。
@RelationshipProperties
@Data
public class FriendShip
{
@Id
@GeneratedValue(generatorClass = UUIDStringGenerator.class)
private String id;
@TargetNode
private Person personEnd;
private String personStart;
private String type = "normal";// best
public String getClassName()
{
return this.getClass().getName();
}
}
public class Person{
@Id
@GeneratedValue(generatorClass = UUIDStringGenerator.class)
private String id;
@Relationship(type = "Friend_With")
List<FriendShip> friendsWith;
更新: 正如我所见,我可能对 sdn6 有错误的想法。我什至不应该用@RelationshipProperties 保存类/它可能不应该像以前在 sdn5 中那样有存储库。也许我只是将对象保存在 @Node 的关系字段中,并通过 Person 存储库中的 @Query 注释基于该关系“Friend_with”在那里搜索。这确实有效,不知道那是否是正确的方法。 所以一个可能的解决方案是:如果这返回 null 那么他们之间就没有友谊:
@Query("MATCH(:Person{id:$friendOwnerID})-[:Friend_With]->
(p:Person{id:$friendId}) return p")
public Person findByFriendsWith(@Param("friendOwnerID") String friendOwnerId,@Param("friendId") String friendId);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。