Hibernate--延迟加载lazy(类级别的延迟加载、关联级别的延迟加载)和抓取策略fetch详解 桃扇骨 2021-08-13 22:52 575阅读 0赞 #### 延迟加载 #### 延迟加载(lazy load)是(也成为懒加载)Hibernate关联关系对象默认的加载方式,延迟加载机制是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作。 通常将延迟加载分为两类:\*\*一类叫做类级别延迟,另一个叫做关联级别的延迟。\*\*类级别的延迟指的是查询某个对象的时候,是否采用有延迟,这个通常在< class >标签上配置lazy属性。关联级别的延迟指的是,查询一个对象的关联对象的是否采用延迟加载。这个通常在< set >或 < many-to-one >上配置lazy属性。 #### 类级别的延迟加载 #### 使用load方法检索某个对象的时候,这个类是否采用延迟加载的策略,就是类级别的延迟。类级别的延迟一般在< class >上配置lazy属性,lazy的默认是true。默认是延迟加载的,所以使用load方法去查询的时候,不会马上发送SQL语句,当真正使用该对象的时候,才会发送SQL语句。 Customer customer = session.load(Customer.class, 1l); 如果不想使用延迟加载也有很多方法,最简单的就是将这个类的映射文件上的lazy属性设置为false,当然也可以将这个持久化类改为final修饰,这样该类就无法生成代理,使延迟加载失效。 #### 关联级别的延迟加载 #### Customer customer = session.get(Customer.class, 1l); Set<LinkMan> linkMans = customer.getLinkMans(); 通过客户查询其关联的联系人对象,在查询联系人的时候是否采用延迟加载成为关联级别的延迟。关联级别的延迟通常是在< set > 和 < many-to-one >上进行配置。 **< set >标签上的lazy通常有三个取值:** * true:默认值,采用延迟加载 * false:检索关联对象的时候,不采用延迟加载 * extra:及其懒惰的 **< many-to-one >标签上的lazy通常有三个取值:** * proxy:默认值,是否采用延迟取决于一的一方类上的lazy属性的值 * false:检索关联对象的时候,不采用延迟加载 * no-proxy:基本不使用 #### 抓取策略 #### 抓取策略是当应用程序需要在(Hibernate实体对象图的)关联关系间进行导航的时候,Hibernate如何获取关联对象的策略。 Hibernate的抓取策略是Hibernate提升性能的一种手段,可以在获取关联对象的时候,对发送的语句进行优化。 抓取策略通过在关联对象的标签上配置fetch属性完成。关联上分为是在< set > 和 < many-to-one >上,也都有不同的取值。 **< set >标签上的fetch通常有三个取值:** * select:默认值,发送的是普通的select语句查询 * join:发送一条迫切左外连接区查询 * subselect:发送一条子查询语句查询其关联对象 **< many-to-one >标签上的fetch有两个取值:** * select:默认值,发送一条普通的select语句查询关联对象 * join:发送一条迫切左外连接语句查询其关联对象 #### 测试 #### < set >上配置的fetch有三个值,lazy有三个值,这样就会产生很多种效果,但是,如果fetch设置为join,lazy就会失效。 #### 创建表 #### cst\_customer与cst\_linkman是一对多关系,cust\_id为外键。 **cst\_customer表:** ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NvbGRfX19wbGF5_size_16_color_FFFFFF_t_70] **cst\_linkman表:** ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NvbGRfX19wbGF5_size_16_color_FFFFFF_t_70 1] #### 创建实体类 #### **Customer类:** public class Customer { //主键 private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; //使用set集合,表达一对多关系 private Set<LinkMan> linkMens = new HashSet<LinkMan>(); public Set<LinkMan> getLinkMens() { return linkMens; } public void setLinkMens(Set<LinkMan> linkMens) { this.linkMens = linkMens; } public Long getCust_id() { return cust_id; } public void setCust_id(Long cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_linkman() { return cust_linkman; } public void setCust_linkman(String cust_linkman) { this.cust_linkman = cust_linkman; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } } **LinkMan类:** //联系人实体 public class LinkMan { //主键 private Long lkm_id; private Character lkm_gender; private String lkm_name; private String lkm_phone; private String lkm_email; private String lkm_qq; private String lkm_mobile; private String lkm_memo; private String lkm_position; //表达多对一关系,外键 private Customer customer ; public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public Long getLkm_id() { return lkm_id; } public void setLkm_id(Long lkm_id) { this.lkm_id = lkm_id; } public Character getLkm_gender() { return lkm_gender; } public void setLkm_gender(Character lkm_gender) { this.lkm_gender = lkm_gender; } public String getLkm_name() { return lkm_name; } public void setLkm_name(String lkm_name) { this.lkm_name = lkm_name; } public String getLkm_phone() { return lkm_phone; } public void setLkm_phone(String lkm_phone) { this.lkm_phone = lkm_phone; } public String getLkm_email() { return lkm_email; } public void setLkm_email(String lkm_email) { this.lkm_email = lkm_email; } public String getLkm_qq() { return lkm_qq; } public void setLkm_qq(String lkm_qq) { this.lkm_qq = lkm_qq; } public String getLkm_mobile() { return lkm_mobile; } public void setLkm_mobile(String lkm_mobile) { this.lkm_mobile = lkm_mobile; } public String getLkm_memo() { return lkm_memo; } public void setLkm_memo(String lkm_memo) { this.lkm_memo = lkm_memo; } public String getLkm_position() { return lkm_position; } public void setLkm_position(String lkm_position) { this.lkm_position = lkm_position; } } #### 配置ORM元数据: #### **Customer.hbm.xml:** <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="pers.zhang.domain" > <class name="Customer" table="cst_customer" > <id name="cust_id" > <generator class="native"></generator> </id> <property name="cust_name" column="cust_name" ></property> <property name="cust_source" column="cust_source" ></property> <property name="cust_industry" column="cust_industry" ></property> <property name="cust_level" column="cust_level" ></property> <property name="cust_linkman" column="cust_linkman" ></property> <property name="cust_phone" column="cust_phone" ></property> <property name="cust_mobile" column="cust_mobile" ></property> <set name="linkMens" inverse="true" cascade="all" > <key column="lkm_cust_id" ></key> <one-to-many class="LinkMan" /> </set> </class> </hibernate-mapping> **LinkMan.hbm.xml:** <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="pers.zhang.domain" > <class name="LinkMan" table="cst_linkman" > <id name="lkm_id" > <generator class="native"></generator> </id> <property name="lkm_gender" ></property> <property name="lkm_name" ></property> <property name="lkm_phone" ></property> <property name="lkm_email" ></property> <property name="lkm_qq" ></property> <property name="lkm_mobile" ></property> <property name="lkm_memo" ></property> <property name="lkm_position" ></property> <many-to-one name="customer" column="lkm_cust_id" class="Customer" ></many-to-one> </class> </hibernate-mapping> **准备数据:** ![在这里插入图片描述][20191007133047866.png] ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NvbGRfX19wbGF5_size_16_color_FFFFFF_t_70 2] **测试默认情况(未设置抓取策略和延迟加载):** @Test public void fun8() { Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //查询一号客户,发送一条select语句查询一号客户 Customer customer = session.get(Customer.class, 1l); //获得一号客户的联系人数量,发送一条select语句查询1号客户的所有联系人 System.err.println(customer.getLinkMens().size()); tx.commit(); } 运行测试输出: 7 **< set >上配置fetch=“select” lazy=“true”:** <set name="linkMens" lazy="true" fetch="select"> <key column="lkm_cust_id" ></key> <one-to-many class="LinkMan" /> </set> //集合级别的关联 //fetch:select 单表查询 //lazy:true 使用时才加载集合数据. @Test public void fun1(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- Customer c = session.get(Customer.class, 2l); Set<LinkMan> linkMens = c.getLinkMens();//关联级别 System.out.println(linkMens); //---------------------------------------------------- tx.commit(); session.close(); } 通过在打印语句前设置断点可知,在打印前并未加载集合数据,确认为懒加载。 **< set >上配置fetch=“select” lazy=“false”:** <set name="linkMens" lazy="false" fetch="select"> <key column="lkm_cust_id" ></key> <one-to-many class="LinkMan" /> </set> 立即加载数据 **< set >上配置fetch=“select” lazy=“extra”:** <set name="linkMens" lazy="extra" fetch="select"> <key column="lkm_cust_id" ></key> <one-to-many class="LinkMan" /> </set> //fetch:select 单表查询 //lazy:extra 极其懒惰.与懒加载效果基本一致. 如果只获得集合的size.只查询集合的size(count语句) @Test public void fun3(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- Customer c = session.get(Customer.class, 2l); Set<LinkMan> linkMens = c.getLinkMens();//关联级别 System.out.println(linkMens.size()); //---------------------------------------------------- tx.commit(); session.close(); } 运行测试,Hibernate打印: Hibernate: select customer0_.cust_id as cust_id1_0_0_, customer0_.cust_name as cust_nam2_0_0_, customer0_.cust_source as cust_sou3_0_0_, customer0_.cust_industry as cust_ind4_0_0_, customer0_.cust_level as cust_lev5_0_0_, customer0_.cust_linkman as cust_lin6_0_0_, customer0_.cust_phone as cust_pho7_0_0_, customer0_.cust_mobile as cust_mob8_0_0_ from cst_customer customer0_ where customer0_.cust_id=? Hibernate: select count(lkm_id) from cst_linkman where lkm_cust_id =? 8 可见获得集合size就只查询集合的size **< set >上配置fetch=“join” lazy=“true| false | extra”:** 当fetch设置为join时,lazy属性失效,立即加载 **< set >上配置fetch=“subselect” lazy=“true”:** <set name="linkMens" lazy="true" fetch="subselect"> <key column="lkm_cust_id" ></key> <one-to-many class="LinkMan" /> </set> @Test //fetch: subselect 子查询 //lazy: false 立即加载 public void fun6(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- String hql = "from Customer"; Query query = session.createQuery(hql); List<Customer> list = query.list();//设置断点,此处查询到所有Customer for(Customer c:list){ System.out.println(c);//Customer已经加载了,直接打印 System.out.println(c.getLinkMens().size());//通过子查询,查询所有linkMan System.out.println(c.getLinkMens());//打印 } //---------------------------------------------------- tx.commit(); session.close(); } Hibernate: select customer0_.cust_id as cust_id1_0_, customer0_.cust_name as cust_nam2_0_, customer0_.cust_source as cust_sou3_0_, customer0_.cust_industry as cust_ind4_0_, customer0_.cust_level as cust_lev5_0_, customer0_.cust_linkman as cust_lin6_0_, customer0_.cust_phone as cust_pho7_0_, customer0_.cust_mobile as cust_mob8_0_ from cst_customer customer0_ //此处为断点 Customer [cust_id=1, cust_name=百度, cust_level=null] Hibernate: select linkmens0_.lkm_cust_id as lkm_cus10_1_1_, linkmens0_.lkm_id as lkm_id1_1_1_, linkmens0_.lkm_id as lkm_id1_1_0_, linkmens0_.lkm_gender as lkm_gend2_1_0_, linkmens0_.lkm_name as lkm_name3_1_0_, linkmens0_.lkm_phone as lkm_phon4_1_0_, linkmens0_.lkm_email as lkm_emai5_1_0_, linkmens0_.lkm_qq as lkm_qq6_1_0_, linkmens0_.lkm_mobile as lkm_mobi7_1_0_, linkmens0_.lkm_memo as lkm_memo8_1_0_, linkmens0_.lkm_position as lkm_posi9_1_0_, linkmens0_.lkm_cust_id as lkm_cus10_1_0_ from cst_linkman linkmens0_ where linkmens0_.lkm_cust_id in ( select customer0_.cust_id from cst_customer customer0_ ) **< set >上配置fetch=“subselect” lazy=“false”:** @Test //fetch: subselect 子查询 //lazy: false 立即加载 public void fun6(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- String hql = "from Customer"; Query query = session.createQuery(hql); List<Customer> list = query.list();//断点,查询出所有的Customer及所有的LinkMan for(Customer c:list){ //直接打印即可 System.out.println(c); System.out.println(c.getLinkMens().size()); System.out.println(c.getLinkMens()); } //---------------------------------------------------- tx.commit(); session.close(); } **< set >上配置fetch=“subselect” lazy=“extra”:** @Test //fetch: subselect 子查询 //lazy: extra 极其懒惰 public void fun7(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- String hql = "from Customer"; Query query = session.createQuery(hql); List<Customer> list = query.list();//断点,只查询Customer for(Customer c:list){ System.out.println(c);//打印 System.out.println(c.getLinkMens().size());//断点,只查询size System.out.println(c.getLinkMens());//查询具体数据 } //---------------------------------------------------- tx.commit(); session.close(); } **< many-to-one >上配置fetch=“select” lazy=“proxy” Customer上的lazy=“true”:(默认情况)**: @Test //fetch:select 单表查询 //lazy:proxy //customer-true 懒加载 public void fun1(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- LinkMan lm = session.get(LinkMan.class, 3l);//断点,只查询LinkMan Customer customer = lm.getCustomer();//获得关联的Customer,不加载 System.out.println(customer);//使用Customer时加载 //---------------------------------------------------- tx.commit(); session.close(); } **< many-to-one >上配置fetch=“select” lazy=“proxy” Customer上的lazy=“false”:** @Test //fetch:select 单表查询 //lazy:proxy //customer-false 立即加载 public void fun1(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- LinkMan lm = session.get(LinkMan.class, 3l);//断点,加载LinkMan和对应的Customer(单表查询) Customer customer = lm.getCustomer(); System.out.println(customer);//直接使用 //---------------------------------------------------- tx.commit(); session.close(); } **< many-to-one >上配置fetch=“join” lazy=“失效” :** @Test //fetch:join 多表 //lazy: 失效 public void fun3(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- LinkMan lm = session.get(LinkMan.class, 3l);//多表查询出LinkMan和对应的Customer Customer customer = lm.getCustomer(); System.out.println(customer); //---------------------------------------------------- tx.commit(); session.close(); } [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NvbGRfX19wbGF5_size_16_color_FFFFFF_t_70]: /images/20210813/0ff964275d4c46668755fc7c5c449703.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NvbGRfX19wbGF5_size_16_color_FFFFFF_t_70 1]: /images/20210813/4f74ac6970ee4d66976fda00b2146337.png [20191007133047866.png]: /images/20210813/8aaa9f47ef784ee1951d923f39a93998.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NvbGRfX19wbGF5_size_16_color_FFFFFF_t_70 2]: /images/20210813/6c44b24860974c6cb60a652d8160e321.png
还没有评论,来说两句吧...