hibernate one2many (单向关联)

Posted 错莫难瞒

tags:

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

Classes类:

package com.cnblogs.hibernate_first;

import java.util.Set;

public class Classes {

    private int id;

    private String name;
    
    //hibernate对Set进行了扩展,可以延时加载,在需要的时候才需要查询SET。比如有1000个学生,当需要的时候才进行加载
    private Set students;

    public Set getStudents() {
        return students;
    }

    public void setStudents(Set students) {
        this.students = students;
    }

    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;
    }
}

Student:

public class Student {

    private int id;

    private String 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;
    }

}

classes.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 package="com.cnblogs.hibernate_first">
    <class name="Classes" table="t_classes">
        <id name="id" column="t_id">
            <generator class="native"></generator>
        </id>
        <property name="name" column="t_name" length="30"/>
        <set name="students">
            <key column="classesid"></key>
            <one-to-many class="com.cnblogs.hibernate_first.Student"/>
        </set>
    </class>
</hibernate-mapping>

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 package="com.cnblogs.hibernate_first">
    <class name="Student" table="t_student">
        <id name="id" column="t_id">
            <generator class="native"></generator>
        </id>
        <property name="name" column="t_name" length="30"/>
    </class>
</hibernate-mapping>

测试类:

package com.cnblogs.hibernate_first;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;

import junit.framework.TestCase;

public class One2ManyTest extends TestCase {

    public void testSave1() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            // 建立关联
            Student student1 = new Student();
            student1.setName("张三");
            
            Student student2 = new Student();
            student2.setName("李四");
            Classes classes = new Classes();
            classes.setName("动力节点");
            Set<Student> students = new HashSet<Student>();
            students.add(student1);
            students.add(student2);
            classes.setStudents(students);
            //因为student不是持久化状态,他是Transient状态
            session.save(classes);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }
    }
    
    public void testSave2() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            // 建立关联
            Student student1 = new Student();
            student1.setName("张三");
            session.save(student1);
            
            Student student2 = new Student();
            student2.setName("李四");
            session.save(student2);
            
            Classes classes = new Classes();
            classes.setName("动力节点");
            Set<Student> students = new HashSet<Student>();
            students.add(student1);
            students.add(student2);
            classes.setStudents(students);
            //因为student不是持久化状态,他是Transient状态
            session.save(classes);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }
    }


    public void testLoad1() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            Classes classes = (Classes)session.load(Classes.class, 1);
            System.out.println("Classes.name = " + classes.getName());
            Set students = classes.getStudents();
            for(Iterator iter = students.iterator();iter.hasNext();){
                Student student = (Student)iter.next();
                System.out.println("Student in Classes " + student.getName());
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }
    }
}

 

hibernate 一对多的关联映射(单向 Classes ——》Student)
hibernate对Set进行了扩展,可以延时加载,在需要的时候才需要查询SET。比如有1000个学生,当需要的时候才进行加载
一对多和多对一的映射原理是一样的,都是在多的一端加入一个外键指向一的一端
他们的区别在于维护的关系不用
多对一维护的关系:多指向一的关系,如果维护了多指向一的关系,那么加载多的时候会把一加载上来。
一对多维护的关系:一指向多的关系,如果维护了一指向多的关系,那么加载一的时候会把一加载上来。

在一的一端维护关系存在缺陷
*因为多的一端Student不知道Classes的存在(也就是Student没有维护与Classes的关系),所以在保存Student的时候关系字段Classid为null,
如果将该关系字段设置为非空,则将无法保存数据。
*另外因为Student不维护关系,而Classes维护关系,Classes就会发出多余的update语句,保证Classes与Student有关系,,这样加载Classes的
时候,才能把该Classes对应的学生集合加载上来。












以上是关于hibernate one2many (单向关联)的主要内容,如果未能解决你的问题,请参考以下文章

hibernate关联关系映射

hibernate学习四 hibernate关联关系映射

Hibernate单向“多对一”关联

Hibernate单向“一对一”关联

Hibernate单向“一对多”关联

Hibernate关联映射