3

I found out about Neo4j OGM yesterday and quickly made a new project to test out how it works. One problem I've come across is setting Relationhip properties as this is crucial for my project. Here's an example:

Room Node:

@NodeEntity
public class Room {

@GraphId
Long id;

@Property(name="name")
String name;

@Relationship(type="CONNECTS")
List<Room> rooms;

public List<Room> getRooms() {
    if(rooms == null)
        rooms = new ArrayList<Room>();

    return rooms;
}

public void setRooms(List<Room> rooms) {
    this.rooms = rooms;
}

public Room(String name){
    this.name = name;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Room(){
}

public void connectsTo(Room room){
    this.getRooms().add(room);
}
}

Connects Node (Relation):

@RelationshipEntity(type="CONNECTS")
public class Connects {

@GraphId
Long id;

@StartNode
Room startMapNode;

@EndNode
Room endMapNode;

@Property(name="length")
int length;

public Connects(Room startMapNode, Room endMapNode){
    this.startMapNode = startMapNode;
    this.endMapNode = endMapNode;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public Room getStartMapNode() {
    return startMapNode;
}

public void setStartMapNode(Room startMapNode) {
    this.startMapNode = startMapNode;
}

public Room getEndMapNode() {
    return endMapNode;
}

public void setEndMapNode(Room endMapNode) {
    this.endMapNode = endMapNode;
}

public int getLength() {
    return length;
}

public void setLength(int length) {
    this.length = length;
}

public Connects(){

}

}

Main method:

public static void main(String[] args) {
    SessionFactory sessionFactory = new SessionFactory("at.htl.in110010.domain");
    Session session  = sessionFactory.openSession("http://localhost:7474");

    session.purgeDatabase();

    Room roomOne = new Room("TEST_ROOM_ONE");
    Room roomTwo = new Room("TEST_ROOM_TWO");

    roomOne.connectsTo(roomTwo);
    roomTwo.connectsTo(roomOne);

    Connects connectRelation = new Connects(roomOne,roomTwo);
    connectRelation.setLength(2);

    session.save(connectRelation);

}

Now as you can see I've set the length in my main method, but when I check the database under http://localhost:7474 it shows the relation between the nodes but says it has no properties.

Here is the console output: http://pastebin.com/CByfmVcR

Any help in setting the Property would be very appreciated. Or is there perhaps a different/easier way of mapping objects to the neo4J database ?

Thanks

Luanne
  • 19,145
  • 1
  • 39
  • 51

1 Answers1

3

Using a relationship entity is the right thing to do as you have properties on the relationship. But this also means that your relationship between rooms is represented by Connects. So, Room should have a reference to Connects rather than to the other Room directly.

e.g.

@Relationship(type="CONNECTS")
List<Connects> rooms;

Here's a test that resembles your domain model:

https://github.com/neo4j/neo4j-ogm/tree/master/src/test/java/org/neo4j/ogm/domain/friendships and

https://github.com/neo4j/neo4j-ogm/blob/master/src/test/java/org/neo4j/ogm/integration/friendships/FriendshipsRelationshipEntityTest.java

I notice you're using neo4j-ogm 1.1.3. Please upgrade to 1.1.4 as it contains important fixes.

Luanne
  • 19,145
  • 1
  • 39
  • 51