gpt4 book ai didi

java - Spring-Data-Neo4j:relationshipEntity 仅返回节点的 graphId。

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:40 24 4
gpt4 key购买 nike

我正在使用 Spring-Data-Neo4j 创建一个示例。在此执行CRUD操作,所有操作运行成功。但是当我从实体获取关系船集合时,它仅返回节点的 graphId ,其他值为 null。以下是我的代码。如果我做错了什么,请纠正我。

实体:

@NodeEntity
@ToString(callSuper=true, exclude={"movies"})
@EqualsAndHashCode(callSuper = true, exclude = {"name", "movies"})
public class Person extends BaseEntity{

@Getter @Setter
@Indexed(unique = true)
private Long id;
@Getter @Setter
private String name;
@Getter @Setter
@RelatedToVia(type = RelationshipTypes.FRIEND, elementClass = FriendsRelationship.class, direction = Direction.BOTH)
private Set<FriendsRelationship> friends;
}

@RelationshipEntity(type=RelationshipTypes.FRIEND)
public class FriendsRelationship extends BaseEntity{

@StartNode
@Getter @Setter
private Person person;
@EndNode
@Getter @Setter
private Person friend;
@Getter @Setter
private String friendsType;
}

创建关系:

public FriendsRelationship createRelationshipBetweenPersons(Person person, Person friend, 
Class<FriendsRelationship> relationshipEntity, String friendshipType) {
FriendsRelationship relationship = neo4jTemplate.createRelationshipBetween(person, friend, relationshipEntity, friendshipType, true);
neo4jTemplate.save(relationship);
return relationship;
}

Controller :

@RequestMapping(value="find-person-by-id", method=RequestMethod.POST)
public String findPersonById(long id, Model model) {
Person person = personService.findPersonByProperty("id", id);
model.addAttribute("actor", person);
model.addAttribute("personFriends", person.getFriends());
return "person/view-person-detail";
}

在 Controller 中,当我获取人员时,人员获取成功,但我获取 friend 时,它包含具有相同人员对象的 start_node ,但 end_node 仅包含具有 graphId 值的人员对象,其他值为 null。

最佳答案

为了解决这个问题,我们需要在 FriendsRelationship 实体的起始节点和结束节点添加 @Fetch 注解,如下所示:

@RelationshipEntity(type=RelationshipTypes.FRIEND)
public class FriendsRelationship extends BaseEntity{

@Fetch @StartNode
@Getter @Setter
private Person person;
@Fetch @EndNode
@Getter @Setter
private Person friend;
@Getter @Setter
private String friendsType;
}

现在数据获取成功了。

关于java - Spring-Data-Neo4j:relationshipEntity 仅返回节点的 graphId。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27341342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com