I have an entity like below:
@Entity
@Table(name = "orders")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Order {
@Convert(converter = UserInfoJsonConverter.class)
private UserInfo userInfo;
@Convert(converter = IndUserInfoJsonConverter.class)
private IndUserInfo indUserInfo;
@Convert(converter = DuplicateInfoJsonConverter.class)
private DuplicateInfo duplicateInfo;
@Convert(converter = PartnershipJsonConverter.class)
private PartnershipInfo partnerShipInfo;
@Convert(converter = SecretJsonConverter.class)
private SecretInfo secretInfo;
....
And as you see I have 5 fields as JSON string in DB. And also I have 5 JsonConverter implementations like below:
@Slf4j
@Converter(autoApply = true)
public class IndUserInfoJsonConverter implements AttributeConverter<IndUserInfo, String> {
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public String convertToDatabaseColumn(IndUserInfo attribute) {
try {
objectMapper.registerModule(new JavaTimeModule());
return objectMapper.writeValueAsString(attribute);
} catch (JsonProcessingException ex) {
log.error("Could not write view {} to json", attribute, ex);
throw new InternalServerErrorException("Could not write the object details into string", ex);
}
}
@Override
public IndUserInfo convertToEntityAttribute(String dbData) {
try {
objectMapper.registerModule(new JavaTimeModule());
return dbData == null ? null : objectMapper.readValue(dbData, IndUserInfo.class);
} catch (JsonProcessingException ex) {
log.error("Could not write view {} to json", dbData, ex);
throw new InternalServerErrorException("Could not convert input string into object", ex);
}
}
}
And my question is how can I generalize these implementations? I mean how can I get rid of code repeat? Because the only difference is IndUserInfo in here.