Neo4j-spring-data(4.1.1)自我关系(父子)是重复的

时间:2016-05-01 15:58:58

标签: neo4j spring-data-neo4j-4 neo4j-ogm

我正在使用spring-data-4.1.1&带有ogm注释的Neo4j 2.3.2

以下是我的实体

    @NodeEntity(label = "Component")
    public class Component extends BaseEntity {
    .........

      @Relationship(type = Relation.LINK_TO)
      private Set<Link> links = new HashSet<>();

      @Relationship(type = Relation.PARENT)
      private Set<Component> parents = new HashSet<>();
    .........
    .........


}

和Link类

@RelationshipEntity(type = Relation.LINK_TO)
public class Link extends BaseEntity {

    @Property(name = "isSelfLink")
    private boolean isSelfLink;

    @StartNode
    private Component component;

    @EndNode
    private Component linkComponent;
}

我已删除了getter / setter / hashcode / equals以保持其清洁

现在,我的代码是添加两个组件父/子和一个链接

Component parentcomp = new Component(1, name);
Component childcomp =  new Component(2, name);
childcomp.getParents().add(parentcomp);

Link link = new Link();
link.setComponent(parentcomp);
link.setLinkComponent(childcomp);
parentcomp.getLinks().add(link);

componentRepository.save(parentcomp,-1);

现在,根据逻辑

  1. 对象parentcomp属性&#39; parent&#39;应该是空的
  2. 对象childcomp属性&#39; parent&#39;应该有parentcomp对象
  3. 并且parentcomp属性&#39;链接&#39;应该有childcomp

    (parentcomp)---- ---- LINKS_TO&GT;(childcomp)

    (parentcomp)LT; ---- ---- PARENT(childcomp)

  4. 注意:我的要求是我们需要双向关系..

    但是,下面是加载父实体或子实体时的结果

    1. 对象 parentcomp 属性&#39; parent&#39;有childcomp,parentcomp而不是空
    2. 对象 childcomp 属性&#39; parent&#39;同时具有childcomp,parentcomp而不是parentcomp
    3. 此行为一直存在,直到Neo4j会话在内部清除。一段时间后(或在应用程序重启后),映射会正确显示。

      我尝试使用neo4joperations.clear()清理会话仍然存在问题。但如果我查询

      match (c:Component)-[:PARENT]->(p) where c.componentId = {0} return p
      

      结果是正确的。

      我不确定如何解决这个问题...

0 个答案:

没有答案