1、Annotation
Husband.java
 1package com.bebig.hibernate.model;
 2
 3import javax.persistence.Embedded;
 4import javax.persistence.Entity;
 5import javax.persistence.GeneratedValue;
 6import javax.persistence.Id;
 7
 8@Entity
 9public class Husband {
10    private int id;
11
12    private String name;
13
14    private Wife wife;
15
16    @Id
17    @GeneratedValue
18    public int getId() {
19        return id;
20    }

21
22    public String getName() {
23        return name;
24    }

25
26    @Embedded
27    public Wife getWife() {
28        return wife;
29    }

30
31    public void setId(int id) {
32        this.id = id;
33    }

34
35    public void setName(String name) {
36        this.name = name;
37    }

38
39    public void setWife(Wife wife) {
40        this.wife = wife;
41    }

42}

43
Wife.java
 1package com.bebig.hibernate.model;
 2
 3public class Wife {
 4    private String wifename;
 5
 6    private int age;
 7
 8    public int getAge() {
 9        return age;
10    }

11
12    public String getWifeName() {
13        return wifename;
14    }

15
16    public void setAge(int age) {
17        this.age = age;
18    }

19
20    public void setWifeName(String name) {
21        this.wifename = name;
22    }

23}

24
hibernate.cfg.xml
1 <mapping class="com.bebig.hibernate.model.Husband"/>
2、XML
除了hibernate.cfg.xml与以上不同外,另添加一个Husband.hbm.xml文件。
Husband.hbm.xml
 1<?xml version="1.0"?>
 2<!DOCTYPE hibernate-mapping PUBLIC
 3        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5
 6<hibernate-mapping package="com.bebig.hibernate.model">
 7    <class name="Husband">
 8        <id name="id">
 9            <generator class="native"></generator>
10        </id>
11        <property name="name"></property>
12        <component name="wife">
13            <property name="wifeName"></property>
14            <property name="age"></property>
15        </component>
16    </class>
17</hibernate-mapping>
hibernate.cfg.xml
1<mapping resource="com/bebig/hibernate/model/Husband.hbm.xml" />