我正在使用Spring-Data-Neo4j
创建示例。在此,执行CRUD操作,并且所有操作都成功运行。但是当我从实体获取关系集合时,它仅返回节点的graphId
,其他值为空。以下是我的代码。如果我做错了,请纠正我。
实体:
@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;
}
控制器:
@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";
}
在控制器中,当我获取此人时,该人获取成功,但我获取了朋友,它包含start_node
具有相同的人物对象,但end_node
包含具有graphId
的人物对象仅值,其他值为空。
答案 0 :(得分:0)
要解决此问题,我们需要在@Fetch
实体的起始节点和结束节点添加FriendsRelationship
注释,如下所示:
@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;
}
现在数据获取成功。