更新与桥表连接的表的元素

时间:2017-08-18 01:36:18

标签: java playframework ebean playframework-2.6

我有以下表格:

BOOK: id,name
USER: id,username,status
BOOK_USER: book_id, user_id

我想在我的应用程序中使用功能,用户可以设置阅读状态。例。开始阅读停止阅读。

在我的Book.java中,我将USER表作为ManyToMany

引入
  @JsonIgnore
  @ManyToMany
  @JoinTable(name = "user",
      joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"),
      inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
  public List<User> users;

和我的User.java

  @ManyToMany(mappedBy = "users")
  public List<Book> bookList;

当用户在开始阅读时设置停止读数时,我想更新用户表。从我的BookController.java更新方法调用此方法。

  public void changeReadingStatus() {
    for (User user : users) {
      user.setStatus("Finished Reading");
      user.update();
    }
  }

我收到错误

Caused by: io.ebean.DataIntegrityException: Error when batch flush on sql: insert into BOOK_USER (book_id, user_id) values (?, ?)
        at io.ebean.config.dbplatform.SqlCodeTranslator.translate(SqlCodeTranslator.java:44)

1 个答案:

答案 0 :(得分:0)

我认为你应该这样配置

@ManyToMany
@JoinTable(name = "BOOK_USER",
    joinColumns = @JoinColumn(name = "book_id"),
    inverseJoinColumns = @JoinColumn(name = "user_id"))
public List<User> users;



@ManyToMany(mappedBy = "users")
public List<Book> bookList;

更多信息here