欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[Hibernate单表操作] 组件属性

程序员文章站 2022-07-12 18:09:14
...
一 组件属性定义
实体类中某个属性属于用于自定义的类的对象。

[Hibernate单表操作] 组件属性
            
    
    博客分类: Hibernate 组件属性Hibernate 
 
 
二 组件属性应用
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
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<!-- Generated 2017-9-17 10:33:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="Students" table="STUDENTS">
        <id name="sid" type="int">
            <column name="SID" />
            <generator class="native" />
            <!--  <generator class="assigned" />-->
        </id>
        <property name="sname" type="java.lang.String">
            <column name="SNAME" />
        </property>
        <property name="gender" type="java.lang.String">
            <column name="GENDER" />
        </property>
        <property name="birthday" type="timestamp">
            <column name="BIRTHDAY" />
        </property>
        <!--         
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
        -->
        <property name="picture" type="java.sql.Blob">
            <column name="PICTURE" />
        </property>
        <component name="address" class="Address">
                <property name="postcode" column="POSTCODE"></property>
                <property name="photo" column="PHOTO"></property>
                <property name="address" column="ADDRESS"></property>
        </component>
    </class>
</hibernate-mapping>
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、测试结果

 


[Hibernate单表操作] 组件属性
            
    
    博客分类: Hibernate 组件属性Hibernate 
 
  • [Hibernate单表操作] 组件属性
            
    
    博客分类: Hibernate 组件属性Hibernate 
  • 大小: 96.9 KB
  • [Hibernate单表操作] 组件属性
            
    
    博客分类: Hibernate 组件属性Hibernate 
  • 大小: 33.2 KB