自己最近在做项目的过程中遇到了多对多修改的问题,因为之前已经有好几年没有用过Hibernate了,而Hibernate中的多对多的操作又是最复杂的,所以在这里难免就多花了一些时间,在这里总结下来供大家参考。以下我们来个简单的多对多关系建立老师Teacher 与 课程Course 是一个多对多的关系。
Pojo与XMl配置如下:
以下为 Pojo代码
=============================Course Pojo============================
package com.domain;
import java.util.HashSet;
import java.util.Set;
/**
* @author dengshaohua
* @create_date 2011-09-14
*/
public class Course {
private String id;
private String name;
private Set<Teacher> teachers = new HashSet<Teacher>();
public String getId() {
return id;
}
public void setId(String 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 Pojo============================
package com.domain;
import java.util.HashSet;
import java.util.Set;
/**
* @author dengshaohua
* @create_date 2011-09-14
*/
public class Teacher {
private String id;
private String name;
private Set<Course> courses = new HashSet<Course>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
XML配置文件如下
=================================Course 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>
<class name="com.domain.Course" table="tbl_course"
batch-size="100" dynamic-insert="true"
dynamic-update="true">
<id name="id" column="id">
<generator class="uuid" />
</id>
<property name="name" column="name" type="string" />
<set access="property" lazy="true" inverse="false"
cascade="save-update" name="teachers"
batch-size="10" fetch="select"
table="tbl_teacher_course">
<key column="fk_course_id" />
<many-to-many class="com.domain.Teacher"
column="fk_teacher_id" />
</set>
</class>
</hibernate-mapping>
=================================Teacher 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>
<class name="com.domain.Teacher" table="tbl_teacher"
batch-size="100" dynamic-insert="true"
dynamic-update="true">
<id name="id" column="id">
<generator class="uuid" />
</id>
<property name="name" column="name" type="string" />
<set access="property" lazy="true" inverse="true"
cascade="save-update" name="courses"
batch-size="10" fetch="select"
table="tbl_teacher_course">
<key column="fk_teacher_id" />
<many-to-many class="com.domain.Course"
column="fk_course_id" />
</set>
</class>
</hibernate-mapping>
先往数据库里插入一些记录
public void testSave() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
// create course
Course c1 = new Course();
Course c2 = new Course();
c1.setName("C");
c2.setName("Java");
// create teacher
Teacher t1 = new Teacher();
Teacher t2 = new Teacher();
t1.setName("Leo");
t2.setName("Rose");
// create relationship
c1.getTeachers().add(t1);
c1.getTeachers().add(t2);
t1.getCourses().add(c1);
t2.getCourses().add(c1);
// 因为主控方级联设置为save-update,如果设置为none,则下面被注释的代码需要开启,否则会报错
//session.save(t1);
//session.save(t2);
session.save(c1);
session.getTransaction().commit();
session.close();
}
下面是测试的一些结果:
1. 单独修改课程(Course)的信息,因为在这里课程(Course)被设置成了主控方(inverse="true"),所以在修改的时候,还需要保持与教师(Teacher)的关系,代码如下:
public void testUpdate() {
Course c1 = new Course();
c1.setId("402881ee175f04be01175f04c05d0001");
c1.setName("new CourseName");
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
String hql = "from Course c left join fetch c.teachers where c.id="+c1.getId();
List<Course> courses = session.createQuery(hql).list();
if(courses!=null && courses.size()>0){
c1.setTeachers(courses.get(0).getTeachers());
}
session.save(c1);
session.getTransaction().commit();
session.close();
}
通过这种方式去修改,查看后台,只有一条Update的SQL语句。
但是如果不将Teacher与Course进行关联的话,因为Course是主控方,在保存的时候,Course与Teacher的关系就会丢失,这个时候后台会生成两条SQL,一条Update和一个Delete.
2. 单独修改教师(Teacher)信息的时候,就没有那么复杂了,在Action中获得了从From表单封装到的值后直接调会Save方法就可以了,如下:
public void testUpdate() {
Teacher t1 = new Teacher();
t1.setId("402881ee175f04be01175f04c05d0001");
t1.setName("new TeacherName");
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(t1);
session.getTransaction().commit();
session.close();
}