- enum값을 DB에 넣을 시 자동으로 value값으로 넣어주는 converter
public enum CarType {
SUV("1"), CUV("2"), MPV("3"), RV("4");
private String value;
@Converter(autoApply = true)
public static class CarTypeConverter extends AbstractBaseEnumConverter<CarType, String>{
@Override
protected CarType[] getValueList() {
return CarType.values();
}
}
}
- class의 Object 값을 DB에 원하는 형태로 저장해 주는 converter
@Getter
@Builder
public class Receipt {
private ReceiptType receiptType;
//receiptType이 SUCCESS일때만 requestId가 존재
private Integer requestId;
@Converter(autoApply = true)
public static class ReceiptConverter implements AttributeConverter<Receipt, String> {
@Override
public String convertToDataBaseColumn(Receipt receipt) {
final String prefix = " (REQUEST ID : ";
final String postfix = ")";
String returnString = receipt.getReceiptType().name();
if(receipt.getRequestId() != null) {
returnString += prefix + receipt.getRequestId() + postfix;
}
return returnString;
}
@Override
public Receipt convertToEntityAttrubute(String data) {
if(data.contains(ReceiptType.SUCCESS.name())) {
final String splitPattern = "\\(";
final String replacePattern = "[^0-9]";
String []arr = data.split(splitPattern);
return Receipt.builder()
.receiptType(ReceiptType.SUCCESS)
.requestId(Integer.valueOf(arr[1].replaceAll(replacePattern, "")))
.build();
}
return Receipt.builder()
.receiptType(ReceiptType.valueOf(data))
.build();
}
}
public enum ReceiptType {
CANCEL, SUCCESS, LIMIT_EXCESS
}
}