Spring 双方数据neo4j访问关系

Spring Data neo4j access relationship from both sides

在我的 neo4j 数据库中,我有这个

(:Song)-[:APPEARS_ON {song_nr: x}]->(:Release)

从我的 Spring 引导应用程序,我想从歌曲端和发行端访问此关系,同时访问歌曲编号。

Song.java

@Node
public class Song {

    @Id @GeneratedValue
    private Long id;
    private String name;
    private OffsetTime length;

    @Relationship(type="APPEARS_ON", direction = Relationship.Direction.OUTGOING)
    private final List<AppearsOn> releases;

    private Song(String name, OffsetTime length, List<AppearsOn> releases) {
        this.name = name;
        this.length = length;
        this.releases = releases;
    }

    public Long getId() { return id; }

    public String getName() { return name; }

    public OffsetTime getLength() { return length; }

    public List<AppearsOn> getReleases() { return releases; }

}

AppearsOn.java

@RelationshipProperties
public class AppearsOn {

    @Id @GeneratedValue
    private Long id;
    @Property("song_nr")
    private Integer songNr;

    @TargetNode
    private Release release;

    public Long getId() {
        return id;
    }


    public Integer getSongNr() { return songNr; }

    public Release getRelease() { return release; }
}

Release.java

@Node
public class Release {

    @Id @GeneratedValue
    private Long Id;
    private String name;
    private Integer year;

    @Relationship(type = "APPEARS_ON", direction = Relationship.Direction.INCOMING)
    private final List<AppearsOnReverse> songs;

    public Release(String name, Integer year, List<AppearsOnReverse> songs) {
        this.name = name;
        this.year = year;
        this.songs = songs;
    }

    public Long getId() {
        return Id;
    }

    public String getName() {
        return name;
    }

    public Integer getYear() {
        return year;
    }

    public List<AppearsOnReverse> getSongs() { return songs; }

}

AppearsOnReverse.java

@RelationshipProperties
public class AppearsOnReverse {

    @Id @GeneratedValue
    private Long id;
    @Property("song_nr")
    private Integer songNr;

    @TargetNode
    private Song song;

    public Long getId() {
        return id;
    }


    public Integer getSongNr() {
        return songNr;
    }

    public Song getSong() {
        return song;
    }
}

当我尝试查询某些内容时,例如List<Release> allReleases = releaseRepository.findAll();

我遇到这样的异常:

org.neo4j.driver.exceptions.DatabaseException: Expected 
RegularSinglePlannerQuery(QueryGraph {Nodes: ['  rootNodeIds@7'], .......


Instead, got: 
RegularSinglePlannerQuery(QueryGraph {Nodes: ['  rootNodeIds@7'], .......

是否有可能从双方访问关系,或者这会破坏 SDN 吗?

org.neo4j.driver v4.2.7
spring-boot-starter-data-neo4j v2.5.6
spring-data-neo4j v6.1.6

你的映射/模型没有问题 问题出在您使用的 Neo4j 版本中。 它有一个查询规划器错误。 不知道是什么时候出现的,4.3.3 / 4.2.10 及更高版本解决了。

这与 SDN 6.1.6 中一些需要的查询改进相结合导致了现在有问题的星座。 我建议手动将 Spring Data Neo4j 版本设置为 6.1.5 如果 您无法升级数据库。