hibernate 双向一对多(多对一)
配置hibernate环境
实体类
package com.wd.pojo;
public class Student {
private Integer id;
private String name;
private Teacher teacher;//此处通过t_student中的tid列映射到t_teacher表中的一行记录
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
public Student(String name) {
super();
this.name = name;
}
public Student(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
}
public class Teacher {
private Integer id;
private String name;
private Set<Student> stuSet =new HashSet<>();//此处指定映射Student,注意添加get/set方法
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStuSet() {
return stuSet;
}
public void setStuSet(Set<Student> stuSet) {
this.stuSet = stuSet;
}
public Teacher(Integer id, String name, Set<Student> stuSet) {
super();
this.id = id;
this.name = name;
this.stuSet = stuSet;
}
public Teacher(String name, Set<Student> stuSet) {
super();
this.name = name;
this.stuSet = stuSet;
}
public Teacher() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + "]";
}
}
映射文件:一定注意inverse控制关联关系
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.wd.pojo.Student" table="t_student">
<id name="id" column="id">
<generator class="identity"></generator>
</id>
<property name="name" column="name" length="50"></property>
<!-- 注意此处所指定的是一行记录,并且能够通过tid列映射到t_teacher表格中 -->
<!-- 若是Teacher.hbm.xml中没有设置inverse可以在此处 insert="false" update="false" -->
<many-to-one name="teacher" column="tid"/>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.wd.pojo.Teacher" table="t_teacher">
<id name="id" column="id">
<generator class="identity"></generator>
</id>
<property name="name" column="name" length="50" ></property>
<!-- 此处设置inverse为true或者在Student.hbm.xml文件中设置 insert="false" update="false" -->
<set name="stuSet" inverse="true">
<!-- 指定外键,可以通过此键值查询出t_student中的信息 -->
<!-- column指定的外键值必须是t_student中已经有的字段,否则返回[] -->
<key column="tid" not-null="true"/>
<one-to-many class="com.wd.pojo.Student"/>
</set>
</class>
</hibernate-mapping>
测试代码
public class HibernateTest {
@Test
public void testGet() {
Session session = null;
try {
session = HibernateUtil.getSession();
session.beginTransaction();
// Student student = (Student) session.get(Student.class, 4);
// Teacher teacher = student.getTeacher();
// System.out.println(teacher);
// Teacher teacher = (Teacher) session.get(Teacher.class, 1);
// Set<Student> stuSet = teacher.getStuSet();
// for (Student student : stuSet) {
// System.out.println(student);
// }
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
}