*****************
Teacher.java
****************
package blog.hibernate.domain;
import java.util.Set;
public class Teacher {
private int id;
private String name;
private Set<Student> students;
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;
}
}
******************
Student.hbm.xml
*****************
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="blog.hibernate.domain">
<class name="Student" table="students">
<id name="id" column="student_id">
<generator class="native"></generator>
</id>
<property name="name" column="student_name"></property>
<set name="teachers" table="teacher_student">
<key column="student_id"></key>
<many-to-many class="Teacher" column="teacher_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
******************
Student.java
*****************
package blog.hibernate.domain;
import java.util.Set;
public class Student {
private int id;
private String name;
private Set<Teacher> teachers;
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<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
}
******************
Teacher.hbm.xml
*****************
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="blog.hibernate.domain">
<class name="Teacher" table="teachers">
<id name="id" column="teacher_id">
<generator class="native"></generator>
</id>
<property name="name" column="teacher_name"></property>
<set name="students" table="teacher_student">
<key column="teacher_id"></key>
<many-to-many class="Student" column="student_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
**************
juint test : Many2Many.java
*************
package junit.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.BeforeClass;
import org.junit.Test;
import blog.hibernate.HibernateUtil;
import blog.hibernate.domain.Student;
import blog.hibernate.domain.Teacher;
public class Many2Many {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void add(){
Teacher t1 = new Teacher();
t1.setName("t1");
Teacher t2 = new Teacher();
t2.setName("t2");
Set<Teacher> ts = new HashSet<Teacher>();
ts.add(t1);
ts.add(t1);
Student s1 = new Student();
s1.setName("s1");
Student s2 = new Student();
s2.setName("s2");
Set<Student> ss = new HashSet<Student>();
ss.add(s1);
ss.add(s2);
t1.setStudents(ss);
t2.setStudents(ss);
/*与上面的有一方面设置就行,全设置会导致org.hibernate.exception.ConstraintViolationException
s1.setTeachers(ts);
s2.setTeachers(ts);*/
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
session.save(t1);
session.save(t2);
session.save(s1);
session.save(s2);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
}finally{
if (session != null) {
session.close();
}
}
}
}