Hibernate-ORM:04.Hibernate中的get()和load()

 

 

 

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

 

本篇博客会讲如何用get()或load()查询单个对象和对缓存的简单操作,以及他俩的区别和相同(前面有的那些配置不做重复展示,见谅)

 

Hibernate中查询单个,就是用get()或load()任意一个做操作即可!

我会讲明区别和相同的地方,再加上一些验证我说的区别和相同的实例:

 

一,查询单个

  1.使用get()查询单个

 

    @Test
    /*get他是直接加载*/
    public void t4GetDemo01(){
        System.out.println("----------------------------------");
        Teacher teacher = session.get(Teacher.class, 1);
        System.out.println("----------------------------------");
        System.out.println(teacher.getName());
    }

 

  2.使用load()查询单个

 

    @Test
    /*load他是懒加载*/
    public void t5LoadDemo01(){
        System.out.println("----------------------------------");
        Teacher teacher = session.load(Teacher.class, 1);
        System.out.println("----------------------------------");
        System.out.println(teacher.getName());
    }

 

 

 

二,相同处

  1.get()和load()都使用缓存,他们查找记录的策咯是,先找一级缓存,有就拿来用,没有再去找二级,二级还没有就找数据库

  (验证规则:同样的查俩次,看看发几次sql不就好了么!)

 

    /*get查记录从一级缓存开始找,一级没找到去二级找,二级没找到去数据库,数据库没找到返回null*/
    @Test
    /*测试get缓存1*/
    /*从他只发一条sql就知道他走的有缓存,因为二级缓存没有配置,所以,是一级缓存*/
    public void t6GetCacheDemo01(){
        System.out.println("----------------------------------");
        Teacher teacher1 = session.get(Teacher.class, 1);
        Teacher teacher2 = session.get(Teacher.class, 1);
        System.out.println("----------------------------------");
    }

 

  2.他俩使用基本相似,传的同样俩个参数,返回值也一样

 

三,区别

  1.get()是直接加载,load是懒加载(又称延迟加载)

  (验证:)

 

    /*get和load都是查询,区别:*/
    @Test
    /*get他是直接加载*/
    public void t4GetDemo01(){
        System.out.println("----------------------------------");
        Teacher teacher = session.get(Teacher.class, 1);
        System.out.println("----------------------------------");
        System.out.println(teacher.getName());
    }
    @Test
    /*load他是懒加载*/
    public void t5LoadDemo01(){
        System.out.println("----------------------------------");
        Teacher teacher = session.load(Teacher.class, 1);
        System.out.println("----------------------------------");
        System.out.println(teacher.getName());
    }
    /*测试在有懒加载的时候不使用它查不查数据*/
    public void t12LoadLazyTrueDemo02(){
        Teacher teacher = session.load(Teacher.class, 1);
        System.out.println("----------------------------------");
    }

 

  2.查询一个没有的记录get()返回的是null,load()则会引发异常ObjectNotFoundException

 

    @Test
    /*测试get获取一个没有的值*/
    public void t9GetHaventDemo01(){
        Teacher teacher = session.get(Teacher.class, 5);
        System.out.println("----------------------------------");
        System.out.println(teacher);
    }
    @Test
    /*测试load获取一个没有的值,会抛ObjectNotFoundException*/
    public void t10LoadHaventDemo01(){
        Teacher teacher = session.load(Teacher.class, 5);
        System.out.println("----------------------------------");
        System.out.println(teacher);
    }

 

 

四,操作一级缓存查看对get()和load()的影响清除单个缓存,和全部缓存

 

    @Test
    /*清除单个缓存*/
    public void t7GetCacheDemo02(){
        System.out.println("----------------------------------");
        Teacher teacher1 = session.get(Teacher.class, 1);
        Teacher teacher2 = session.get(Teacher.class, 2);
        System.out.println("----------------------------------");
        session.evict(teacher1);
        teacher1= session.get(Teacher.class, 1);
    }
    @Test
    /*清除全部缓存*/
    public void t8GetCacheDemo03(){
        System.out.println("----------------------------------");
        Teacher teacher1 = session.get(Teacher.class, 1);
        Teacher teacher2 = session.get(Teacher.class, 2);
        System.out.println("----------------------------------");
        session.clear();
        teacher1= session.get(Teacher.class, 1);
        teacher2 = session.get(Teacher.class, 2);
    }

 

  结论:get()和load()确实使用到了缓存,如果缓存被清除,他们就得再发sql到数据库去查

 

五,load()也可以直接加载

  方案:

  在**.hbm.xml小配置文件中,配置延迟加载lazy=false

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.dawn.day01.entity">
    <!--如果上面指定package的话,class的name就不必写全类名-->
    <!--lazy:是否懒加载(延迟加载)        默认值是true,延迟加载-->
    <!--<class name="Teacher">-->
    <!--直接加载-->
    <class name="Teacher" lazy="false">
        <!--主键-->
        <id name="id" column="tid">
            <!--主键生成策咯  assigned程序员自己创建-->
            <!--identity是mysql里的自增,一会做增加操作不必再给主键赋值-->
            <!--increment是先查最大的主键列,在下一条给主键加一-->
            <!--sequence是oracle的主键生成策咯,他一会需要指定序列名字<param name="sequence">序列名</param>-->
            <generator class="assigned"></generator>
        </id>
        <property name="name" column="name"></property>
    </class>
</hibernate-mapping>

 

  测试:

 

    @Test
    /*测试在没有懒加载的时候不使用它查不查数据*/
    public void t12LoadLazyTrueDemo02(){
        Teacher teacher = session.load(Teacher.class, 1);
        System.out.println("----------------------------------");
    }

 

 

 

 

作者:晨曦Dawn

转载请注明出处,博客地址https://www.cnblogs.com/DawnCHENXI/p/9090682.html

如果有错误,请指出,感激不尽!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

posted @ 2018-05-25 21:04  晨曦Dawn  阅读(172)  评论(0编辑  收藏  举报