hibernate_boolean類型的處理

來源:互聯網
上載者:User

標籤:

xml方式,直接寫就行,hibernate會直接幫你產生:

javaBean程式碼片段:

private boolean leaf;    public boolean isLeaf() {        return leaf;    }    public void setLeaf(boolean leaf) {        this.leaf = leaf;    }

hbm.xml配置直接寫就行:

<property name="leaf"></property>

測試裡代碼:

//s.setLeaf(true);s.setLeaf(false);

資料庫結果:true會存入1,false存入0。

Annotation方式:

定義boolean類型屬性為:private  boolean   isLeave;

JavaBean程式碼片段:

boolean類型自動的get、set為:

private boolean isLeave;
public
boolean isLeave() { return isLeave; } public void setLeave(boolean isLeave) { this.isLeave = isLeave; }

這樣hibernate是不認識的,hibernate裡,屬性名稱必須和set、get方法名一致,所以需要修改方法名:

    private boolean isLeave;        /**     * 如果不寫@Type(type="yes_no")預設存入true  1 false  0     * @return     */    //修改自動產生的get方法isLeave()為getIsLeave    @Type(type="yes_no")    public boolean getIsLeave() {        return isLeave;    }    //把自動產生的set方法:setLeave改為:setIsLeave    public void setIsLeave(boolean isLeave) {        this.isLeave = isLeave;    }

測試程式碼片段:

//t.setIsLeave(false);t.setIsLeave(true);

資料庫結果:0、1是預設時的結果,Y、N是@Type(type="yes_no")的結果:

Ok。

hibernate_boolean類型的處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.