Entity 값과 DB값 간 매핑
- 엔티티의 boolean 값을 DB에 저장시에는 Y, N 값으로 저장하고 싶을때 등 사용.
@Entity
public class aaa {
...
@Convert(converter=BooleanToYnConverter.class)
private boolean isExist;
}
@Converter
//@Converter(autoApply = true) autoApply를 true로 설정하면
//모든 AttributeConverter로 설정한 타입 값을 가진 엔티티 필드 타입에 자동 적용된다.
public class BooleanToYnConverter implements AttributeConverter<Boolean, String> {
@Override
public String convertToDatabaseColumn(boolean attribute) {
return attribute to string code;
}
@Override
public Boolean convertToEntityAttribute(Boolean dbData) {
return String to boolean code;
}
}