1

I have an object (which is a graph node) that has a one-many relationship to another object (which is also a graph node). When I persist the first object, I would want it to persist any dependent objects and create the appropriate relationships as well.

This works, however the relationships that get created in this manner is auto-generated by Spring Data and it only contains the type. It doesn't make an instance of the RelationshipEntity class that I have defined before and there is no way to specify the class that I want it to use. It also doesn't get included in the Spring Data maintained LocationHasMany index.

Here are my class definitions:

@NodeEntity
public class LocationNode {

    private String name;

    @GraphId
    private Long id;

    @RelatedTo(type="HAS_MANY", elementClass=CategoryNode.class, enforceTargetType=true)
    private Set<CategoryNode> categories = new HashSet<CategoryNode>();

}

@NodeEntity
public class CategoryNode {

    private String name;

    @GraphId
    private Long id;

}



@RelationshipEntity(type="HAS_MANY")
public class LocationHasMany {

    @GraphId
    private Long nodeId;

    @StartNode
    private LocationNode location;

    @EndNode
    private CategoryNode category;

}

When I persist with Spring Data, it will generate the location node and the category node in addition to a relationship called "HAS_MANY". However, it is not the same as if I persisted the relationship myself. There is no internal __TYPE__ field that is indicative of a Spring Data persisted object.

On my LocationNode object, I can do something like this:

@NodeEntity
public class LocationNode {

    @RelatedToVia(type="HAS_MANY", elementClass=LocationHasMany.class)
    private Set<LocationHasMany> categories;

}

But then I feel as if that adds unnecessary detail to my LocationNode object that it is persisted using some sort of Graph layer. It seems like the annotation @RelatedTo should have the ability to specify a specific RelationshipEntity class that needs to be used during relationship creation. Also, in this scenario, I also need to make sure that all the CategoryNodes are persisted before I persist the LocationNode. Otherwise the relationship creation throws an error. (Still lots of manual dependency handling)

Has anyone encountered this issue before and solved it?

My last solution is to created a custom Repository implementation and then handle all the relationship creation / object persistence in that method. ( saveLocationWithDependencies(LocationNode location) ) However, I would prefer to not have to do it for every object that has nested graph nodes. That is a ton of extra code that will have to be maintained.

4

0 回答 0