存在如下表,考虑多对一和一对多的配置
多对一,如何在多的一端配置 从多的一端获取一的一端的数据
student
public class Student {
private int id;
private String name;
private int age;
private Grade grade;
//get…set
}
Grade
public class Grade {
private int id;
private String name;
//get…set
}
Grade.hbm.xml
<hibernate-mapping package="cn.siggy.pojo">
<class name="Grade">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
Student.hbm.xml
<hibernate-mapping package="cn.siggy.pojo">
<class name="Student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"/>
<property name="age"/>
<!-- 多对一 name 表示属性名 class 指明 属性对应的类
column指 数据库表中的列名 -->
<many-to-one name="grade" class="Grade"
column="grade_id" foreign-key="fk_grade" not-null="true"/>
</class>
</hibernate-mapping>
测试(session对象的获取封装在HibernateUtil,略)
@Test
public void testSave() throws HibernateException, SerialException, SQLException{
Session session = null;
Transaction tx = null;
try{
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Grade grade = new Grade();
grade.setName("基础");
session.save(grade);
Student stu = new Student();
stu.setName("张三疯");
stu.setAge(22);
stu.setGrade(grade);
session.save(stu);
tx.commit();
}catch (HibernateException e) {
if(tx!=null)
tx.rollback();
e.printStackTrace();
throw e;
}finally{
HibernateUtil.closeSession();
}
}
一对多 在一的一端配置 从一的一端获取多的一端的数据 用集合表示
Student
public class Student {
private int id;
private String name;
private int age;
//get…set
}
Grade
public class Grade {
private int id;
private String name;
private Set<Student> students = new HashSet<Student>(0); //get…set
}
Grade.hbm.xml
<hibernate-mapping package="cn.siggy.pojo">
<class name="Grade">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"/>
<!-- set是 Grade中的集合属性 name属性名称 -->
<set name="students">
<!-- key表示外键 column外键列名-->
<key column="grade_id" not-null="true"></key>
<!-- one-to-many 一对多 类Grade 中 students 所表示类型 -->
<one-to-many class="Student"/>
</set>
</class>
</hibernate-mapping>
student.hbm.xml
<hibernate-mapping package="cn.siggy.pojo">
<class name="Student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"/>
<property name="age"/>
</class>
</hibernate-mapping>
测试
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Grade grade = new Grade();
grade.setName("基础");
Student stu = new Student();
stu.setName("张三疯");
stu.setAge(22);
Student stu1 = new Student();
stu1.setName("老王");
stu1.setAge(23);
//关联
grade.getStudents().add(stu);
grade.getStudents().add(stu1);
//保存数据的顺序 是根据外键的配置来决定的
//如果外键不能为null,那么先保存一的一端
//如果外键可以为null,则可以随意保存
session.save(grade);
session.save(stu);
session.save(stu1);
tx.commit();
session = HibernateUtil.getSession();
tx = session.beginTransaction();
//取数据
Grade grade = (Grade)session.get(Grade.class, 1);
System.out.println("gradeName="+grade.getName());
System.out.println("grade所对应的多的一端的数据");
Iterator<Student> iter = grade.getStudents().iterator();
for(;iter.hasNext();){
Student temp = iter.next();
System.out.println("name="+temp.getName()+"\tage="+temp.getAge());
}
tx.commit();