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

前端 未结 4 892
醉梦人生
醉梦人生 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: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);
    }
    

提交回复
热议问题