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条记录。

Hibernate 中集合对象的抓取策略(Fetching strategies)相关推荐

  1. Hibernate 原汁原味的四种抓取策略(转)

    原文出处:http://www.cnblogs.com/rongxh7/archive/2010/05/12/1733088.html     尊重原作者,访问原创地址 最近在研究 Hibernate ...

  2. JAVAWEB开发之Hibernate详解(三)——Hibernate的检索方式、抓取策略以及利用二级缓存进行优化、解决数据库事务并发问题

    Hibernate的检索方式  Hibernate提供了以下几种检索对象的方式: 导航对象图检索方式:根据已经加载的对象导航到其他对象. OID检索方式:按照对象的OID来检索对象. HQL检索方式: ...

  3. Hibernate抓取策略

    为什么80%的码农都做不了架构师?>>>    6.抓取策略:抓取策略主要是指获取连接对象的策略. 6.1.基于XML的抓取 1.1.基于XML抓取many-to-one sessi ...

  4. Hibernate之加载策略(延迟加载与即时加载)和抓取策略(fetch)

    假设现在有Book和Category两张表,表的关系为双向的一对多,表结构如下: 假设现在我想查询id为2的那本书的书名,使用session.get(...)方法: 1 Session session ...

  5. hibernate教程--抓取策略详解

    Hibernate的抓取策略 1.1. 区分延迟和立即检索: 立即检索: * 当执行某行代码的时候,马上发出SQL语句进行查询. * get() 延迟检索: * 当执行某行代码的时候,不会马上发出SQ ...

  6. hibernate教程--抓取策略

    Hibernate的抓取策略 1.1. 区分延迟和立即检索: 立即检索: * 当执行某行代码的时候,马上发出SQL语句进行查询. * get() 延迟检索: * 当执行某行代码的时候,不会马上发出SQ ...

  7. BASLER Pylon 抓取策略

    One by One Grab Strategy 逐个抓取策略 当使用 one by one 抓取模式时,图像按其获取顺序处理. 相机抓取引擎实例将空缓存队列中的缓存取消,并将空缓存排入低级别API流 ...

  8. (转)Hibernate框架基础——在Hibernate中java对象的状态

    http://blog.csdn.net/yerenyuan_pku/article/details/52760627 在Hibernate中java对象的状态 Hibernate把对象分为4种状态: ...

  9. JS中集合对象(Array、Map、Set)及类数组对象的使用与对比

    JS中集合对象(Array.Map.Set)及类数组对象的使用与对比 在使用js编程的时候,常常会用到集合对象,集合对象其实是一种泛型,在js中没有明确的规定其内元素的类型,但在强类型语言譬如Java ...

最新文章

  1. CSS2.0中文手册(CHM版)
  2. 升级了下 IDEA 后 Lombok 失效了...
  3. 3星|《数据思维:从数据分析到商业价值》:有趣的数据分析案例
  4. 第十五讲 傅里叶级数引入
  5. 【Paper】2021_领导-跟随多智能体系统容错一致性控制
  6. 《JAVA与模式》之抽象工厂模式
  7. stm32之PVD可编程电压监测器(掉电保存数据)
  8. Jmeter初学(一)
  9. ELK入门01—Elasticsearch安装
  10. php转移单引号,php如何转义单引号
  11. Flutter GetX 状态管理 使用入门 程序计数器 (二)
  12. 新入职了一个卷王,天天加班12点!张口闭口就是性能优化,太让人崩溃……...
  13. noi 8780 拦截导弹
  14. html打印word文档,HTML文件到WORD文档双面打印三步曲
  15. PS插件:灯光工厂安装教程
  16. 8uftp使用教程 图解,8uftp使用教程 图解
  17. 计算机英语词汇助记,英语单词查询软件
  18. 在屏幕上绘制圆形函数
  19. 新年贺卡php,PS设计2016猴年大吉新年贺卡
  20. Android MotionLayout As a Dancer

热门文章

  1. java 模板引擎_极简 Spring Boot 整合 Thymeleaf 页面模板
  2. mysql %w_MySQLDATE_FORMAT()函数
  3. 驱动利器:数据驱动下的产品决策(上)
  4. SpringMVC-web.xml头代码
  5. TensorFlow模型保存和加载方法
  6. lintcode :Partition List 链表划分
  7. jsp上传下载+SmartUpload插件上传
  8. WebSocket FlashSocket
  9. Cambridge hpc
  10. ways for partner