DynamicRelationshipType in Spring Data Neo4j or defining relationship types at runtime

感情迁移 提交于 2019-12-12 18:05:47

问题


Can I specify relationship type at runtime??

I am creating a set of relationshipEntity objects within an Entity using something like

@Fetch
@RelatedToVia(type="RELATED_IN_SOME_WAY", direction = Direction.BOTH)
Set<ThingRelationship> relationships = new HashSet<ThingRelationship>();

where ThingRelationship is

@RelationshipEntity
public class ThingRelationship {

public ThingRelationship() {
    super();
}

//incremental neo4j set ID
@GraphId Long nodeId;

//Start and end nodes
@StartNode Thing startThing;
@EndNode Thing endThing;

//Relationship Type
@org.springframework.data.neo4j.annotation.RelationshipType
String relationship;

However I DONT want to specify the relationship type (type="RELATED_IN_SOME_WAY") at compile time but rather at runtime. When I remove type="RELATED_IN_SOME_WAY I get an error that a default type must be defined

In Neo4j such a runtime relationship type I think requires the use of DynamicRelationshipType however I dont think the Spring Data Neo4j supports this concept.

Am I correct and if so is there anyway around this problem? Do I need to dump Spring Data Neo4j and go to use the Core API instead?


回答1:


  • In Neo4j such a runtime relationship type I think requires the use of DynamicRelationshipType however I dont think the Spring Data Neo4j supports this concept.

From the reference documentation

Note

Because dynamic type information is, well, dynamic, it is generally not possible to read the mapping backwards using SDN. The relationship still exists, but SDN cannot help you access it because it does not know what type you gave it. Also, for this reason, we require you to specify a default relationship type, so that we can at least attempt the reverse mapping.

So while the dynamic relationship is still created, it can't use that information to retrieve the nodes/relationships back from the Neo4j db. The default relationship is required so that SDN can at least return the known relationship.

  • Am I correct and if so is there anyway around this problem? Do I need to dump Spring Data Neo4j and go to use the Core API instead?

You can use the SDN to create all dynamic relationships you want using the @RelationshipType but you can't retrieve it back using the default API. You can use write your own Cypher or write traversal code and attach it to your repository or a node property using @Query.



来源:https://stackoverflow.com/questions/18517000/dynamicrelationshiptype-in-spring-data-neo4j-or-defining-relationship-types-at-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!