hibernate - Key & value column name overrides when mapping java.util.Map with JPA annotations -


i looking @ different ways of annotating maps using hibernate 4.1.9 & jpa annotations.

if want store map key attribute of entity value mark looks this

    @onetomany(mappedby = "deptbyid", targetentity = com.demo.impls.employee.class)     @mapkey(name = "entityid")     private map<long, employee> employeesbyid; 

note above mark not create join table map returned via query @ run-time, map dynamic , not have add element map in java them returned query.

now want contents of map reflect application has added map rather performing dynamic query.

there 4 varieties of map want store

    private map<string, string> map0;     private map<string, entity> map1;     private map<entity, string> map2;     private map<entity, entity> map3; 

in these cases there no relationship between key & , value nor there relationship holding entity. have able specify name of join table column names key & value.

i have tried following

@entity public class department {     @elementcollection     @collectiontable(name = "test_map0")     @column(name="value")     @mapkeycolumn(name="key")     private map<string, string> map0;      @elementcollection(targetclass = com.demo.bb.impls.employee.class)     @collectiontable(name = "test_map1")     @column(name="value")     @mapkeycolumn(name="key")     private map<string, employee> map1;      @elementcollection     @mapkeyclass(value = com.demo.bb.impls.employee.class)     @collectiontable(name = "test_map2")     @column(name="value")     @mapkeycolumn(name="key")     private map<employee, string> map2;      @elementcollection(targetclass = com.demo.bb.impls.parkingspace.class)     @mapkeyclass(value = com.demo.bb.impls.employee.class)     @collectiontable(name = "test_map3")     @column(name="value")     @mapkeycolumn(name="key")     private map<employee, parkingspace> map3; 

case 0 map works fine & generated join table has columns department, value, key

the other 3 cases work in as can store data in tables & in java interrogate tables relevant keys/values & expected results - i.e. handle storing entities using @elementcollection

but column name overrides using @column(name="value") & @mapkeycolumn(name="key") ignored when key or value entity.

i have tried using @manytomany annotations follows

    @manytomany(targetentity = com.demo.bb.impls.employee.class)     @jointable(name = "test_map1_b")     @column(name="value")     @mapkeycolumn(name="key")     private map<string, employee> map1_b;      @manytomany(targetentity = com.demo.bb.impls.parkingspace.class)     @mapkeyclass(value = com.demo.bb.impls.employee.class)     @jointable(name = "test_map3_b")     @column(name="value")     @mapkeycolumn(name="key")     private map<employee, parkingspace> map3_b; 

but again key & value column names overrides ignored. know of way enforce these column name overrides.

thanks in advance...

update....after looking @ response @wypieprz think know correct annotation allow specify column names value & key when map keyed basic entity value.

by using following

    @manytomany(targetentity = com.demo.bb.impls.employee.class)     @jointable(name = "test_map1", inversejoincolumns=@joincolumn(name="value"))     @mapkeycolumn(name="key")     private map<string, employee> map1; 

using inversejoincolumn can specify value column name.

but if key entity have not found way specify key column name. doc @mapkeycolumn "specifies mapping key column of map map key basic type"

i not sure of annotations use when key entity & value basic. using manytomany not work & think may have use elementcollection again cannot find way specify key column name.

update 2... peter halicky solution.

in summary name 3 columns on each of cases need this.

@elementcollection @collectiontable(name = "test_map0", joincolumns = @joincolumn(name = "department")) @column(name = "value") @mapkeycolumn(name = "key") private map<string, string> map0;  @manytomany(targetentity = com.hibernate.elephants.employee.class) @jointable(name = "test_map1", joincolumns = @joincolumn(name = "department"), inversejoincolumns = @joincolumn(name = "value")) @mapkeycolumn(name = "key") private map<string, employee> map1;  @elementcollection @collectiontable(name = "test_map2", joincolumns = @joincolumn(name = "department")) @mapkeyclass(value = com.hibernate.elephants.employee.class) @mapkeyjoincolumn(name = "key") @column(name = "value") private map<employee, string> map2;  @manytomany(targetentity = com.hibernate.elephants.parkingspace.class) @jointable(name = "test_map3", joincolumns = @joincolumn(name = "department"), inversejoincolumns = @joincolumn(name = "value")) @mapkeyclass(value = com.hibernate.elephants.employee.class) @mapkeyjoincolumn(name="key") private map<employee, com.hibernate.elephants.parkingspace> map3; 

note 2 cases specified elementcollection 2 cases value entity need use manytomany.

i'm using entity key map, below. using @mapkeyjoincolumn annotation specify name of column key of map. worked me on hibernate, not sure other jpa implementations do, surely worth trying.

@elementcollection @collectiontable(name="breed_descriptions", joincolumns={ @joincolumn(name="breed") }) @column(name="description") @mapkeyjoincolumn(name="language") private map<language, string> descriptions = new hashmap<>(); 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -