Spring 数据 Neo4j 4 - findById(长 id)return 空

Spring Data Neo4j 4 - findById(Long id) return null

我正在使用 SDN 4 和 neo4j-ogm 1.1.4

我正在尝试使用 findById(Long id) GraphRepository 获取我的数据,但始终 return 为空。之后我尝试使用 findByName(String name) 并且它起作用了。我知道有使用 findOne(Long id, int depth) 的替代方法,但是当我想进行自定义查询时,例如 findByObjectId(Long id),它会很麻烦。

在 neo4j 尝试手动查询后,它 return 也为空。那么关于这个有什么问题吗?

@NodeEntity
public class Fetch1 {

    @GraphId Long id;

    private String name;

    @Relationship(type="HAS_FETCH2")
    @JsonIgnore
    private List<Fetch2> fetch2;

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

    @Relationship(type="HAS_FETCH2")
    @JsonIgnore
    public List<Fetch2> getFetch2() {
        return fetch2;
    }

    @Relationship(type="HAS_FETCH2")
    @JsonIgnore
    public void setFetch2(List<Fetch2> fetch2) {
        this.fetch2 = fetch2;
    }

    @Override
    public String toString() {
        return "Fetch1 [id=" + id + ", name=" + name + ", fetch2=" + fetch2 + "]";
    }
}

@NodeEntity
public class Fetch2 {

    @GraphId Long id;

    private String name;

    @Relationship(type="HAS_FETCH2", direction=Relationship.INCOMING)
    @JsonIgnore
    private Fetch1 fetch1;

    @Relationship(type="HAS_FETCH3")
    @JsonIgnore
    private List<Fetch3> fetch3;

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

    @Relationship(type="HAS_FETCH2", direction=Relationship.INCOMING)
    @JsonIgnore
    public Fetch1 getFetch1() {
        return fetch1;
    }

    @Relationship(type="HAS_FETCH2", direction=Relationship.INCOMING)
    @JsonIgnore
    public void setFetch1(Fetch1 fetch1) {
        this.fetch1 = fetch1;
    }

    @Relationship(type="HAS_FETCH3")
    @JsonIgnore
    public List<Fetch3> getFetch3() {
        return fetch3;
    }

    @Relationship(type="HAS_FETCH3")
    @JsonIgnore
    public void setFetch3(List<Fetch3> fetch3) {
        this.fetch3 = fetch3;
    }

    @Override
    public String toString() {
        return "Fetch2 [id=" + id + ", name=" + name + ", fetch1=" + fetch1 + ", fetch3=" + fetch3 + "]";
    }    
}

这是我的存储库

public interface Fetch1Repository extends GraphRepository<Fetch1>{

    Fetch1 findById(Long id);
    Fetch1 findByFetch2Id(Long id);
    Fetch1 findByFetch2Name(String name);
}

在这种情况下,findById 不会按您预期的方式工作,因为 id 不是图中的节点 属性,而 findByXXX 会查找属性。

在 Cypher 中,区别是:

MATCH (n) WHERE id(n) = .... // find by id

MATCH (n {n.name = "Steve Jobs" }) ... // find by property

只需使用 findOne(id) 或 findOne(id, depth)。