简体   繁体   中英

Spring Data Neo4J 4 - no template.fetch()

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM