【问题标题】:Spring Data ArangoDB get parent directly with the annotationSpring Data ArangoDB 直接使用注解获取父级
【发布时间】:2018-06-21 07:49:40
【问题描述】:

这是我的父子映射

家长

@Document("parents")
public class NodeParent extends CommonBo {

  private String name;

  @To(lazy = true)
  private List<RelationChildOf> childsOf = new ArrayList<>();

  ...
}

关系

@Edge(value="childof")
public class RelationChildOf {

  @From
  private NodeChild child;

  @To
  private NodeParent parent;

  ...
}

儿童

@Edge(value="children")
public class NodeChild extends CommonBo {

  ...
}

如何使用派生查询对子代中的父代进行建模和检索?

我在演示代码中看不到它https://github.com/arangodb/spring-data-demo

【问题讨论】:

    标签: graph spring-data arangodb


    【解决方案1】:

    如果您想通过父名查找子级,可以使用@Relations 而不是@To(请参阅docs)。这样您就不必在您的应用程序中接收边缘RelationChildOf

    public class CommonBo {
      @Id
      private String id;
    }
    
    @Document("parents")
    public class NodeParent extends CommonBo {
      private String name;
    }
    
    @Document(value = "children")
    public class NodeChild extends CommonBo {
      @Relations(edges = { RelationChildOf.class }, lazy = true)
      private final List<NodeParent> parents = new ArrayList<>();
    }
    

    repository 接口中的 find 方法必须使用 NodeChild 中的字段 parentsNodeParent 中的字段 name 的名称。 findBy-Parents-Name-(String)

    public interface ChildRepository extends ArangoRepository<NodeChild> {
      NodeChild findByParentsName(String name);
    }
    

    用法:

      @Autowired
      ParentsRepository parentsRepository;
    
      @Autowired
      ChildRepository childRepository;
    
      @Autowired
      ChildOfRepository childOfRepository;
    
      @Test
      public void test() {
        NodeParent parent = new NodeParent("John");
        parentsRepository.save(parent);
    
        NodeChild child = new NodeChild("John Junior");
        childRepository.save(child);
    
        RelationChildOf childOf = new RelationChildOf(child, parent);
        childOfRepository.save(childOf);
    
        NodeChild childOfJohn = childRepository.findByParentsName("John");
        assertThat(childOfJohn.name, is(child.name));
      }
    

    【讨论】:

      猜你喜欢
      • 2018-08-13
      • 2017-08-31
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 2020-04-22
      • 2021-07-03
      • 2016-04-11
      • 2013-01-27
      相关资源
      最近更新 更多