我想创建 M:N 关系如下
每个用户可以拥有零个或多个电子书
每本电子书必须属于一个或多个用户
我在 Hibernate 中的映射:
用户.java
@Entity
@Table(name = "USERS")
public class User {
//...
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "USER_EBOOK", joinColumns = @JoinColumn(name = "USER_ID", nullable = false),
inverseJoinColumns = @JoinColumn(name = "EBOOK_ID", nullable = false))
private List<Ebook> listOfEbooks = new ArrayList<Ebook>();
//...
}
电子书.java
@Entity
@Table(name="EBOOK")
public class Ebook {
//...
@ManyToMany(mappedBy = "listOfEbooks", fetch = FetchType.EAGER)
@NotFound(action = NotFoundAction.EXCEPTION)
private List<User> listOfEbookUsers = new ArrayList<User>();
//...
}
如何添加此附加约束,例如一个或多个 - 零个或多个?,当我仅将电子书对象保存到数据库时,存在不属于任何人的电子书。