如何使用 Spring Data ArangoDB 检索边上的附加属性

How to retrieve additional attribute on edge with Spring Data ArangoDB

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

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

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

我的环境:

我的类是这些:

@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;
  }

}

@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

要直接使用边缘而不是直接连接元素(在本例中,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;
    }
}