专栏首页HongtenHibernate 中集合对象的抓取策略(Fetching strategies)

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 条评论
登录 后参与评论

相关文章

  • Hibernate Annotation (Hibernate 注解)

    英文:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

    Hongten
  • Java Web Commons-Utils (数据库连接方法)

        数据库驱动类:Oracle: ojdbc6.jar                   MySQL: mysql-connector-java-5.1....

    Hongten
  • Hibernate延迟加载 lazy loading

    /**  *  * @author XHW  *  * @date 2011-7-18  *  */ public class HibernateTest...

    Hongten
  • Autowired注解

    package com.how2java.pojo; import org.springframework.beans.factory.annotation...

    互联网金融打杂
  • Spring BeanPostProcessor 的执行顺序

    实际上在实战中,我们很少处理 Spring 的初始化操作,所以没有必要区分 Before 还是 After。只需要实现其中的一个即可,显然选 After 方法更...

    wsuo
  • Oozie分布式任务的工作流——Spark篇

    Spark是现在应用最广泛的分布式计算框架,oozie支持在它的调度中执行spark。在我的日常工作中,一部分工作就是基于oozie维护好每天的spark离线任...

    用户1154259
  • 移动物联网模块之FOTA更新攻击

    嵌入式GPRS/EDGE、3G、4G和5G模块通常用于很多用途,包括连接设备,如对讲机、警报器、汽车ECU、车载信息娱乐系统(IVI)、租赁汽车的开锁箱(Get...

    物联网安全小编
  • mysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQLmysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQL

    在mysql中,有时我们在做数据库查询时,需要得到某字段中包含某个值的记录,但是它也不是用like能解决的,使用like可能查到我们不想要的记录,它比like更...

    一个会写诗的程序员
  • 理解Python3中的__main__

    Python3 中 以双下划线开头且结尾变量被称为 内置变量,如 init,name. 该变量的具体值由该模块被调用的方式决定。 1、当该模块被直接调用时,值为...

    py3study
  • 【Spark】Spark Local 及 Spark On Standalone 环境搭建

    (1)解压spark安装包 $ cd /opt/softwares/cdh cdh]$ tar -zxf spark-1.6.1-bin-2.5.0-cdh...

    魏晓蕾

扫码关注云+社区

领取腾讯云代金券