分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

hibernate检索策略(抓取策略)

发布时间:2023-09-06 01:26责任编辑:彭小芳关键词:暂无标签

检索策略

  类级别检索

    默认检索策略:默认延迟加载, 可以使用lazy属性来进行改变.        

  session.get(clazz,object)默认立即加载

    @Test//测试左外连接查询public void test13(){Session session = HibernateUtils.getSession();Transaction bt = session.beginTransaction();Customer customer = session.get(Customer.class,1);bt.commit();session.close();}

  

  session.load(clazz,object)默认延迟加载    可以使用Hibernate.initialize(customer)初始化数据;

      @Test//load延迟加载public void test14(){Session session = HibernateUtils.getSession();Transaction bt = session.beginTransaction();Customer customer = session.load(Customer.class,1);bt.commit();session.close();}

关联级别检索

  一对一<one-to-one>

  一对多/多对一   <set>下有<one-to-many>      <many-to-one>

  多对多<many-to-many>

我们主要是在set下one-to-many或many-to-one设置lazy和fetch

查询一个客户下的订单

    set上

lazy
true延迟检索
false立即检索
extra加强延迟加载
fetch
select多条简单的sql语句
join采用迫切左外连接
subselect将生成子查询的sql

未进行设置则默认为懒加载

@Testpublic void test15(){Session session = HibernateUtils.getSession();Transaction bt = session.beginTransaction();String hql="from Customer c right outer join c.orders";Query query = session.createQuery(hql);List<Object[]> list = query.list();bt.commit();session.close();}

 sql打印:

Hibernate: ????select ???????customer0_.c_id as c_id1_0_, ???????customer0_.name as name2_0_ ????from ???????test.c_customer customer0_

  打印结果证明只进行查询了customer对象

  其中Order对象未进行加载  这样就是在调用时会发送sql语句进行查询  为了解决这一事件  我们让Customer立即加载

修改其配置文件:

 ?<set lazy="false" > //设置立即加载

  再次执行会sql打印

Hibernate: ????select ???????customer0_.c_id as c_id1_0_, ???????customer0_.name as name2_0_ ????from ???????test.c_customer customer0_Hibernate: ????select ???????orders0_.o_customer_id as o_custom4_1_0_, ???????orders0_.o_id as o_id1_1_0_, ???????orders0_.o_id as o_id1_1_1_, ???????orders0_.o_money as o_money2_1_1_, ???????orders0_.o_receiverInfo as o_receiv3_1_1_, ???????orders0_.o_customer_id as o_custom4_1_1_ ????from ???????test.o_order orders0_ ????where ???????orders0_.o_customer_id=?Hibernate: ????select ???????orders0_.o_customer_id as o_custom4_1_0_, ???????orders0_.o_id as o_id1_1_0_, ???????orders0_.o_id as o_id1_1_1_, ???????orders0_.o_money as o_money2_1_1_, ???????orders0_.o_receiverInfo as o_receiv3_1_1_, ???????orders0_.o_customer_id as o_custom4_1_1_ ????from ???????test.o_order orders0_ ????where ???????orders0_.o_customer_id=?

  这样在查询customer时会进行查询order

测试lazy=extra属性

<set lazy="true" > //设置延迟加载

  执行以下方法

      @Testpublic void test15(){Session session = HibernateUtils.getSession();Transaction bt = session.beginTransaction();String hql="from Customer";Query query = session.createQuery(hql);List<Customer> list = query.list();/*int size = list.size();System.out.println(size);*/for (Customer customer : list) {System.out.println(customer.getOrders().size());}//操作bt.commit();session.close();}

  sql打印:

Hibernate: ????select ???????customer0_.c_id as c_id1_0_, ???????customer0_.name as name2_0_ ????from ???????test.c_customer customer0_Hibernate: ????select ???????orders0_.o_customer_id as o_custom4_1_0_, ???????orders0_.o_id as o_id1_1_0_, ???????orders0_.o_id as o_id1_1_1_, ???????orders0_.o_money as o_money2_1_1_, ???????orders0_.o_receiverInfo as o_receiv3_1_1_, ???????orders0_.o_customer_id as o_custom4_1_1_ ????from ???????test.o_order orders0_ ????where ???????orders0_.o_customer_id=?1Hibernate: ????select ???????orders0_.o_customer_id as o_custom4_1_0_, ???????orders0_.o_id as o_id1_1_0_, ???????orders0_.o_id as o_id1_1_1_, ???????orders0_.o_money as o_money2_1_1_, ???????orders0_.o_receiverInfo as o_receiv3_1_1_, ???????orders0_.o_customer_id as o_custom4_1_1_ ????from ???????test.o_order orders0_ ????where ???????orders0_.o_customer_id=?100

  

<set lazy="extra" > //设置加强延迟加载

  sql打印为:

Hibernate: ????select ???????customer0_.c_id as c_id1_0_, ???????customer0_.name as name2_0_ ????from ???????test.c_customer customer0_Hibernate: ????select ???????count(o_id) ????from ???????test.o_order ????where ???????o_customer_id =?1Hibernate: ????select ???????count(o_id) ????from ???????test.o_order ????where ???????o_customer_id =?100

  

总结: ???对于懒加载和加强懒加载区别:都是是在调用时才会产生,但是区别在于发送sql语句的意义;懒加载在发送sql语句时会发送查询全部的语句,返回为每一列,而加强懒加载发送的是count(*),我需要什么加强懒加载会给我什么直接查询,而懒加载不会.

  

测试fetch    只是sql的生成方式不同而已

 <set name="orders" ?lazy="false" fetch="subselect" > ?//subselect生成子查询
     @Testpublic void test15(){Session session = HibernateUtils.getSession();Transaction bt = session.beginTransaction();String hql="from Customer";Query query = session.createQuery(hql);List<Customer> list = query.list();bt.commit();session.close();}

  sql打印:

Hibernate: ????select ???????customer0_.c_id as c_id1_0_, ???????customer0_.name as name2_0_ ????from ???????test.c_customer customer0_Hibernate: ????select ???????orders0_.o_customer_id as o_custom4_1_1_, ???????orders0_.o_id as o_id1_1_1_, ???????orders0_.o_id as o_id1_1_0_, ???????orders0_.o_money as o_money2_1_0_, ???????orders0_.o_receiverInfo as o_receiv3_1_0_, ???????orders0_.o_customer_id as o_custom4_1_0_ ????from ???????test.o_order orders0_ ????where ???????orders0_.o_customer_id in (    //子查询 ???????????select ???????????????customer0_.c_id ????????????from ???????????????test.c_customer customer0_ ???????)

  

                                  在<many-to-one>或<one-to-one>如果去查询对方

fetch
select 
join 
lazy
false立即加载
proxy是否采用延迟,由另一方决定
no-proxy不做研究

hibernate检索策略(抓取策略)

原文地址:http://www.cnblogs.com/fjkgrbk/p/hibernate_lazy.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved