我有两个实体类“用户”和“文档”。每个用户都有一个收件箱和一个发件箱,它们实际上是两个列表,每个文档可能驻留在多个用户的收件箱和发件箱中。这是我的课程:
@Entity
public class User {
@Id
private Long id;
@ManyToMany(mappedBy = "userinbox", cascade=CascadeType.ALL)
private List<Document> inbox = new ArrayList<Document>();
@ManyToMany(mappedBy = "useroutbox", cascade=CascadeType.ALL)
private List<Document> outbox = new ArrayList<Document>();
}
@Entity
public class Document {
@Id
private Long id;
@ManyToMany(cascade=CascadeType.ALL)
private List<User> userinbox = new ArrayList<User>();
@ManyToMany(cascade=CascadeType.ALL)
private List<User> useroutbox = new ArrayList<User>();
}
当我运行程序并尝试将文档分配给用户的收件箱(反之亦然)时,我收到以下错误:
Error Code: 1364
Call: INSERT INTO DOCUMENT_USER (userinbox_ID, inbox_ID) VALUES (?, ?)
bind => [2 parameters bound]
Internal Exception: java.sql.SQLException: Field 'useroutbox_ID' doesn't have a default value
Query: DataModifyQuery(name="userinbox" sql="INSERT INTO DOCUMENT_USER (userinbox_ID, inbox_ID) VALUES (?, ?)")
生成的关联表如下所示:
DOCUMENT_USER
useroutbox_ID | outbox_ID |userinbox_ID | inbox_ID
我将如何为这种多对多关系分配默认值?制作两个关联表会更好吗 - >一个用于收件箱关系,另一个用于发件箱关系?我将如何做到这一点?这个问题的其他解决方案?
任何帮助高度赞赏 - 非常感谢提前!