1. 程式人生 > >10-hibernate單表操作-組件屬性

10-hibernate單表操作-組件屬性

http -h idt wid 文件 技術 set string alt

組件屬性:

實體類中某個屬性屬於用戶自定義的類的對象,比如在實體類中某個屬性是自定義類的對象:

技術分享

這個Address是一個用戶自定義類。

該自定義類Address定義如下:

//地址類
public class Address {
    
    private String postcode;//郵編
    private String phone;//電話
    private String address;//地址
    
    public Address()
    {
    }
    public Address(String postcode, String phone, String address) {
        
//super(); this.postcode = postcode; this.phone = phone; this.address = address; } public String getPostcode() { return postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } public String getPhone() {
return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }

實體類中修改如下:

技術分享

Test類中修改如下:

技術分享

標準註冊規則如下:address表示對應實體類中的屬性名,class表示自定義類名,

下面的postcode表示自定義類中的屬性名,表示對應數據庫中字段。

技術分享

在hbm.xml文件中註冊如下:

技術分享

執行完後查詢結果:

技術分享

10-hibernate單表操作-組件屬性