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

2021-07-20 10:05

阅读:520

标签:null   dtd   .config   uil   logs   取数   数据库   size   private   

 一. 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">


hibernate-configuration>
    
session-factory>
    
    property name="dialect">org.hibernate.dialect.Oracle9Dialectproperty>
    property name="connection.url">jdbc:oracle:thin:local:1521:orclproperty>
    property name="connection.username">scottproperty>
    property name="connection.password">tigerproperty>
    property name="connection.driver_class">oracle.jdbc.OracleDriverproperty>
    
    property name="show_sql">trueproperty>
    
    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">

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加载的区别

标签:null   dtd   .config   uil   logs   取数   数据库   size   private   

原文地址:http://www.cnblogs.com/xushirong/p/7056133.html


评论


亲,登录后才可以留言!