【问题标题】:How to retrieve additional attribute on edge with Spring Data ArangoDB如何使用 Spring Data ArangoDB 在边缘检索附加属性
【发布时间】:2020-04-25 23:04:50
【问题描述】:

我刚刚开始使用 ArangoDB 及其 spring 数据库实现一个爱好项目。

我创建了两个名为 User 和 Post 的文档。并创建了一个名为 Vote 的边缘。

我的 Vote 自定义属性不同于 _from 和 _to。我可以使用该自定义属性保存该边缘,也可以从 ArangoDB ui 中看到它。但我无法用我的 Java 对象检索该属性。

我的环境:

  • ArangoDB 版本:3.6.3
  • arangodb-spring-data 版本:3.1.0

我的课程如下:

@Document("posts")
@Getter @Setter @NoArgsConstructor
public class Post {

  @Id
  String id;

  @Relations(edges = Vote.class, lazy = true, direction = Direction.INBOUND)
  Collection<Vote> votes;
  @Ref
  User writtenBy;

  String content;
  List<String> externalLinks;
  List<String> internalLinks;
  
  public Post(String content) {
    super();
    this.content = content;
  }
  
}


@Document("users")
@Getter @Setter @NoArgsConstructor
public class User {

  @Id
  String id;

  String name;
  String surname;
  String nick;
  String password;
  
  public User(String name, String surname, String nick) {
    super();
    this.name = name;
    this.surname = surname;
    this.nick = nick;
  }

}

@Edge
@Getter @Setter @NoArgsConstructor
@HashIndex(fields = { "user", "post" }, unique = true)
public class Vote {

  @Id
  String id;

  @From
  User user;

  @To
  Post post;

  Boolean upvoted;

  public Vote(User user, Post post, Boolean upvoted) {
    super();
    this.user = user;
    this.post = post;
    this.upvoted = upvoted;
  }

}

【问题讨论】:

    标签: spring-data arangodb


    【解决方案1】:

    @Relations 注释应应用于表示相关顶点(而不是边)的字段。例如。这应该工作:

      @Relations(edges = Vote.class, lazy = true, direction = Direction.INBOUND)
      Collection<User> usersWhoVoted;
    

    这里是相关文档:https://www.arangodb.com/docs/3.6/drivers/spring-data-reference-mapping-relations.html

    【讨论】:

    • 感谢您的回复。我应该得到 Vote 对象,然后可以按是否支持进行分组,但我认为它不是这样工作的。我现在需要为此创建投票存储库。
    • 还可以在 Post 类中使用 @From Collection&lt;Vote&gt; votes; 获取所有选票。
    【解决方案2】:

    要直接使用边而不是直接使用连接的元素(在本例中为 votes),您可以将 Post 文档更改为:

    @Document("posts")
    @Getter @Setter @NoArgsConstructor
    public class Post {
    
        @Id
        String id;
    
        // Change this:
        // @Relations(edges = Vote.class, lazy = true, direction = Direction.INBOUND)
        // To this:
        @From(lazy = true)
        Collection<Vote> votes;
        @Ref
        User writtenBy;
    
        String content;
        List<String> externalLinks;
        List<String> internalLinks;
    
        public Post(String content) {
            super();
            this.content = content;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      相关资源
      最近更新 更多