I’ve looked at a number of similiar questions on Stack Overflow to no avail. I have a pretty simple scenario where I would like to use an AttributeConverter
to convert an Enum to a numeric code in the database. My enum class is:
public enum DispatchStage {
STAGE_0_DISPATCH_INIT(0),
STAGE_25_VERIFIED(25),
STAGE_100_DONE(100);
private final int code;
DispatchStage(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}
Which is a member of this class:
@Data
public class DocumentDispatch {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Convert(converter = DispatchStageConverter.class)
DispatchStage dispatchStage = DispatchStage.STAGE_0_DISPATCH_INIT;
}
And I’ve created the following attribute converter:
@Converter(autoApply = true)
public class DispatchStageConverter implements AttributeConverter<DispatchStage, Integer> {
@Override
public Integer convertToDatabaseColumn(DispatchStage stage) {
System.out.println("USING CONVERTER");
return stage.getCode();
}
@Override
public DispatchStage convertToEntityAttribute(Integer code) {
System.out.println("USING CONVERTER");
for (DispatchStage stage : DispatchStage.values()) {
if (stage.getCode() == code) {
return stage;
}
}
throw new IllegalArgumentException("Unknown code: " + code);
}
}
I have jpa and jakarta.persistence in my pom.xml, and yet the attribute converter is never invoked when I save DocumentDispatch to the DB. The system outs don’t appear in the log, and I get a sql error for trying to save a string to an int field. I’ve tried a number of suggestions and ideas, but there’s no sign of Spring employing my converter at all. Is there some really obvious mistake I’m making, or is there a way to debug this?