简体   繁体   English

JPA:两个实体之间存在多个多对多关系?

[英]JPA: Multiple many-to-many relations between two entities?

I have two entity classes 'User' and 'Document'.我有两个实体类“用户”和“文档”。 Each user has an inbox and an outbox which are in fact two List and each Document may reside in multiple inbox's and outbox's of users.每个用户都有一个收件箱和一个发件箱,实际上是两个List,每个Document可能驻留在多个用户的收件箱和发件箱中。 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 programm and try to assign a document to a user's 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 looks like this:生成的关联表如下所示:

DOCUMENT_USER
useroutbox_ID | outbox_ID |userinbox_ID | inbox_ID

How would I assign default values for such a many-to-many relation?我将如何为这种多对多关系分配默认值? Would it be better to make two association tables -> one for inbox-relation, another for outbox-relation?制作两个关联表会更好吗 -> 一个用于收件箱关系,另一个用于发件箱关系? How would I accomplish that?我将如何做到这一点? Other solutions to this problem?这个问题的其他解决方案?

Any help highly appreciated - many thanks in advance!非常感谢任何帮助 - 非常感谢!

I think that the better option is to have two separate tables, one per relation.我认为更好的选择是有两个单独的表,每个关系一个。 Because you actually have two relations between two different entities, and not one relation with four different entities.因为您实际上在两个不同实体之间有两种关系,而不是与四个不同实体之间的一种关系。

So, you should add a @JoinTable annotation to each of your attributes in the Document side since the User side those relations are mapped to a property.因此,您应该为Document端的每个属性添加一个@JoinTable注释,因为在User端,这些关系被映射到一个属性。 Something like the following:类似于以下内容:

@Entity
public class Document {

    @Id
    private Long id;

    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name = "document_inbox", joinColumns = @JoinColumn(name = "userinbox_id"),
               inverseJoinColumns = @JoinColumn(name = "inbox_id"))
    private List<User> userinbox  = new ArrayList<User>();
    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name = "document_outbox", joinColumns = @JoinColumn(name = "useroutbox_id"),
               inverseJoinColumns = @JoinColumn(name = "outbox_id"))
    private List<User> useroutbox  = new ArrayList<User>();

}

Leave the other entity as it is now.让另一个实体保持原样。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM