I want to add a mapping as
Map personMap;
inside an entity class, where Person
is the entity. The
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.