Hibernate:如何通过注释在一个联接表中联接3个表? [英] Hibernate : How to Join 3 tables in one join table by Annotation?
本文介绍了Hibernate:如何通过注释在一个联接表中联接3个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些角色,用户和应用程序 我想创建该表的映射休眠模式:
I have some roles, users and applications I want to create a mapping hibernate of this table :
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)
);
对于一个应用程序,一个角色可以有许多用户,而一个用户可以有许多角色.
for an application, a role can have many users and a user can of many roles.
我尝试此映射:
Application.java
Application.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的键必须是唯一的.
Unfortunately, this is not working in my case because in java, the key of a Map must be unique.
通过这种映射,我们只有一个角色和一个应用程序的用户.
With this mapping, we can have only one user for a role and an application.
推荐答案
尝试此实现:
@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;
}
这篇关于Hibernate:如何通过注释在一个联接表中联接3个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文