According to the documentation, to map a <String,Object> map without using a join table, you need to use the @MapKey annotation. You set the name attribute of @MapKey to a field in the value class, and that will be used for the key of the map, like this:
Code:
@Entity
public class BaseClass {
@Id
private int Id;
@OneToMany(mappedBy="base")
@MapKey(name="key")
private Map<String, ValueClass> myMap;
}
@Entity
public class ValueClass {
@ManyToOne
@JoinColumn(name="base_id")
private BaseClass base;
private String key;
{
This works, but now In Java you're storing the key twice, once as the key of the map, and again as a field in the value class. In my case, I'm getting my data from a JSON web service and generating my POJOs with the Jackson JSON parser. Before I save this data to my database with Hibernate, I have to add code that will copy all the keys of all maps to the corresponding key field in the value objects.
This is ugly IMO. Is there a way I can make hibernate automatically add a column for the key in the value class without actually adding a field for it in the POJO? If I don't add the @MapKey annotation, hibernate actually adds a valueclass_KEY column, but it's value is always null.