在Hibernate的一对多配置的级联保存中,1方和N方的实体类和关系映射文件都已经配置好,
N方Student实体类:
package cn.itcase.domain;
public class Student {
private String sid;
private String sname;
private int sage;
private Teacher teac;
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public Teacher getTeac() {
return teac;
}
public void setTeac(Teacher teac) {
this.teac = teac;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
}
N方Student.hbm.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcase.domain.Student" table="student">
<id name="sid" column="sid">
<generator class="uuid" />
</id>
<property name="sname" column="sname" ></property>
<property name="sage" column="sage" ></property>
<many-to-one name="teac" class="cn.itcase.domain.Teacher" column="t_id" />
</class>
</hibernate-mapping>
1方Teacher实体类:
package cn.itcase.domain;
import java.util.Set;
public class Teacher {
private String tid;
private String tname;
private String course;
private Set<Student> stus;
public String getTid() {
return tid;
}
public void setTid(String tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public Set<Student> getStus() {
return stus;
}
public void setStus(Set<Student> stus) {
this.stus = stus;
}
}
1方Teacher.hbm.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcase.domain.Teacher" table="teacher">
<id name="tid" column="tid">
<generator class="uuid" />
</id>
<property name="tname" column="tname" ></property>
<property name="course" column="course" ></property>
<set name="stus" >
<key column="t_id" ></key>
<one-to-many class="cn.itcase.domain.Student"/>
</set>
</class>
</hibernate-mapping>
测试代码类:
package cn.itcase.hibernatetest;
/**
* 一对多操作中的级联保存
*/
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.itcase.domain.Student;
import cn.itcase.domain.Teacher;
import cn.itcase.utils.HibernateUtils;
public class HibernateO2MSave {
@Test
public void addDemo1() {
SessionFactory sessionFactory=null;
Session session = null;
Transaction tx=null;
try {
sessionFactory = HibernateUtils.getSessionFactory();
session=sessionFactory.openSession();
tx = session.beginTransaction();
//创建教师对象
Teacher t1=new Teacher();
t1.setTname("李老师");
t1.setCourse("语文");
//创建学生对象
Student s1=new Student();
s1.setSname("小明");
s1.setSage(18);
Student s2=new Student();
s2.setSname("小李");
s2.setSage(22);
//建立教师对象和学生对象的双向联系
t1.getStus().add(s1);
t1.getStus().add(s2);
s1.setTeac(t1);
s2.setTeac(t1);
//分别保存教师和学生对象
session.save(t1);
session.save(s1);
session.save(s2);
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
session.close();
sessionFactory.close();
}
}
}
测试结果发现,Hibernate能自动在数据库里面创建student表和teacher表而且两个表见的联系也存在,可是测试中的一条教师记录以及和这条教师记录关联的学什么记录无法插入数据库,也没有错误异常信息
排查过程:
单独插入教师记录和学生记录都成功了,手动在数据库中插入教师和学生记录并建立练习没有问题
后来发现,在Teacher的实体类中,用了记录何其有联系的学生记录的Set集合,需要示例化