Spring Boot - Apply persistence Converter Annotation based on a condition

Viewed 17

I have below repository model class

class Model {
    @Column(name="id")
    private static Integer id;

    @Column(name="column-to-be-converted")
    @Convert(converter=Converter.class)
    private static String columnToBeConverted;

    @Column(name="apply-converter")
    private static boolean applyConverter;

    // Getters and Setters 
}

Below is the Converter class

@Component
@Converter
public class PasswordConverter implements AttributeConverter<String, String> {
    
    @Override
    public String convertToDatabaseColumn(String rawData) {
        // logic goes here
    }
    
    @Override
    public String convertToEntityAttribute(String convertedData) {
        // logic goes here
    }

}

I want to apply @Convert annotation to the field columnToBeConverted only if the field applyConverter is set to true

I tried investigating if the model object can be passed to Converter Class as argument or with using @Conditional

Please suggest how can this be achieved

Thank you!

0 Answers
Related