简体   繁体   中英

DynamicRelationshipType in Spring Data Neo4j or defining relationship types at runtime

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?

  • 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 .

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