有多个外键时如何直接映射多图美?

时间:2019-07-05 04:32:03

标签: java hibernate many-to-many

enter image description here

我与上图有关系,我试图将它们映射为直接抛出表user_role_project,如下所示。

class user {
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "tbl_user_role_project",
        joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
private Set<Role> roles;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "tbl_user_role_project",
        joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "project_id", referencedColumnName = "id"))
private Set<Project> projects;
}

class project {
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "tbl_user_role_project",
        joinColumns = @JoinColumn(name = "project_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private Set<User> users;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "tbl_user_role_project",
        joinColumns = @JoinColumn(name = "project_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
private Set<Role> roles;
}

与表角色相同。 从表项目中,我可以使所有用户和角色成功,但是在尝试添加新项目时出现问题。

不是添加新项目,而是 将新的一个关系添加到表user_role_project 中。抛出异常是因为它试图将 两行插入到表user_role_project 中。

enter image description here

添加项目后预期。

enter image description here

问:我如何映射manyTomany三个表user,role,project而没有表user_role_project? 非常感谢。

0 个答案:

没有答案