Marcin Majewski
Marcin Majewski

Reputation: 1047

Hibernate and maps with enums

I have map in my entity:

private Map<String, MyEnum> myMap = new HashMap<>();

where MyEnum is enum. How to correctly annotate this in hibernate? I would like to have one table (and of course as this is one to many, one additional table that stores foreign keys):

+-----+--------+--------+
| key | string | MyEnum |
+-----+--------+--------+
|   1 | banana | PERU   |
|   2 | orange | BRAZIL |
+-----+--------+--------+

Upvotes: 1

Views: 109

Answers (1)

pL4Gu33
pL4Gu33

Reputation: 2095

Use this:

@Enumerated(EnumType.STRING)
@ElementCollection
private Map<String, MyEnum> names = new HashMap<>();

Result:

FKID, ENUM(As String) , STRING

Upvotes: 1

Related Questions