---
title: "[Hibernate单表操作] 组件属性"
author: "cakin24"
date: "2017-11-08T19:26:04Z"
tags: []
categories: ["编程语言"]
slug: "hibernate"
draft: false
lastmod: "2017-11-08T19:26:04Z"
id: 2399097
url: "https://cakin24.iteye.com/blog/2399097"
---

一 组件属性定义

实体类中某个属性属于用于自定义的类的对象。

![](http://dl2.iteye.com/upload/attachment/0127/7341/0a77da63-8c8f-3811-86b1-725d6b599bb8.png)  
 

 

二 组件属性应用

1、Address

public class Address {

        private String postcode; //邮编

        private String photo;      //电话

        private String address; //地址

        

        public Address()

        {

                

        }

 

        public Address(String postcode, String photo, String address) {

                this.postcode = postcode;

                this.photo = photo;

                this.address = address;

        }

 

        public String getPostcode() {

                return postcode;

        }

 

        public void setPostcode(String postcode) {

                this.postcode = postcode;

        }

 

        public String getPhoto() {

                return photo;

        }

 

        public void setPhoto(String photo) {

                this.photo = photo;

        }

 

        public String getAddress() {

                return address;

        }

 

        public void setAddress(String address) {

                this.address = address;

        }

        

}

2、Students

import java.sql.Blob;

import java.util.Date;

//学生类

public class Students {

    /*

     * JavaBeans的四点原则

     * 1.必须是公有的类

     * 2.提供公有的不带参数的默认的构造方法

     * 3.属性私有

     * 4.属性setter/getter封装

     \*/

    private int sid; // 学号

    private String sname; // 姓名

    private String gender; // 性别

    private Date birthday; // 出生日期

    //private String address; // 地址

    private Blob picture;//照片

    private Address address; // 地址

    

    public Address getAddress() {

        return address;

    }

    public void setAddress(Address address) {

        this.address = address;

    }

    public Blob getPicture() {

        return picture;

    }

    public void setPicture(Blob picture) {

        this.picture = picture;

    }

    public Students() {

    }

    public Students(int sid, String sname, String gender, Date birthday,

            String address) {

        // super();

        this.sid = sid;

        this.sname = sname;

        this.gender = gender;

        this.birthday = birthday;

        //this.address = address;

    }

    public int getSid() {

        return sid;

    }

    public void setSid(int sid) {

        this.sid = sid;

    }

    public String getSname() {

        return sname;

    }

    public void setSname(String sname) {

        this.sname = sname;

    }

    public String getGender() {

        return gender;

    }

    public void setGender(String gender) {

        this.gender = gender;

    }

    public Date getBirthday() {

        return birthday;

    }

    public void setBirthday(Date birthday) {

        this.birthday = birthday;

    }

    @Override

    public String toString() {

        return "Students [sid=" + sid + ", sname=" + sname + ", gender="

                + gender + ", birthday=" + birthday + ", address=" + address

                + "]";

    }

}

 

3、Students.hbm.xml

&lt;?xml version="1.0"?&gt;

&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"[http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"](http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd%22);&gt;

&lt;!-- Generated 2017-9-17 10:33:28 by Hibernate Tools 3.4.0.CR1 --&gt;

&lt;hibernate-mapping&gt;

    &lt;class name="Students" table="STUDENTS"&gt;

        &lt;id name="sid" type="int"&gt;

            &lt;column name="SID" /&gt;

            &lt;generator class="native" /&gt;

            &lt;!--  &lt;generator class="assigned" /&gt;--&gt;

        &lt;/id&gt;

        &lt;property name="sname" type="java.lang.String"&gt;

            &lt;column name="SNAME" /&gt;

        &lt;/property&gt;

        &lt;property name="gender" type="java.lang.String"&gt;

            &lt;column name="GENDER" /&gt;

        &lt;/property&gt;

        &lt;property name="birthday" type="timestamp"&gt;

            &lt;column name="BIRTHDAY" /&gt;

        &lt;/property&gt;

        &lt;!--         

        &lt;property name="address" type="java.lang.String"&gt;

            &lt;column name="ADDRESS" /&gt;

        &lt;/property&gt;

        --&gt;

        &lt;property name="picture" type="java.sql.Blob"&gt;

            &lt;column name="PICTURE" /&gt;

        &lt;/property&gt;

        &lt;component name="address" class="Address"&gt;

                &lt;property name="postcode" column="POSTCODE"&gt;&lt;/property&gt;

                &lt;property name="photo" column="PHOTO"&gt;&lt;/property&gt;

                &lt;property name="address" column="ADDRESS"&gt;&lt;/property&gt;

        &lt;/component&gt;

    &lt;/class&gt;

&lt;/hibernate-mapping&gt;

4、测试方法

        @Test

        public void testSaveStudents(){

                //Students s= new Students(1,"张三丰","男",new Date(),"武当山");

                Students s= new Students();

                s.setSname("张三丰");

                s.setGender("男");

                s.setBirthday(new Date());

                //s.setAddress("武当山");

                Address address = new Address("7232","125674323","湖北");

                s.setAddress(address);

            session.save(s);

        }

5、测试结果

 

![](http://dl2.iteye.com/upload/attachment/0127/7343/c2056512-c257-3be3-9e83-97ef73ca51d6.png)