Armen Arzumanyan
Armen Arzumanyan

Reputation: 2043

How to add properties to the relationship in Spring data neo4j when we use createRelationshipBetween

For example I want to make relationship between User A and User B and they have RelationshipEntity named MakeFriend, I am used code below, but I am also want to set in relation entity some property values like 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;

Upvotes: 0

Views: 2253

Answers (1)

tstorms
tstorms

Reputation: 5001

You could could add the following to your UserEntity class:

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

friend.setRole("yourRole");

Another way to do it, when you're using the advanced mapping mode, is using one of the NodeBacked.relateTo() methods. Then add the property to the returned Relationship.

And a third way, it to use the Neo4jTemplate.createRelationshipBetween() method and provide your properties (e.g. role) as the final argument.

Upvotes: 3

Related Questions