How to add a Map in an entity class?

后端 未结 1 1359
情话喂你
情话喂你 2020-12-28 08:42

I want to add a mapping as

Map personMap;

inside an entity class, where Person is the entity. The

相关标签:
1条回答
  • 2020-12-28 09:07

    As per section 2.7 of JSR-317, if the value of the Map is an entity (which is your case) a join table is created and then a OneToMany / ManyToOne annotation should be used.

    As for the key, if it is a Basic Type, the @MapKeyColumn can be used to customize the mapping column of the key. So here is my take on your example:

    @OneToMany
    @MapKeyColumn(name="person_nickname")
    Map<String, Person> personMap;
    

    EDITED:

    After some testing, the following seems to work pretty well:

    @ElementCollection
    @CollectionTable(name="<name_of_join_table>")
    @MapKeyColumn(name="<name_of_map_key_in_table>")
    Map<String, Person> personMap;
    

    The above generates a join table with three fields: one for the mapping holder id, one for the key and one for the value.

    0 讨论(0)
提交回复
热议问题