I need help with storing a map in JPA2, where both keys and values are enums (Map<Enum, Enum>). With Hibernate as my JPA provider it stores the enums as a blob but I need the data stored as strings. I tried the following annotations to fix this problem:
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyEnumerated(value = EnumType.STRING)
public Map<Enum, Enum> getElementsMap() {
return elementsMap;
}
But the data is still being stored into the DB as blob. Has anyone solved this problem?
2 Answers 2
@Enumerated is used to to define type for value. Following maps to table where column for both key and value are varchars and name of the enum will be saved:
@Enumerated(EnumType.STRING)
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyEnumerated(value = EnumType.STRING)
public Map<MyEnum, MyOtherEnum> elementsMap = new HashMap<>();
It will produce roughly following table:
[NAME_OF_ENTITY]_ELEMENTSMAP (
NAME_OF_ENTITY_ID INTEGER,
ELEMENTSMAP VARCHAR(255),
ELEMENTSMAP_KEY VARCHAR(255)
)
1 Comment
Almost each and every Java Object has a toString() method If you want to represent your Map in the database, then I suggest this be your option.
However I have to ask are you sure it is the MAP you wish to store and not the elements of the keys or values?
1 Comment
Explore related questions
See similar questions with these tags.
toString()on both the keys and values. What's the problem?