I have two classes, each containing a Map<String,String>, which I want to persist. One like this works:
Code:
@Entity(name = "foo")
public class Foo implements Serializable {
@Id
@Column(name = "id")
private String id;
@CollectionOfElements(fetch=FetchType.EAGER)
@JoinTable( name = "foo_map", joinColumns = {@JoinColumn(name = "foo_id")})
@Column( name = "value", nullable = false )
@org.hibernate.annotations.MapKey( columns={ @Column(name="name" ) } )
private Map<String, String> map = new HashMap<String, String>();
}
i.e. I can create an object, populate its map, and then call entityManager.persist() on the object, and the object and the contents of the map are correctly persisted to the tables "foo" and "foo_map".
Now, if I do exactly the same thing on this class:
Code:
@Entity(name = "bar")
public class Bar implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@CollectionOfElements(fetch = FetchType.EAGER)
@JoinTable( name = "bar_map", joinColumns = {@JoinColumn(name = "bar_id")})
@Column( name = "value", nullable = false )
@org.hibernate.annotations.MapKey( columns={ @Column(name="name" ) } )
private Map<String, String> map = new HashMap<String, String>();
}
persistence of the map simply does not work. The object itself is persisted (to the "bar" table) but the contents of the map are never persisted.
If I compare the logs, in the working example I see an insert into the "foo" table followed by inserts into the "foo_map" table. In the nonworking example there's an insert for the "bar" table, but no inserts for the "bar_map" table.
The bar_map table is created correctly by Hibernate, and retrieving objects shows me queries on both the bar and bar_map tables.
The only obvious difference I can see is that the Bar class has an autogenerated int id where the Foo class has an assigned string one.