1、Annotation
Group.java
 1package com.bebig.hibernate.model;
 2
 3import java.util.HashSet;
 4import java.util.Set;
 5
 6import javax.persistence.Entity;
 7import javax.persistence.GeneratedValue;
 8import javax.persistence.Id;
 9import javax.persistence.OneToMany;
10import javax.persistence.Table;
11
12@Entity
13@Table(name = "t_group")
14public class Group {
15    private int id;
16
17    private String name;
18
19    private Set<User> users = new HashSet<User>();
20
21    @Id
22    @GeneratedValue
23    public int getId() {
24        return id;
25    }

26
27    public String getName() {
28        return name;
29    }

30
31    @OneToMany(mappedBy="group")
32    public Set<User> getUsers() {
33        return users;
34    }

35
36    public void setId(int id) {
37        this.id = id;
38    }

39
40    public void setName(String name) {
41        this.name = name;
42    }

43
44    public void setUsers(Set<User> users) {
45        this.users = users;
46    }

47}

48
User.java
 1package com.bebig.hibernate.model;
 2
 3import javax.persistence.Entity;
 4import javax.persistence.GeneratedValue;
 5import javax.persistence.Id;
 6import javax.persistence.JoinColumn;
 7import javax.persistence.ManyToOne;
 8import javax.persistence.Table;
 9
10@Entity
11@Table(name = "t_user")
12public class User {
13    private int id;
14
15    private String name;
16
17    private Group group;
18
19    @ManyToOne
20    @JoinColumn(name = "groupId")//指定关联字段的名称
21    public Group getGroup() {
22        return group;
23    }

24
25    @Id
26    @GeneratedValue
27    public int getId() {
28        return id;
29    }

30
31    public String getName() {
32        return name;
33    }

34
35    public void setGroup(Group group) {
36        this.group = group;
37    }

38
39    public void setId(int id) {
40        this.id = id;
41    }

42
43    public void setName(String name) {
44        this.name = name;
45    }

46}

47
hibernate.cfg.xml
1<mapping class="com.bebig.hibernate.model.Group"/> 
2         <mapping class="com.bebig.hibernate.model.User"/> 
2、XML
修改两个类映射文件与hibernate.cfg.xml
Group.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="Group" table="t_group">
 8        <id name="id">
 9            <generator class="native"></generator>
10        </id>
11        <property name="name"></property>
12        <set name="users">
13            <key column="groupId"></key>
14            <one-to-many class="User" />
15        </set>
16    </class>
17</hibernate-mapping>
User.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="User" table="t_user">
 8        <id name="id">
 9            <generator class="native"></generator>
10        </id>
11        <property name="name"></property>
12        <many-to-one name="group" column="groupId"></many-to-one>
13    </class>
14</hibernate-mapping>
hibernate.cfg.xml
1<mapping resource="com/bebig/hibernate/model/Group.hbm.xml" />
2        <mapping resource="com/bebig/hibernate/model/User.hbm.xml" />