Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-14-2021 04:02 AM
Hi! I am changing from spring boot 2.3 to 2.4, with that to neo4j 6.
I am rewriting the "RelationshipEntity"s to "RelationshipPropery".
The difference here is that a RelationshipProperty now only has one end of the relation as "TargetNode", instead of "StartNode"/"EndNode".
I read here that a bidirectional relationship in neo4j is also just onedirectional, and the other side is implied.
So now saving for example a Friendship into person1's "FriendshipList", with a targetNode set to person2 will imply the otherside, and person2 will also have a friendship to person1?
Or do i have to set it in both directions as shown below.
As an example:
public class Person{
@Relationship(type = "Friend_With")
List<FriendShip> friendShips;
............
............
}
@RelationshipProperties
@Data
public class FriendShip
{
@Id
@GeneratedValue(generatorClass = UUIDGenerator.class)
private String id;
@TargetNode
private Person personEnd;
private String type = "normal";
public String getClassName()
{
return this.getClass().getName();
}
}
//saving the relationship:
Person person1 = new Person();
Person person2 = new Person();
Friendship friendship1= new Friendship();
friendship1.setTargetnode(person2)
Friendship friendship2= new Friendship();
friendship2.setTargetnode(person1)
person1.getFriendships().add(friendship1);
person2.getFriendships().add(friendship2);
personRepo.saveAll(List.of(person1,person2));
friendshipRepo.saveAll(List.of(friendship1,friendShip2));
Hope it's not too confusing what i mean.
Thanks,
Daniel
01-15-2021 01:50 AM
Hello,
In your domain, if the fact that person A is friend with person B implies that person B is friend with person A, then it is better to define the relationship on only 1 entity.
I think that the code you wrote above would result in 2 relationships being created, not once, which would be redundant.
One benefit of avoiding cycles like this in your mapped graph is that you will not have to deal with recursive serialization issues if you ever expose such entities via REST API for instance (although there are solutions for this problem).
All the sessions of the conference are now available online