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

Product.java

代码;

package com.b510.examples;

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

public class Product implements java.io.Serializable {

 private static final long serialVersionUID = -1546206493725028472L;  private Integer id;  private Category category;  private String name;  private String price;  private String descripton;

 public Product() {  }

 public Integer getId() {   return this.id;  }

 public void setId(Integer id) {   this.id = id;  }

 public Category getCategory() {   return this.category;  }

 public void setCategory(Category category) {   this.category = category;  }

 public String getName() {   return this.name;  }

 public void setName(String name) {   this.name = name;  }

 public String getPrice() {   return this.price;  }

 public void setPrice(String price) {   this.price = price;  }

 public String getDescripton() {   return this.descripton;  }

 public void setDescripton(String descripton) {   this.descripton = descripton;  }

}

Category.java

代码:

package com.b510.examples;

import java.util.HashSet; import java.util.Set;

public class Category implements java.io.Serializable {

 private static final long serialVersionUID = 3240281547213597385L;  private Integer id;  private String name;  private String description;  private Set products = new HashSet(0);

 public Category() {  }

 public Integer getId() {   return this.id;  }

 public void setId(Integer id) {   this.id = id;  }

 public String getName() {   return this.name;  }

 public void setName(String name) {   this.name = name;  }

 public String getDescription() {   return this.description;  }

 public void setDescription(String description) {   this.description = description;  }

 public Set getProducts() {   return this.products;  }

 public void setProducts(Set products) {   this.products = products;  }

}

Product.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.Product" table="product" catalog="users">         <id name="id" type="java.lang.Integer">             <column name="id" />             <generator class="increment" />         </id>         <many-to-one name="category" class="com.b510.examples.Category" fetch="select">             <column name="category_id" />         </many-to-one>         <property name="name" type="java.lang.String">             <column name="name" length="500" />         </property>         <property name="price" type="java.lang.String">             <column name="price" length="10" />         </property>         <property name="descripton" type="java.lang.String">             <column name="descripton" length="500" />         </property>     </class> </hibernate-mapping>

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" >             <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();    for(Product p:products){     System.out.println(p.getName());    }   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=? java SE应用程序设计 这里我们看到 的是,我们在读取一个集合对象的 时候,hibernate用了一条selelct语句;

1、查询抓取(Select fetching)

修改: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" fetch="select">             <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();    for(Product p:products){     System.out.println(p.getName());    }   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=? java SE应用程序设计 这种方法是查询抓取,他是hibernate默认值,所以我们会看到和上面的运行效果一摸一样。

测试代码二:

HibernateTest.java

代码;

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

import java.util.Set;

import org.hibernate.Query; 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();  Query query=session.createQuery("select c from Category c inner join fetch c.products where c.id=?");   query.setInteger(0, 1);   Category category=(Category)query.uniqueResult();   System.out.println("id:"+category.getId()+"  ,name:"+category.getName()+", description:"+category.getDescription());   Set<Product> products=category.getProducts();    for(Product p:products){     System.out.println(p.getName());    }   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_,         products1_.id as id0_1_,         category0_.name as name1_0_,         category0_.description as descript3_1_0_,         products1_.category_id as category2_0_1_,         products1_.name as name0_1_,         products1_.price as price0_1_,         products1_.descripton as descripton0_1_,         products1_.category_id as category2_0__,         products1_.id as id0__     from         users.category category0_     inner join         users.product products1_             on category0_.id=products1_.category_id     where         category0_.id=? id:1  ,name:java, description:java好啊 java SE应用程序设计

我们发现现在hibernate只是用了一个select语句。这是我们通过hql语句重载底层设置的抓取策略

hql的优先级很好。尽管你我们在配置文件中设置了fetch=“select”,就是说hibernate要用两个selelct语句

去执行这样的行为,但是,我们在hql语句中设置了急迫内连接查询,这样hibernate就必须只用一条select语句去查询

这样就等于把我们底层设置进行了重载,或者说是覆盖。

2、子查询抓取(Subselect fetching)

 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" fetch="subselect">             <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.Iterator; import java.util.List; import java.util.Set;

import org.hibernate.Session; import org.hibernate.Transaction;

/**  *  * @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();   Transaction tx = session.beginTransaction();

List categorys = session.createQuery("from Category").list();   for (Iterator<Category> it = categorys.iterator(); it.hasNext();) {    Category category = it.next();    System.out.println("id:" + category.getId() + "  ,name:"      + category.getName() + ", description:"      + category.getDescription());    for (Iterator<Product> it2 = category.getProducts().iterator(); it2      .hasNext();) {     Product p = it2.next();     System.out.println(p.getName());    }   }   tx.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_,         category0_.name as name1_,         category0_.description as descript3_1_     from         users.category category0_ 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 in ( select                 category0_.id             from                 users.category category0_         ) java SE应用程序设计 id:2  ,name:Hibernate, description:Hibernate好啊 java WEB开发与实战

我们看到,下面的selelct语句中又嵌套了一个select语句

3、连接抓取(Join fetching)

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" fetch="join">             <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.Iterator; import java.util.List;

import org.hibernate.Session; import org.hibernate.Transaction;

/**  *  * @author XHW  *  * @date 2011-7-18  *  */ public class HibernateTest {  public static void main(String[] args) {   new HibernateTest().example();  }

 public void example(){   Session session =HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();   Transaction tx=session.beginTransaction();   Category category=(Category)session.get(Category.class, 1);   System.out.println("id:" + category.getId() + "  ,name:"     + category.getName() + ", description:"     + category.getDescription());   Iterator<Product> it=category.getProducts().iterator();   Product p=null;   while(it.hasNext()){    p=it.next();    System.out.println(p.getName());   }   tx.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_1_,         category0_.name as name1_1_,         category0_.description as descript3_1_1_,         products1_.category_id as category2_3_,         products1_.id as id3_,         products1_.id as id0_0_,         products1_.category_id as category2_0_0_,         products1_.name as name0_0_,         products1_.price as price0_0_,         products1_.descripton as descripton0_0_     from         users.category category0_    left outer join         users.product products1_             on category0_.id=products1_.category_id     where         category0_.id=? id:1  ,name:java, description:java好啊 java SE应用程序设计

4、批量抓取(Batch fetching)

 Category.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" batch-size="3">             <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.Iterator; import java.util.List;

import org.hibernate.Session; import org.hibernate.Transaction;

/**  *  * @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();   Transaction tx = session.beginTransaction();

  List<Category> categorys = session.createQuery("from Category").list();   for(Category category:categorys){    System.out.println(category.getProducts());   }     tx.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_,         category0_.name as name1_,         category0_.description as descript3_1_     from         users.category category0_ 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 in (   ?, ?, ?         ) [com.b510.examples.Product@d7b7d9, com.b510.examples.Product@93df2c, com.b510.examples.Product@3a1834, com.b510.examples.Product@190a0d6, com.b510.examples.Product@2f729e, com.b510.examples.Product@10deb5f, com.b510.examples.Product@b1cd0] [com.b510.examples.Product@18488ef] 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 in (       ?, ?, ?         ) [] 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 in (   ?, ?         ) batch-size="3"所以查询的时候是一次查询3条记录。

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

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Golang语言社区

Go 语言结构体

Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型。 结构体是由一系列具有相同类型或不同类型的数据构成的数据集合。 结构体表...

35012
来自专栏Spring相关

Springboot+ajax传输json数组以及单条数据的方法

3702
来自专栏算法修养

UESTC 491 Tricks in Bits

Tricks in Bits Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format...

3414
来自专栏小樱的经验随笔

HDU 1039 Easier Done Than Said?

Easier Done Than Said? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 6...

2846
来自专栏一个会写诗的程序员的博客

Koltin Any 类型Koltin Any 类型

The root of the Kotlin class hierarchy. Every Kotlin class has Any as a supercla...

802
来自专栏数据科学

使用流快速构建爬虫

var element = $('#b35ad07b-1572-46ac-83be-ba4a3d8906b4'); {"model_id": "85e8...

882
来自专栏码匠的流水账

聊聊rocketmq的FileAppender

org/apache/rocketmq/logging/inner/LoggingBuilder.java

781
来自专栏深度学习与数据挖掘实战

Graph application with Python, Neo4j, Gephi & Linkurious.js

I love Python, and to celebrate Packt Python week, I’ve spent some time developi...

1232
来自专栏猿人谷

c/c++ 宏中"#"和"##"的用法

c/c++ 宏中"#"和"##"的用法 一、一般用法 我们使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起. 用法: #i nclude<cstdio...

2195
来自专栏深度学习计算机视觉

递归求最大子串序列长度

递归求最大子串序列长度 import java.util.Scanner; /** * Created by junyi.pc on 2017/1/25....

3817

扫码关注云+社区

领取腾讯云代金券