如何在hibernate注释中映射三个表

时间:2017-07-22 15:00:17

标签: java spring hibernate spring-mvc hibernate-annotations

我的问题与下面的问题非常相似。

Hibernate : How to Join 3 tables in one join table by Annotation?

由于我没有回答,我在这里再次提问。 我有用户,角色,应用程序表。如何在注释中映射它。我尝试过如下。

@JoinTable(name = "role_application_user",
           joinColumns = @JoinColumn(name = "application_identifier"),
           inverseJoinColumns = @JoinColumn(name = "user_identifier"))
@MapKeyJoinColumn(name = "role_identifier")
@ElementCollection
private Map<Role, User> userByRole = new HashMap<>();

但是由此我无法分配多个应用程序/角色访问权限。我需要有所有可能的选项来分配。我在这里做错了什么?

我想在最终的连接表中使用这样的东西。

UserID RoleID ApplicationID
1      1      1
1      1      2
1      1      3
1      2      1
1      3      1
2      1      1

这是我的实体类

User.java

Class User{

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "CTC_AC_USER_INSTITUTION", joinColumns = {@JoinColumn(name = "userId")}, inverseJoinColumns = {@JoinColumn(name = "institutionId")})
private Set<Institution> institutions = new HashSet<Institution>();

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "CTC_AC_USER_USER_PROFILE", joinColumns = {@JoinColumn(name = "userId")}, inverseJoinColumns = {@JoinColumn(name = "userProfileId")})
private Set<UserProfile> userProfiles = new HashSet<UserProfile>();

}

UserProfile.java

Class UserProfile {
@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "type", length = 25, unique = true, nullable = false)
    @NotBlank
    private String type = UserProfileType.USER.getUserProfileType();
}

Institution.java

Class Institution{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Length(max = 500)
    @Column(name = "name", nullable = false)
    private String name;
}

0 个答案:

没有答案