How do you map a “Map” in hibernate using annotations?

You could simply use the JPA annotation @MapKey (note that the JPA annotation is different from the Hibernate one, the Hibernate @MapKey maps a database column holding the map key, while the JPA’s annotation maps the property to be used as the map’s key).

@javax.persistence.OneToMany(cascade = CascadeType.ALL)
@javax.persistence.MapKey(name = "name")
private Map<String, Person> nameToPerson = new HashMap<String, Person>();

Leave a Comment