I'm trying to create an entity where one of the field is a Map with Enum key:
public class MyEntity {
@ElementCollection
@CollectionTable(name="attributes", joinColumns=@JoinColumn(name="my_entity_id"))
@MapKeyColumn(name = "attribute_key")
@Column(name="attribute_value")
private Map<Attribute, String> attributes;
}
The Attribute
is just a simple enumeration with no additional fields or logic:
public enum Attribute {
ATTRIBUTE1, ATTRIBUTE2, ATTRIBUTE3;
}
This maps nicely and do work. But the collection table, attributes
creates with integer
column definition for my map key, as the default is EnumType.ORDINAL
. For my purposes I need it to be string, but I can't just place @Enumerated(EnumType.STRING)
on my field as this leads to an exception.
Do I have any options of how can I achieve this desired behaviour? Thanks very much.
Kudos to @BilalBOUTAYA
The answer is: use @MapKeyEnumerated
.
The @Enumerated
annotation applies to the value column which is obviously incompatible with the annotation.
Example:
@JsonIgnore
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "FTT_REGISTRI_ESCLUSIONI", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "FTT_FK_ESCLUSIONE_TO_REGISTRO"), joinColumns = @JoinColumn(name = "REGISTRO_ID"))
@MapKeyColumn(name = "CLAUSOLA_ESCLUSIONE", length = 40, nullable = false)
@MapKeyClass(FttEsclusioneType.class)
@MapKeyEnumerated(EnumType.STRING)
@Column(name = "RECORD_COUNT", nullable = false)
protected final Map<FttEsclusioneType, Long> esclusioneRecordCounters = new HashMap<>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With