Spring Data Neo4j 4,OGM和@Relationship注释

时间:2017-03-18 13:14:40

标签: neo4j spring-data-neo4j-4 neo4j-ogm

我有以下基本实体:

@NodeEntity
public class Likeable extends Authorable {

    private final static String CONTAINS = "CONTAINS";

    @Relationship(type = CONTAINS, direction = Relationship.INCOMING)
    private Set<Like> likes = new HashSet<>();

    private long totalLikes;

    private long totalDislikes;

    private double likeSum;

    public long getTotalLikes() {
        return totalLikes;
    }

    public void setTotalLikes(long totalLikes) {
        this.totalLikes = totalLikes;
    }

    public long getTotalDislikes() {
        return totalDislikes;
    }

    public void setTotalDislikes(long totalDislikes) {
        this.totalDislikes = totalDislikes;
    }

    public double getLikeSum() {
        return likeSum;
    }

    public void setLikeSum(double likeSum) {
        this.likeSum = likeSum;
    }

}

使用当前类一切正常但是当我将likes getter / setter添加到此类时:

public Set<Like> getLikes() {
    return likes;
}

public void setLikes(Set<Like> likes) {
    this.likes = likes;
}

我的测试失败并出现断言错误(无例外)..例如删除查询后。

但是在添加

之后
@Relationship(type = CONTAINS, direction = Relationship.INCOMING)

over getter / setter一切都恢复正常:

@NodeEntity
public class Likeable extends Authorable {

    private final static String CONTAINS = "CONTAINS";

    @Relationship(type = CONTAINS, direction = Relationship.INCOMING)
    private Set<Like> likes = new HashSet<>();

    private long totalLikes;

    private long totalDislikes;

    private double likeSum;

    @Relationship(type = CONTAINS, direction = Relationship.INCOMING)
    public Set<Like> getLikes() {
        return likes;
    }

    @Relationship(type = CONTAINS, direction = Relationship.INCOMING)
    public void setLikes(Set<Like> likes) {
        this.likes = likes;
    }
...

}

它是一个正确注释的NodeEntity类吗?对我来说,这种宣言看起来有点多余。在这种情况下,您能否告诉我们什么是正确的声明?

1 个答案:

答案 0 :(得分:4)

是的,这是现在实现它的正确方法 - 从neo4j-ogm版本2.1.x开始。

参见文档:

  

@Relationship上的direction属性默认为OUTGOING。必须使用INCOMING方向显式注释由INCOMING关系支持的任何字段或方法。

https://neo4j.com/docs/ogm-manual/current/reference/#reference:annotating-entities:relationship(本节末尾)。

我认为 你实际上不需要字段上的注释,但你需要setter和getter。此外,neo4-ogm github上存在相关问题。