Reputation: 1823
In SDN4 I wish to persist a @RelationshipEntity
which is not a @NodeEntity
's property.
Example:
@NodeEntity
public class User{
Long id;
}
@RelationshipEntity(type="FOLLOWS")
public class Follows{
@GraphId private Long relationshipId;
@StartNode private User follower;
@EndNode private User followee;
@Property private Date from;
public Follows(){}
public Follows(User u1, User u2){
this.follower = u1;
this.followee = u2;
}
}
@Repository
interface FollowsRepository extends GraphRepository<Follows>{}
And then persist the Follows
@Relationship like this
...
followsRepository.save(new Follows(user1, user2));
...
But when doing so, the Relationship is not persisted!!
Sadly as stated in the accepted answer this cannot (yet) be done (SDN 4.0.0.RELEASE)
It is possible to persist @RelationshipEntities
using @Query
in GraphRepositories
.
@Query("Match (a:User), (b:User) WHERE id(a) = {0}
AND id(b) = {1} CREATE (a)-[r:FOLLOWS {date:{2}}]->(b) RETURN r ")
This can also be done by treating Follows
as a @NodeEntity
, which might not be the most performant thing to do BUT will not affect any of the @NodeEntities
of the domain, nor the service layer AND you won't have to mess with the depth factor when loading and persisting entities
@NodeEntity
public class User{
Long id;
}
@NodeEntity
public class Follows{
private Long Id;
@Relationship(type="FOLLOWER")
private User follower;
@Relationship(type="FOLLOWEE")
private User followee;
private Date from;
public Follows(){}
public Follows(User u1, User u2){
this.follower = u1;
this.followee = u2;
}
}
....
//Placed in userService
Follows createFollowsRelationship(User u1, User u2){
return followsRepository.save(new Follows(user1, user2));
}
.....
Upvotes: 1
Views: 243
Reputation: 19373
At the moment, you cannot persist a relationship entity directly when it is not referenced from participating node entities. You'll have to save the start node and make sure it has a reference to the relationship entity.
There will be some enhancements around how relationship entities are persisted but not in the next release.
Upvotes: 3