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 java.util.HashSet;
 4import java.util.Set;
 5
 6import javax.persistence.Entity;
 7import javax.persistence.GeneratedValue;
 8import javax.persistence.Id;
 9import javax.persistence.ManyToMany;
10
11@Entity
12public class Student {
13    private int id;
14
15    private String name;
16
17    private Set<Teacher> teachers = new HashSet<Teacher>();
18
19    @Id
20    @GeneratedValue
21    public int getId() {
22        return id;
23    }

24
25    public String getName() {
26        return name;
27    }

28
29    @ManyToMany(mappedBy = "students")
30    public Set<Teacher> getTeachers() {
31        return teachers;
32    }

33
34    public void setId(int id) {
35        this.id = id;
36    }

37
38    public void setName(String name) {
39        this.name = name;
40    }

41
42    public void setTeachers(Set<Teacher> teachers) {
43        this.teachers = teachers;
44    }

45}

46
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        <set name="teachers" table="t_s">
13            <key column="s_id"></key>
14            <many-to-many class="Teacher" column="t_id" />
15        </set>
16    </class>
17</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" />