首页 > 代码库 > Hibernate中使用load和get加载的区别

Hibernate中使用load和get加载的区别

 一. Hibernate使用load机制和使用get加载的区别(重点)

   1.相同点:get 和load他们都可以通过指定的实体类与ID从数据库中读取数据,并返回对应的实例

   2. 不同点:如果找不到符合条件的纪录,get()方法将返回null.而load()将会报出ObjectNotFoundEcception.

    2.1get 是直接从数据库中检索   而load会使用懒加载(延迟加载)产生一个代理对象,属性oid

    2.2 使用load加载,Hibernate会认为该id对应的对象是一定存在的,所以它可以放心的使用,它可以放心的使用代理来 延迟加载该对象。在用到对象中的其他属性数据时才查询数据库,但是万一数据库中不存在该记录,那没办法,只能抛异常。

    2.3 使用get加载,hibernate会确认一下该id对应的数据是否存在,首先在session缓存中查找,然后在二级缓存中查找,还没有就查数据库,数据库中没有就返回null。

    3.测试代码如下:

      3.1创建Hibernate的主配置文件:

  

<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
    <!-- 配置sessionFactory,表示获取连接,包含连接的四要素 
        注:hibernate 连接的四要素中的名字不能更改,必须是以下的名字
    -->
<session-factory>
    <!--方言   告诉hibernate,连接的是oracle数据库     -->
    <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
    <property name="connection.url">jdbc:oracle:thin:local:1521:orcl</property>
    <property name="connection.username">scott</property>
    <property name="connection.password">tiger</property>
    <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
    <!-- 此标签用于是否在控制台输出sql语句  此处ture表示输出 -->
    <property name="show_sql">true</property>
    <!--此标签表示引入hiberante的映射文件 -->
    <mapping resource="cn/et/hibernate/lesson02/query/Emp.hbm.xml" />
</session-factory>

</hibernate-configuration>

  3.2 创建hibernate的隐射文件Emp.hbm.xml

  

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="cn.et.hibernate.lesson01.Emp" table="EMP" schema="SCOTT">
        <id name="empno" type="java.lang.Short">
            <column name="EMPNO"/>
            <generator class="native" />
        </id>
        <property name="ename" type="java.lang.String">
            <column name="ENAME">
                <comment>员工姓名</comment>
            </column>
        </property>
        <property name="job" type="java.lang.String">
            <column name="JOB">
                <comment>员工职位</comment>
            </column>
        </property>
        <property name="mgr" type="java.lang.Short">
            <column name="MGR">
                <comment>领导编号</comment>
            </column>
        </property>
        <property name="hiredate" type="java.util.Date">
            <column name="HIREDATE">
                <comment>雇佣日期</comment>
            </column>
        </property>
        <property name="sal" type="java.lang.Double">
            <column name="SAL">
                <comment>月薪</comment>
            </column>
        </property>
        <property name="comm" type="java.lang.Double">
            <column name="COMM">
                <comment>奖金</comment>
            </column>
        </property>
        <property name="deptno" type="java.lang.Short">
            <column name="DEPTNO">
                <comment>所属部门编号</comment>
            </column>
        </property>
    </class>
</hibernate-mapping>

  3.3创建Emp表的实体类

  

package cn.et.hibernate.lesson02.query;

import java.util.Date;

/**
 * Emp entity. @author MyEclipse Persistence Tools
 */

public class Emp implements java.io.Serializable {

    // Fields

    private Short empno;
    private String ename;
    private String job;
    private Short mgr;
    private Date hiredate;
    private Double sal;
    private Double comm;
    private Short deptno;

    // Constructors

    /** default constructor */
    public Emp() {
    }

    /** full constructor */
    public Emp(String ename, String job, Short mgr, Date hiredate, Double sal,
            Double comm, Short deptno) {
        this.ename = ename;
        this.job = job;
        this.mgr = mgr;
        this.hiredate = hiredate;
        this.sal = sal;
        this.comm = comm;
        this.deptno = deptno;
    }

    // Property accessors

    public Short getEmpno() {
        return this.empno;
    }

    public void setEmpno(Short empno) {
        this.empno = empno;
    }

    public String getEname() {
        return this.ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return this.job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Short getMgr() {
        return this.mgr;
    }

    public void setMgr(Short mgr) {
        this.mgr = mgr;
    }

    public Date getHiredate() {
        return this.hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public Double getSal() {
        return this.sal;
    }

    public void setSal(Double sal) {
        this.sal = sal;
    }

    public Double getComm() {
        return this.comm;
    }

    public void setComm(Double comm) {
        this.comm = comm;
    }

    public Short getDeptno() {
        return this.deptno;
    }

    public void setDeptno(Short deptno) {
        this.deptno = deptno;
    }

}

  3.4 创建测试类代码:

  

package cn.et.hibernate.lesson02.query;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class TestHibernate {
    //表示增加
    @Test
    public void test1(){
        //创建sessionFactory
        SessionFactory sf = new Configuration().configure("/cn/et/hibernate/lesson02/hibernate.cfg.xml").buildSessionFactory();
        Session session = sf.openSession();
        //开启一个事物
        Transaction  t = session.beginTransaction();
        Emp emp = (Emp)session.load(Emp.class,Short.parseShort("7369"));
        t.commit();
    }
}
//上述代码可用dubug模式进行调试,加载load 和get ,测试加载的过程

 

Hibernate中使用load和get加载的区别