0

I am using SDN 4 and neo4j-ogm 1.1.4

I am trying to fetch my data using findById(Long id) GraphRepository, but always return null. After that I am trying to use findByName(String name) and its worked. I know there is alternative using findOne(Long id, int depth), but when I want to make custom query, for example findByObjectId(Long id), it will be trouble.

After try manual query at neo4j, it return null too. So any issue about this ?

@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 + "]";
    }    
}

And this is my Repository

public interface Fetch1Repository extends GraphRepository<Fetch1>{

    Fetch1 findById(Long id);
    Fetch1 findByFetch2Id(Long id);
    Fetch1 findByFetch2Name(String name);
}
David Vincent
  • 634
  • 1
  • 8
  • 33

1 Answers1

4

In this case, findById won't work the way you expect, because the id is not a node property in the graph, and findByXXX looks for properties.

In Cypher, the difference is:

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

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

Just use findOne(id) or findOne(id, depth).

Vince
  • 2,181
  • 13
  • 16
  • 1
    Yes, i tried using findOne(id) and its worked. By the way, in previous version, SDN 3, I can use findById() and retrieve the object inside. In SDN 4, i can't use it anymore. – David Vincent Jan 14 '16 at 09:59
  • Imagine you had a class whose internal id field was called "graphId" rather than "id", for the sake of argument. On that same class you also had a property called "id" as well, which represented some business id for the object (rather than its database id). What would you expect findById() to do? FindById will work if you have a property called "id". Yes it is different from SDN 3, but I would argue SDN 3 semantics for findByXXX were ambiguous. – Vince Jan 14 '16 at 10:10
  • Oh i see, now I understand. So last question, how i can retrieve object with 2 parameter, Id and other property? I can't use findByIdAndProperty() – David Vincent Jan 14 '16 at 10:19
  • If you'd like to say something like 'retrieve this known object only if it has the property "enabled", you can use a query: @Query("MATCH (t:Entity) where ID(t) = {0} and t.status = {1}") public Entity getEntityWithStatus(Long id, String status); – Vince Jan 14 '16 at 12:24