我有2个读者和书籍实体,并且都有数据库中的表格。我还有读者和书籍的ReadersBooks表。
代码中的实体:
funcall
所以读者是所有者。我希望读者可以“喜欢”和“不像”一本书,所以我写了那段代码:
public class Reader implements Serializable {
// More fields.....
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@JoinTable(name = "ReadersBooks", joinColumns = {
@JoinColumn(name = "READER_ID", referencedColumnName = "ID")}, inverseJoinColumns = {
@JoinColumn(name = "BOOK_ID", referencedColumnName = "ID")})
@ManyToMany(cascade = CascadeType.ALL)
private Collection<Book> likesCollection;
// More getters and setters.....
}
public class Book implements Serializable {
// More fields.....
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@ManyToMany(mappedBy = "likesCollection")
private Collection<Reader> likesReadersCollection;
// More getters and setters.....
}
如果我试图用读者“喜欢”书,读者会正确更新,我可以在ReadersBooks表中看到一个新行。但是当我加载一些书时:
Collection<Book> likes = getCurrentReader().getLikesCollection();
if (likes.contains(book)) {
likes.remove(book);
} else {
likes.add(book);
}
readersFacade.edit(getCurrentReader());
它返回0.
在“不像”后,阅读器正确更新,并且还有ReadersBooks表,但书的结果保持为0。
那么为什么图书实体不会更新?我希望Reader将成为所有者,而Reader的集合中的每个更改都会影响Book实体。