【问题标题】:Spring Data Neo4j 4 - findById(Long id) return nullSpring Data Neo4j 4 - findById(Long id) 返回 null
【发布时间】:2016-04-19 12:42:49
【问题描述】:

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

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

在 neo4j 尝试手动查询后,它也返回 null。那么这有什么问题吗?

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

【问题讨论】:

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


    【解决方案1】:

    在这种情况下,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)。

    【讨论】:

    • 是的,我尝试使用 findOne(id) 并且它有效。顺便说一句,在以前的版本 SDN 3 中,我可以使用 findById() 并检索里面的对象。在 SDN 4 中,我不能再使用它了。
    • 假设你有一个类,它的内部 id 字段被称为“graphId”而不是“id”,为了争论。在同一个类中,您还有一个名为“id”的属性,它表示对象的一些业务 id(而不是其数据库 id)。你希望 findById() 做什么?如果您有一个名为“id”的属性,FindById 将起作用。是的,它与 SDN 3 不同,但我认为 findByXXX 的 SDN 3 语义是模棱两可的。
    • 哦,我明白了,现在我明白了。所以最后一个问题,我如何检索具有 2 个参数、Id 和其他属性的对象?我不能使用 findByIdAndProperty()
    • 如果您想说“仅当它具有“启用”属性时才检索此已知对象,您可以使用查询:@Query("MATCH (t:Entity) where ID (t) = {0} and t.status = {1}") public Entity getEntityWithStatus(Long id, String status);
    猜你喜欢
    • 2017-06-24
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    相关资源
    最近更新 更多