hibernate 之 关联映射的双向一对多关联

Posted forever_2h

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate 之 关联映射的双向一对多关联相关的知识,希望对你有一定的参考价值。

1. 考虑学生表 和 教师表,表结构和单向的表结构一样。

2. 类结构:Teacher.java

public class Teacher {
    private int id;
    private String name;
    private Set<Student> students = new HashSet<Student>();
    public Teacher() {
    }
    public Teacher(String name) {
        super();
        this.name = name;
    }

    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.java

public class Student {
    private int id;
    private String name;
    private String sex;
    private Teacher teacher;
    public Student() {
    }
    public Student(String name, String sex) {
        super();
        this.name = name;
        this.sex = sex;
    }

    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 String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
}

3.  映射文件:Teacher.hbm.xml :

<hibernate-mapping>
    <class name="cn.wh.vo.Teacher" table="t_teacher">
        <id name="id">
            <generator class="native"></generator>
        </id>
        <property name="name"/>
        <set name="students" inverse="true">
            <key column="tid"></key>
            <one-to-many class="cn.wh.vo.Student"/>
        </set>
    </class>
</hibernate-mapping>

Student.hbm.xml:

<hibernate-mapping>
    <class name="cn.wh.vo.Student" table="t_student">
        <id name="id">
            <generator class="native"></generator>
        </id>
        <property name="name"/>
        <property name="sex"/>
        <!-- 配置多对一的关联映射
            name配置的是  一的一端在多的一端的属性名
            column 配置的是 一的一端在多的一端表中的外键
         -->
        <many-to-one name="teacher" column="tid"/>
    </class>
</hibernate-mapping>

4. 测试:

public class HibernateTest {
    private Session session=null;
    @Before
    public void setUp(){
        session = HibernateUtil.getSession();
    }
    @After
    public void tearDown(){
        HibernateUtil.close();
    }
    @Test
    public void testCreateDB(){
        Configuration cfg = new Configuration().configure();
        SchemaExport export = new SchemaExport(cfg);
        //第一个参数  是否打印  数据库脚本
        //第二个参数 是否将脚本导入数据库执行
        export.create(true, true);
    }
    @Test
    public void testInit(){
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Teacher t1 = new Teacher("nico");
            Teacher t2 = new Teacher("michile");
            Student s1 = new Student("liming","男");
            s1.setTeacher(t1);
            Student s2 = new Student("hanmei","女");
            s2.setTeacher(t1);
            Student s3 = new Student("jim","男");
            s3.setTeacher(t1);
            
            session.save(t1);
            session.save(t2);
            session.save(s1);
            session.save(s2);
            session.save(s3);
            tx.commit();
            
        } catch (Exception e) {
            if(tx!=null)
                tx.rollback();
        }
    }
    @Test
    public void testGet(){
        Student stu = (Student)session.get(Student.class, 1);
        System.out.println(stu.getName()+"---"+stu.getSex()+"----"+stu.getTeacher().getName());
        System.out.println("----------------------------");
        Teacher t = (Teacher)session.get(Teacher.class, 1);
        System.out.println("teacherName==="+t.getName());
        for(Student s:t.getStudents()){
            System.out.println(s.getName()+"----"+s.getSex());
        }
        
    }
    
}

 

以上是关于hibernate 之 关联映射的双向一对多关联的主要内容,如果未能解决你的问题,请参考以下文章

hibernate 之 关联映射的基于外键的双向一对一关联

Hibernate一对多单向(双向)关联映射

java之hibernate之基于外键的双向一对一关联映射

hibernate------映射相关

15.Hibernate一对多双向关联映射+分页

Hibernate框架学习之注解配置关系映射