简体   繁体   English

Spring Data Neo4j中的DynamicRelationshipType或在运行时定义关系类型

[英]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 我正在使用类似的东西在实体内创建一组RelationshipEntity对象

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

where ThingRelationship is ThingRelationship在哪里

@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. 但是我不想在编译时而是在运行时指定关系类型( type =“ RELATED_IN_SOME_WAY” )。 When I remove type="RELATED_IN_SOME_WAY I get an error that a default type must be defined 当我删除type =“ RELATED_IN_SOME_WAY时,出现错误,必须定义默认类型

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. 在Neo4j中,我认为这样的运行时关系类型需要使用DynamicRelationshipType,但是我不认为Spring Data Neo4j支持此概念。

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? 我是否需要转储Spring Data Neo4j并转而使用Core API?

  • 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. 在Neo4j中,我认为这样的运行时关系类型需要使用DynamicRelationshipType,但是我不认为Spring Data Neo4j支持此概念。

From the reference documentation 参考文档

Note 注意

Because dynamic type information is, well, dynamic, it is generally not possible to read the mapping backwards using SDN. 因为动态类型信息是动态的,所以通常无法使用SDN向后读取映射。 The relationship still exists, but SDN cannot help you access it because it does not know what type you gave it. 该关系仍然存在,但是SDN无法帮助您访问它,因为它不知道您给它指定了什么类型。 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. 因此,尽管仍然创建了动态关系,但是它无法使用该信息从Neo4j db中检索回节点/关系。 The default relationship is required so that SDN can at least return the known relationship. 默认关系是必需的,以便SDN至少可以返回已知关系。

  • 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? 我是否需要转储Spring Data Neo4j并转而使用Core API?

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. 您可以使用SDN使用@RelationshipType创建所需的所有动态关系,但无法使用默认API找回它。 You can use write your own Cypher or write traversal code and attach it to your repository or a node property using @Query . 您可以编写自己的Cypher或编写遍历代码,然后使用@Query将其附加到存储库或节点属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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