I have a spring boot application that designates an AttributeConverter
for an entity that converts an enum from uppercase to title case for storage in the database.
I have the following entity:
@Entity
@Table(name = "customerleads")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class CustomerLead implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Enumerated(EnumType.STRING)
@Column(name = "type")
@Convert(converter = CustomerLeadTypeConverter.class)
private CustomerLeadType type = CustomerLeadType.OPEN;
}
And the following AttributeConverter class:
@Converter(autoApply = true)
public class CustomerLeadTypeConverter implements AttributeConverter<CustomerLeadType, String> {
@Override
public String convertToDatabaseColumn(CustomerLeadType attribute) {
switch (attribute) {
case OPEN:
return "Open";
case CLOSED:
return "Closed";
case DELETED:
return "Deleted";
default:
throw new IllegalArgumentException("Unknown" + attribute);
}
}
@Override
public CustomerLeadType convertToEntityAttribute(String dbData) {
switch (dbData) {
case "Open":
return OPEN;
case "Closed":
return CLOSED;
case "Deleted":
return DELETED;
default:
throw new IllegalArgumentException("Unknown" + dbData);
}
}
}
Neither @Converter(autoApply = true)
nor the @Convert(converter = CustomerLeadTypeConverter.class)
seem to trigger the conversion.