我有一些角色、用户和应用程序我想创建该表的映射 hibernate :
CREATE TABLE role_application_user (
role_identifier INTEGER not null,
application_identifier INTEGER not null,
user_identifier INTEGER not null,
KEY FK_role_identifier (role_identifier),
KEY FK_application_identifier(application_identifier),
KEY FK_user_identifier (user_identifier),
CONSTRAINT FK_role_identifier FOREIGN KEY (role_identifier) REFERENCES role (identifier),
CONSTRAINT FK_application_identifier FOREIGN KEY (application_identifier) REFERENCES application (identifier),
CONSTRAINT FK_user_identifier FOREIGN KEY (user_identifier) REFERENCES users (login)
);
对于应用程序,一个角色可以拥有多个用户,一个用户可以拥有多个角色。
我尝试这个映射:
应用程序.java
@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<>();
不幸的是,这在我的例子中不起作用,因为在 java 中,Map 的键必须是唯一的。
通过此映射,我们只能让一个用户担任一个角色和一个应用程序。
尝试这个实现:
@Entity
public class User {
@OneToMany
private List<RoleInApplication> rolesInApplications;
}
@Entity
public class Role {
@OneToMany
private List<RoleInApplication> rolesInApplications;
}
@Entity
public class RoleInApplication {
@ManyToOne
private User user;
@ManyToOne
private Role role;
@ManyToOne
private Application application;
}
@Entity
public class Application {
@OneToMany
private List<RoleInApplication> rolesInApplications;
}
我是一名优秀的程序员,十分优秀!