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

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

24
25    public void setId(int id) {
26        this.id = id;
27    }

28
29    public void setName(String name) {
30        this.name = name;
31    }

32}

33
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.JoinColumn;
10import javax.persistence.OneToMany;
11import javax.persistence.Table;
12
13@Entity
14@Table(name = "t_group")
15public class Group {
16    private int id;
17
18    private String name;
19
20    private Set<User> users = new HashSet<User>();
21
22    @Id
23    @GeneratedValue
24    public int getId() {
25        return id;
26    }

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

31
32    @OneToMany
33    @JoinColumn(name = "groupId")
34    public Set<User> getUsers() {
35        return users;
36    }

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

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

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

49}

50
hibernate.cfg.xml
1         <mapping class="com.bebig.hibernate.model.Group"/> 
2         <mapping class="com.bebig.hibernate.model.User"/> 
2、XML
以上两个类文件不用修改,需要修改这两个类的映射文件,还有hibernate.cfg.xml
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    </class>
13</hibernate-mapping>
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>
hibernate.cfg.xml
1<mapping resource="com/bebig/hibernate/model/Group.hbm.xml" />
2        <mapping resource="com/bebig/hibernate/model/User.hbm.xml" />