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

f0brbegy  于 2022-12-09  发布在  Go
关注(0)|答案(2)|浏览(119)

我刚开始用ArangoDB及其Spring Data 库实现一个业余项目。
我创建了两个名为User和Post的文档,并创建了一个名为Vote的边。
我在Vote上有一个不同于then _from和_to的自定义属性。我可以使用该自定义属性保存该边,也可以从ArangoDB ui中查看它。但我无法使用Java对象检索该属性。
我的环境:

  • ArangoDB版本:3.6.3
  • arangodb-spring-数据版本: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;
  }

}
woobm2wo

woobm2wo1#

@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

qco9c6ql

qco9c6ql2#

若要直接处理边而不是直接处理连接的元素(在本例中为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;
    }
}

相关问题