Join fields from from related table into Map
Hello I have two tables: roles and permissions. Roles have name and other things. Permissions have id, idrole, string sname and boolean bcanmo开发者_开发知识库dify. (Relation many to one between role and permission)
I would like to have only one Hibernatee entity class for Role and have there a map where keys would be sname values from reated permissions and values would be bcanmodify values. Is that possible? Which annotations should I use?
the below way works for me and I believe meets ur requirements; the below code goes in the Entity Role and you need not have an entity Permissions defined. But keep in mind the column you want to be used as the map key has to have unique constraint defined in the table, like here you got to have sname values unique in permissions table
@ElementCollection
@CollectionTable(name="permissions",joinColumns= @JoinColumn(name="role_id",referencedColumnName="role_id")})
@MapKeyColumn(name="sname")
@Column(name="bcanmodify")
private Map<String,Boolean> permissions;
AFAIK, the closest you can do is to have a Map<String, Permission>
in your Role
entity.
@OneToMany(mappedBy = "role")
@MapKey(name = "name")
private Map<String, Permission> permissions;
精彩评论