还是用学生和老师来举例子
一个老师可以教多个学生,一个学生也可以有多个老师
(1)先写老师的实体类
package text01;
import java.util.HashSet;
import java.util.Set;
public class Teacher {
private int id;
private String name;
private Set<Student> students = new HashSet<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
还有映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="text01">
<class name="Teacher" table="teacher" >
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="name"/>
<set name="students" table="other">
<key column="id"></key>
<many-to-many class="text01.Student" column="num"></many-to-many>
</set>
</class>
</hibernate-mapping>
(2)写学生的实体类以及配置文件
实体类
package text01;
import java.util.HashSet;
import java.util.Set;
public class Student {
private int num;
private String name;
private Set<Teacher> teachers = new HashSet<>();
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
}
映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="text01">
<class name="Student" table="student">
<id name="num" column="num" type="integer">
<generator class="native" />
</id>
<property name="name" />
<set name="teachers" table="other">
<key column="num"></key>
<many-to-many class="text01.Teacher" column="id"></many-to-many>
</set>
</class>
</hibernate-mapping>
(3)导入配置文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hib</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="text01/student.hbm.xml" />
<mapping resource="text01/teacher.hbm.xml" />
</session-factory>
</hibernate-configuration>
(4)
来测试一下
package text;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import text01.Student;
import text01.Teacher;
public class text01 {
public static void main(String[] args) {
Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction ts = session.beginTransaction();
Teacher teacher = new Teacher();
Student s1 = new Student();
Student s2 = new Student();
s1.setName("666");
s1.setNum(000);
s2.setName("6667");
s2.setNum(0001);
teacher.setId(1);
teacher.setName("tom");
teacher.getStudents().add(s1);
teacher.getStudents().add(s2);
session.save(teacher);
session.save(s1);
session.save(s2);
ts.commit();
session.close();
}
}