简体   繁体   中英

Spring-Data-Neo4j: relationshipEntity return only graphId for nodes.

I am creating an example using Spring-Data-Neo4j . In this, perform CRUD operations, and all operation run successfully. But when i fetch the relationa ship collection from entity, it return only graphId for nodes, and other values are null. Following is my code. If i do something wrong, please correct me.

Entities:

@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;
}

Create Relationship:

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";
}

In controller, when i fetch the person, the person fetch successfully, but i fetch the friends, it contain start_node with same person object, but end_node contain person object with graphId value only, others values are null.

For solve this problem, we need to add @Fetch annotation at start-node and end-node in FriendsRelationship entity, like as below:

@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;
}

Now the data fetch successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM