Neo4J OGM 中的设置关系属性

Settings Relationship Properties in Neo4J OGM

我昨天发现了 Neo4j OGM,并迅速创建了一个新项目来测试它的工作原理。我遇到的一个问题是设置 Relationhip 属性,因为这对我的项目至关重要。这是一个例子:

房间节点:

@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);
}
}

连接节点(关系):

@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(){

}

}

主要方法:

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);

}

现在如您所见,我已经在我的主要方法中设置了长度,但是当我检查 http://localhost:7474 下的数据库时,它显示了节点之间的关系,但说它没有属性。

控制台输出如下:http://pastebin.com/CByfmVcR

如能提供有关设置 属性 的任何帮助,我们将不胜感激。 或者是否有 different/easier 将对象映射到 neo4J 数据库的方法?

谢谢

使用关系实体是正确的做法,因为您拥有关系的属性。但这也意味着您的房间之间的关系由 Connects 表示。 因此,Room 应该引用 Connects 而不是直接引用另一个 Room

例如

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

这是一个类似于您的域模型的测试:

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

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

我注意到您使用的是 neo4j-ogm 1.1.3。请升级到 1.1.4,因为它包含重要修复。