I am trying to model a social network with Neo4J. This requires a user can have multiple relationships with another user. When I try to persist these relationships, only one is stored. For example, this is the test unit I do:
@Test public void testFindConnections() { Id id1 = new Id(); id1.setId("first-node"); Id id2 = new Id(); id2.setId("second-node"); idService.save(id2); id1.connectedTo(id2, "first-rel"); id1.connectedTo(id2, "second-rel"); idService.save(id1); for (Id im : idService.findAll()) { System.out.println("-" + im.getId()); if (im.getConnections().size() > 0) { for (ConnectionType ite : im.getConnections()) { System.out .println("--" + ite.getId() + " " + ite.getType()); } } } }
This should output:
-first-node --0 first-rel --1 second-rel -second-node --0 first-rel --1 second-rel
However, it outputs:
-first-node --0 first-rel -second-node --0 first-rel
This is my node entity:
@NodeEntity public class Id { @GraphId Long nodeId; @Indexed(unique = false) String id; @Fetch @RelatedToVia(direction=Direction.BOTH) Collection<ConnectionType> connections = new HashSet<ConnectionType>(); }
And my relationship entity:
@RelationshipEntity(type = "CONNECTED_TO") public class ConnectionType { @GraphId Long id; @StartNode Id fromUser; @EndNode Id toUser; String type; }
What could the problem be? Is there any other way to model several relationships between nodes?