gpt4 book ai didi

java - hibernate : How to Join 3 tables in one join table by Annotation?

转载 作者:太空宇宙 更新时间:2023-11-04 12:23:34 25 4
gpt4 key购买 nike

我有一些角色、用户和应用程序我想创建该表的映射 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;
}

关于java - hibernate : How to Join 3 tables in one join table by Annotation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38591170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com