I have two classes of objects "User" and "Document". Each user has incoming and outgoing, which are actually two lists, and each document can be in several mailboxes and outgoing from users. Here are my classes:
@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>();
}
When I run the program and try to assign the document to the inbox (and vice versa), I get the following error:
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 (?, ?)")
The generated association table is as follows:
DOCUMENT_USER
useroutbox_ID | outbox_ID |userinbox_ID | inbox_ID
How do I set defaults for this many-to-many relationship? Would it be better to make two association tables → one for inbox-relation, the other for outgoing relationships? How can i do this? Other solutions to this problem?
- !