Is it possible to do type conversion (from boolean to yes_no) in pure JPA?

前端 未结 4 863
醉梦人生
醉梦人生 2020-12-31 22:17

There is an annotation in Hibernate that can persist boolean types as \'Y\'/\'N\' in the database.

https://stackoverflow.com/questions/1154833/configure-hibernate-u

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 22:43

    You can use like this

    @Entity
    public class Employee {
    @Convert(converter=BooleanTFConverter.class)
    private Boolean isActive;
    }
    
    @Converter
    public class BooleanYNConverter implements AttributeConverter{
    @Override
    public String convertToDatabaseColumn(Boolean value) {
        if (value) {
            return "Y";
        } else {
            return "N";
        }
    }
    @Override
    public Boolean convertToEntityAttribute(String value) {
        return "Y".equals(value);
    }
    

    }

提交回复
热议问题