专栏首页HongtenHibernate延迟加载 lazy loading

Hibernate延迟加载 lazy loading

延迟加载在Hibernate中是默认延迟加载;

测试代码一:

HibernateTest.java

代码:

/**  *  */ package com.b510.examples;

import java.util.Set;

import org.hibernate.Session;

/**  *  * @author XHW  *  * @date 2011-7-18  *  */ public class HibernateTest {  public static void main(String[] args) {   new HibernateTest().update();  }  public void update(){   Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();   session.beginTransaction();  Category category=(Category)session.get(Category.class, 1);   System.out.println("id:"+category.getId()+"  ,name:"+category.getName()+", description:"+category.getDescription());   Set<Product> products=category.getProducts();   session.getTransaction().commit();    } }

运行结果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Hibernate:     select         category0_.id as id1_0_,         category0_.name as name1_0_,         category0_.description as descript3_1_0_     from         users.category category0_     where         category0_.id=? id:1  ,name:java, description:java好啊

这里我们看到我们关心的是id,name和description属性,

虽然有:  Set<Product> products=category.getProducts(); 代码,即:不处理集合对象。但是我们只要的是:

  System.out.println("id:"+category.getId()+"  ,name:"+category.getName()+", description:"+category.getDescription()); 输出的是id,name和description属性值,其他的我们不管,所以Hibernate用了lazy loading(延迟加载),带来的好处就是我们不关心的

数据,不用现在加载,当我们要用的时候,才去加载

测试代码二:

HibernateTest.java

代码:

/**  *  */ package com.b510.examples;

import java.util.Set;

import org.hibernate.Session;

/**  *  * @author XHW  *  * @date 2011-7-18  *  */ public class HibernateTest {  public static void main(String[] args) {   new HibernateTest().update();  }  public void update(){   Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();   session.beginTransaction();   Category category=(Category)session.get(Category.class, 1);   System.out.println("id:"+category.getId()+"  ,name:"+category.getName()+", description:"+category.getDescription());   Set<Product> products=category.getProducts();   for(Product product:products){    System.out.println("ID:  "+product.getId()+"  name:"+product.getName()+" price: "+product.getPrice());   }     session.getTransaction().commit();    } }

运行效果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Hibernate:     select         category0_.id as id1_0_,         category0_.name as name1_0_,         category0_.description as descript3_1_0_     from         users.category category0_     where         category0_.id=? id:1  ,name:java, description:java好啊 Hibernate:  select         products0_.category_id as category2_1_,         products0_.id as id1_,         products0_.id as id0_0_,         products0_.category_id as category2_0_0_,         products0_.name as name0_0_,         products0_.price as price0_0_,         products0_.descripton as descripton0_0_     from         users.product products0_     where         products0_.category_id=? ID:  1  name:java SE应用程序设计 price: 78.00 这里可以明确的告诉我们,当我们要加载Set集合的时候,这时候才去加载,而上面的例子,说明的是我们不加载的时候

Hibernate就延迟加载

取消延迟加载:

Category.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="com.b510.examples.Category" table="category" catalog="users">         <id name="id" type="java.lang.Integer">             <column name="id" />             <generator class="increment" />         </id>         <property name="name" type="java.lang.String">             <column name="name" length="500" />         </property>         <property name="description" type="java.lang.String">             <column name="description" length="500" />         </property>         <set name="products" inverse="true" lazy="false">             <key>                 <column name="category_id" />             </key>             <one-to-many class="com.b510.examples.Product" />         </set>     </class> </hibernate-mapping>

测试代码:

HIbernateTest.java

代码:

/**  *  */ package com.b510.examples;

import java.util.Set;

import org.hibernate.Session;

/**  *  * @author XHW  *  * @date 2011-7-18  *  */ public class HibernateTest {  public static void main(String[] args) {   new HibernateTest().update();  }  public void update(){   Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();   session.beginTransaction();   Category category=(Category)session.get(Category.class, 1);   System.out.println("id:"+category.getId()+"  ,name:"+category.getName()+", description:"+category.getDescription());   Set<Product> products=category.getProducts();   session.getTransaction().commit();    } }

运行效果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Hibernate:     select         category0_.id as id1_0_,         category0_.name as name1_0_,         category0_.description as descript3_1_0_     from         users.category category0_     where         category0_.id=? Hibernate:     select         products0_.category_id as category2_1_,         products0_.id as id1_,         products0_.id as id0_0_,         products0_.category_id as category2_0_0_,         products0_.name as name0_0_,         products0_.price as price0_0_,         products0_.descripton as descripton0_0_     from         users.product products0_     where         products0_.category_id=? id:1  ,name:java, description:java好啊 和测试代码一的运行结果相互比较,我们会发现,这次运行结果用了两条select语句。但是我们会发现

第二条select语句,对于我们的需求是没有必要的,他只有一个用处就是占用我们的程序执行时间。当然,

这是我们不希望看到的结果。

一般情况下,Hibernate会默认给我们设置延迟加载。lazy="true" ,这样会提升我们的系统性能,所以一般情况下,我们不会去

设置lazy="false",当然在特殊的情况下,我们必须要取消延迟加载的时候,我们就把lazy="false",就可以了

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Hibernate 中集合对象的抓取策略(Fetching strategies)

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

    Hongten
  • Hibernate中的一级缓存

    在一个会话的生命期里面,他所用到的数据会使用缓冲的,第一次读的是编号为2的数据记录

    Hongten
  • Hibernate的二级缓存

    /**  *  * @author XHW  *  * @date 2011-7-15  *  */ public class HibernateTest {

    Hongten
  • SQL Server merge用法

    有两个表名:source 表和 target 表,并且要根据 source 表中匹配的值更新 target 表。

    Vincent-yuan
  • Linux系列之学会使用CURL命令

    curl命令是一个利用URL规则在shell终端命令行下工作的文件传输工具;curl命令作为一款强力工具,curl支持包括HTTP、HTTPS、ftp等众多协议...

    SmileNicky
  • 从Yoav Goldberg与Yann LeCun争论,看当今的深度学习、NLP与arXiv风气

    选自Medium、Facebook 机器之心编译 作者:Yoav Goldberg、Yann LeCun 参与:黄小天、吴攀、晏奇 最近,来自以色列 Bar ...

    机器之心
  • 第17讲 for循环优化:数据流

    ug902(v2018.1) High-Level Synthesis User Guide page 313 - page 321

    Lauren的FPGA
  • 第18讲 for循环优化:嵌套的for循环

    ug902(v2018.1) High-Level Synthesis User Guide page 313 - page 321

    Lauren的FPGA
  • 第19讲 for循环优化:其他优化方法

    ug902(v2018.1) High-Level Synthesis User Guide page 313 - page 321

    Lauren的FPGA
  • Leetcode: Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear ...

    卡尔曼和玻尔兹曼谁曼

扫码关注云+社区

领取腾讯云代金券