我有以下表格:
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)
答案 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