簡體   English   中英

JPA,如何將Map中實體類型的值添加到主鍵?

[英]JPA, How to add the value of entity type in Map to primary key?

我有3個實體類,如下所示:-

角色實體

@Entity
public class Role {

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

    @ManyToMany
    @LazyCollection(LazyCollectionOption.FALSE)
    @JoinTable(name = "roles_privileges", joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "privilege_id", referencedColumnName = "id"))
    private Set<Privilege> privileges;

    // getters, setters etc

}

特權實體

@Entity
public class Privilege {

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

    private String name;

    @ManyToMany(mappedBy = "privileges", fetch = FetchType.EAGER)
    @JsonIgnore
    private Set<Role> roles;

    // getters, setters etc

}

UrlsMapper實體

@Entity(name = "urls_mapper")
@Table(name = "urls_mapper")
public class UrlsMapper {

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

    @Enumerated(EnumType.STRING)
    @Column(name = "http_method")
    private HttpMethod httpMethod;

    @Column(name = "path")
    private String path;

    @ManyToMany(fetch = FetchType.EAGER)
    @MapKeyJoinColumn(name = "role_id", referencedColumnName = "id")
    @JoinTable(
        name = "u_r_p",
        inverseJoinColumns = @JoinColumn(name = "privilege_id")
    )
    Map<Role, Privilege> privilegeMap;

   // getters, setters etc

}

創建的主鍵和外鍵如下

在此處輸入圖片說明

表生成時的日志如下:-

Hibernate: create table u_r_p (urls_mapper_id bigint not null, privilege_id bigint not null, role_id bigint not null, primary key (urls_mapper_id, role_id)) engine=InnoDB
Hibernate: alter table u_r_p add constraint FKgd7gd9f9ded1s28swdudqs0ro foreign key (privilege_id) references Privilege (id)
Hibernate: alter table u_r_p add constraint FKrryprkx4j60lyjti16eysn5g5 foreign key (role_id) references Role (id)
Hibernate: alter table u_r_p add constraint FKfkthdnoca59a18ba96183p7ov foreign key (urls_mapper_id) references urls_mapper (id)

我只想知道如何將privilege_id也添加到JoinTable u_r_p中,以及是否還有其他最佳選擇。 手動在數據庫中執行操作是一個明顯的替代方法,但是我想知道基於hbm2ddl.auto的解決方案,以便代碼自行管理

我認為您沒有正確建模概念。 您在RolePriviledge之間有一個ManyToMany ,但是什么使UrlMapper成為實體呢? 您在UrlMapper有一個Map<Role, Privilege>字段,但這是UrlMapper表的目的,因此無需重復該操作。 相反,似乎HttpMethodPath是關系的屬性。

在此處輸入圖片說明

但是,我可能還會注意到,您似乎期望許多不同的HttpMethod / Path組合都具有Role / Privilege連接。 這似乎令人難以置信的細粒度和操作噩夢,但無論如何。 無論如何,您似乎要說的是想要Role / Privilege / HttpMethod / Path的唯一組合,因此您應該為此創建一個實體,並且該表表示您的集合。 創建一個擁有唯一的Role / Privilege / HttpMethod / Path的Permission實體。 Role,Privilege,HttpMethod甚至Path本質上都是枚舉,因此您應該為每個表創建一個表,並在Permission實體中具有ManyToOne映射。 您可以在每個查找表中添加雙向OneToMany映射,但是我不確定是否需OneToMany 由你決定。

在此處輸入圖片說明

我認為Privilege將是{allow,deny},但是如果您拒絕,除非角色/ HttpMethod / Path權限明確存在,否則似乎有點糾結。 如果是這種情況,那么我將忽略“ Privilege實體。 無論如何,只是一個想法。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM