1、Annotation
Teacher.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.JoinTable;
11import javax.persistence.ManyToMany;
12
13@Entity
14public class Teacher {
15    private int id;
16
17    private String name;
18
19    private Set<Student> students = new HashSet<Student>();
20
21    @Id
22    @GeneratedValue
23    public int getId() {
24        return id;
25    }

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

30
31    @ManyToMany
32    @JoinTable(name = "t_s", joinColumns = { @JoinColumn(name = "t_id") }, inverseJoinColumns = { @JoinColumn(name = "s_id") })
33    public Set<Student> getStudents() {
34        return students;
35    }

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

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

44
45    public void setStudents(Set<Student> students) {
46        this.students = students;
47    }

48}

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

18
19    public String getName() {
20        return name;
21    }

22
23    public void setId(int id) {
24        this.id = id;
25    }

26
27    public void setName(String name) {
28        this.name = name;
29    }

30}

31
hibernate.cfg.xml
1<mapping class="com.bebig.hibernate.model.Teacher"/> 
2         <mapping class="com.bebig.hibernate.model.Student"/> 
2、XML
修改两个类映射文件和hibernate.cfg.xml
Teacher.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="Teacher">
 8        <id name="id">
 9            <generator class="native"></generator>
10        </id>
11        <property name="name"></property>
12        <set name="students" table="t_s">
13            <key column="t_id"></key>
14            <many-to-many class="Student" column="s_id" />
15        </set>
16    </class>
17</hibernate-mapping>
Student.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="Student">
 8        <id name="id">
 9            <generator class="native"></generator>
10        </id>
11        <property name="name"></property>
12    </class>
13</hibernate-mapping>
hibernate.cfg.xml
1<mapping resource="com/bebig/hibernate/model/Teacher.hbm.xml" />
2        <mapping resource="com/bebig/hibernate/model/Student.hbm.xml" />