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

前端 未结 4 862
醉梦人生
醉梦人生 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

    Pure JPA without Hibernate is achieved by using some kind of conversion

    private boolean enabled;
    
    @Transient
    public boolean isEnabled() {
        return this.enabled;
    }
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    
    @Column(name="ENABLED")
    public String getEnabledAsString(){
        return enabled ? "Y" : "N";
    }
    
    public void setEnabledAsString(String enabled){
        this.enabled = "Y".equalsIgnoreCase(enabled);
    }
    

    Nothing else

    0 讨论(0)
  • 2020-12-31 22:43

    This is pure JPA without using getters/setters, so it answers the question:

    @Entity
    public class Person {    
    
        @Convert(converter=BooleanToStringConverter.class)
        private Boolean isAlive;    
        ...
    }
    

    And then:

    @Converter
    public class BooleanToStringConverter implements AttributeConverter<Boolean, String> {
    
        @Override
        public String convertToDatabaseColumn(Boolean value) {        
            return (value == null || !value) ? "N" : "Y";            
            }    
    
        @Override
        public Boolean convertToEntityAttribute(String value) {
            return "Y".equals(value);
            }
        }
    

    Please note this solution is JPA 2.1, and was not available when the question was first asked: The JPA 2.1 specification was released 22 April 2013.

    0 讨论(0)
  • 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<Boolean, String>{
    @Override
    public String convertToDatabaseColumn(Boolean value) {
        if (value) {
            return "Y";
        } else {
            return "N";
        }
    }
    @Override
    public Boolean convertToEntityAttribute(String value) {
        return "Y".equals(value);
    }
    

    }

    0 讨论(0)
  • 2020-12-31 22:51

    Similar to the above (@Arthur Ronald F D Garcia), but you can also use JPA field access and leave the ivar in the type of the database with transient accessors - marking them @Transient. This ensures JPA acces the entity by field access but leaves the accessors available for appropriately typed usage.

    Using the above example:

    @Column(name="isconstrained")
    private int isConstrained;
    
    @Transient
    public boolean getIsConstrained() {
        return (isConstrained == 1);
    }
    
    @Transient
    public void setIsConstrained(boolean isConstrained) {
        this.isConstrained = (isConstrained? 1 : 0);
    }
    
    0 讨论(0)
提交回复
热议问题