This entity definition results in a runtime exception:
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int id;
@ElementCollection
@Convert(converter = MyStringConverter.class)
private Map<String, String> responsibilities = new HashMap<>();
void put(String key, String value) {
responsibilities.put(key, value);
}
String get(String key) {
return responsibilities.get(key);
}
}
The exception points to the Convert-annotation on the Map:
Indeed, the code in CollectionPropertyHolder explicitly enforces this, and a comment states:
However, this does not appear to be the case: While the standard lists a few cases which require an "attributeName" on a Map, it does not require "attributeName" on a Map of basic types; without "attributeName", the conversion should be applied on the values in the Map. See, e.g., "Example 5" on https://docs.jboss.org/hibernate/jpa/2.2/api/javax/persistence/Convert.html:
(This is identical to Example 5 in the Java TM Persistence API, Version 2.2, Chapter 11.1.10.) One could even go a step further and say that attributeName="value" should be forbidden on Maps with basic type values, since the standard specifies:
(Indeed, Eclipselink will raise an exception when attributeName="value" is specified on a Map with values of a basic type.) |