'Use Enum in JPA

I have enum fields in DB in lowercase and as per Java standard, I have to define them in Uppercase in my spring boot application. If I am using lowercase, enums, I am able to get the results, but trying to convert DB lowercase value to uppercase using Spring attribute converter which is not working. Could someone tell me where I am doing wrong?

AccountLimitType.java

public enum AccountLimitType {
    NONE,
    HARD,
    SOFT;
}

ConvertToDBAccountLimitType.java

@Converter(autoApply = true)
public class ConvertToDBAccountLimitType implements AttributeConverter<AccountLimitType, String>{

    @Override
    public String convertToDatabaseColumn(AccountLimitType attribute) {
        return attribute.toString().toLowerCase();
    }

    @Override
    public AccountLimitType convertToEntityAttribute(String dbData) {
        return AccountLimitType.valueOf(dbData.toUpperCase());
    }

}

Field in Entity class:

    @NotNull
    @Convert(converter = ConvertToDBAccountLimitType.class)
    @Enumerated(EnumType.STRING)
    @Column(name="limit_type",columnDefinition = "ENUM('none','hard','soft') default 'none'")
    private AccountLimitType accountLimitType;

This is the response I am getting: Response in postman



Solution 1:[1]

Can you try without @Enumerated(EnumType.STRING) Also make sure that its calling your converter and don't forget to add a null check there

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tomz