当我们使用createRelationshipBetween时,如何在Spring数据neo4j中为关系添加属性

时间:2013-03-26 12:37:55

标签: spring neo4j spring-data-neo4j

例如,我想在用户A和用户B之间建立关系,他们有RelationshipEntity名为MakeFriend,我使用下面的代码,但我也想在关系实体中设置一些属性值,如role = 10 .... .....

userRepository.createRelationshipBetween(startUser, endUser, MakeFriend.class, RelTypes.FRIEND.name());

@RelationshipEntity
public class MakeFriend {
    @GraphId
    private Long id;
    private String role;
    @StartNode
    private UserEntity startUser;
    @EndNode
    private UserEntity endUser


@NodeEntity
public class UserEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    public static final String FRIEND = "FRIEND";
    public static final String JOYNED = "JOYNED";
    @GraphId
    private Long id;
    @Indexed(unique = true)
    private Long userId;
    private String email;

1 个答案:

答案 0 :(得分:3)

您可以将以下内容添加到UserEntity类:

@RelatedToVia(type = RelTypes.FRIEND, direction = Direction.BOTH)
private MakeFriend friend;

friend.setRole("yourRole");

当您使用高级映射模式时,另一种方法是使用NodeBacked.relateTo()方法之一。然后将该属性添加到返回的Relationship。

第三种方法是使用Neo4jTemplate.createRelationshipBetween()方法并提供属性(例如角色)作为最终参数。