4

I'm trying to upgrade from Spring Data Neo4J 3 to 4 - I'm using Neo4J 2.2.2.

I use a GraphRepository instance to query the database, fetching back an object.

This object has several relationships, which are not fetched (deliberately, to avoid reading in the entire graph).

In the SDN3 code, simply used the Neo4JTemplate class to perform a fetch call for each relationship I needed to fetch. This worked extremely well.

However, in SDN4 this facility has been removed, and replaced by various implementations of the load() method. It's not clear from the documentation how to achieve what I did in SDN3.

To be clear: if I have a Set of objects in the first class I retrieve, governed by a relationship, I want to retrieve only the objects in that Set, not the entire collection of those objects in the database.

Have I missed something crucial in the upgrade process, or is there a simple way of doing what I'm trying to do?

Adding code:

My entity class:

@NodeEntity
public class File implements MetroNode {

    private Long id;

    private String fileName;

    private SourceState sourceState;

    private Set<State> states;

    @GraphId
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    @Relationship(type = "HAS_FILE", direction = Relationship.INCOMING)
    public SourceState getSourceState() {
        return sourceState;
    }

    public void setSourceState(SourceState sourceState) {
        this.sourceState = sourceState;
    }

    public State addState(MetroNode otherNode, StateStatus status) {
        if (this.states == null) {
            this.states = new HashSet<State>();
        }
        State state = new State(this, otherNode, status.toString());
        this.states.add(state);
        return state;
    }

    @Relationship(type = "HAS_STATE", direction = Relationship.OUTGOING)
    public Set<State> getStates() {
        return states;
    }

    public State getActiveState() {
        if (this.states != null) {
            for (State state : this.states) {
                if (state.isActive()) {
                    return state;
                }
            }
        }
        return null;
    }

}

My repository class:

public interface FileRepository extends GraphRepository<File> {

    File findByFileName(String fileName);     
}

When executing the getActiveState() method I get a null return, because the states Set is empty (hasn't been fetched).

Looking again at my code, I wonder if it's because I'm not using a "native" load method from the repository, but the overloaded version?

TrueDub
  • 5,000
  • 1
  • 27
  • 33

1 Answers1

0

SDN 4 allows you to control loading of related entities with the persistence horizon.

Loading an entity with depth 0 will fetch properties of the entity and no related entities. Depth 1 will fetch the first level of related entities, but not their relations and so on.

Controlling the depth by relationship type is not supported.

Luanne
  • 19,145
  • 1
  • 39
  • 51
  • Thanks for your quick reply. I've read the doc you link to and it contains this sentence "By default, loading an instance will map that object’s simple properties and its immediately-related objects (i.e. depth = 1). " This appears not to be the case in my app, where a collection of objects specified by a relationship are not populated. Have I misunderstood the situation here? – TrueDub Dec 15 '15 at 10:49
  • That does not sound right. Default depth 1 should load related objects. Can you update your question with some code or a test and any lines of interest in the debug log? – Luanne Dec 15 '15 at 11:11
  • Which version of SDN 4 are you using? Could you try the same with 4.1.0.BUILD-SNAPSHOT? You'll need to include spring-libs-snapshot Spring http://repo.spring.io/libs-snapshot spring-snapshots http://repo.spring.io/snapshot true – Luanne Dec 15 '15 at 15:32
  • You also need to make sure that any INCOMING relationships have their methods annotated (if present, else fields)- so you need the @Relationship(type = "HAS_FILE", direction = Relationship.INCOMING) also on setSourceState – Luanne Dec 15 '15 at 15:38
  • I'll try with 4.1.0.BUILD-SNAPHOT asap. Are you sure about that annotation? It needs to be on the setter & getter in the same class? Also, I tried to load by executing "File newFile = template.load(File.class, file.getId(),-1)", as an experiment, and it still returned null for the related objects – TrueDub Dec 15 '15 at 16:04
  • Building against that snapshot and repo fails - cannot find neo4j-ogm-1.1.4-SNAPSHOT.jar, doesn't exist in the repos you specified – TrueDub Dec 15 '15 at 16:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98049/discussion-between-luanne-and-truedub). – Luanne Dec 16 '15 at 03:35