hibernate抓取策略fetch

约定不等于承诺〃 2022-06-12 11:29 291阅读 0赞
  1. 抓取策略(fetching strategy)是指:当应用程序需要在(Hibernate实体对象图的)关联关系间经行导航的时候,hibernate如何获取关联对象的策略。抓取策略可以在O/R映射的元数据中声明,也可以在特定的HQL `条件查询(Criteria Query)`中重载声明。

Hibernate3 定义了如下几种抓取策略:

连接抓取(Join fetching) - Hibernate通过 在SELECT语句使用OUTER JOIN(外连接)来 获得对象的关联实例或者关联集合,这种情况lazy无效。

查询抓取(Select fetching) - 另外发送一条SELECT 语句抓取当前对象的关联实体或集合。除非你显式的指定lazy="false"禁止 延迟抓取(lazy fetching),否则只有当你真正访问关联关系的时候,才会执行第二条select语句。

子查询抓取(Subselect fetching) - 另外发送一条SELECT 语句抓取在前面查询到(或者抓取到)的所有实体对象的关联集合。除非你显式的指定lazy="false" 禁止延迟抓取(lazy fetching),否则只有当你真正访问关联关系的时候,才会执行第二条select语句。

批量抓取(Batch fetching) - 对查询抓取的优化方案, 通过指定一个主键或外键列表,Hibernate使用单条SELECT语句获取一批对象实例或集合。

下面来分别介绍这几种抓取

查询抓取(Select fetching)

项目结构如图

Center

所有的代码和上一节《hibernate加载策略之lazy》中的代码一样,可以参考,

现在多的一端设置fetch

Book.hbm.xml代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="com.myeclipse.pojo">
  6. <class name="Book" table="t_book">
  7. <id name="id">
  8. <generator class="identity" />
  9. </id>
  10. <many-to-one name="category" class="Category" column="category_id"
  11. cascade="save-update" fetch="select" lazy="false"/>
  12. <property name="author" />
  13. <property name="name" column="book_name" />
  14. <property name="price" />
  15. <property name="pubDate" />
  16. <!-- 使用过滤器 -->
  17. <filter name="bookFilter" condition="id=:id"></filter>
  18. </class>
  19. <!-- 过滤器定义 : 定义参数 -->
  20. <filter-def name="bookFilter">
  21. <filter-param name="id" type="integer" />
  22. </filter-def>
  23. </hibernate-mapping>

Category.hbm.xml代码:

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="com.myeclipse.pojo">
  6. <class name="Category" >
  7. <id name="id" >
  8. <generator class="identity" />
  9. </id>
  10. <property name="name" />
  11. <set name="books" inverse="true" >
  12. <key>
  13. <column name="category_id" />
  14. </key>
  15. <one-to-many class="Book" />
  16. </set>
  17. </class>
  18. </hibernate-mapping>

HibernateTest中TestCreateDB代码:

  1. @Test
  2. public void testCreateDB() {
  3. Configuration cfg = new Configuration().configure();
  4. SchemaExport se = new SchemaExport(cfg);
  5. // 第一个参数:是否生成ddl脚本
  6. // 第二个参数:是否执行到数据库中
  7. se.create(true, true);
  8. }

使用Junit4执行,重新生成数据库表。

控制台打印的sql语句如下:

  1. alter table t_book
  2. drop
  3. foreign key FK_cm584cq6cv5yht4jrqal0ocaq
  4. drop table if exists Category
  5. drop table if exists t_book
  6. create table Category (
  7. id integer not null auto_increment,
  8. name varchar(255),
  9. primary key (id)
  10. )
  11. create table t_book (
  12. id integer not null auto_increment,
  13. category_id integer,
  14. author varchar(255),
  15. book_name varchar(255),
  16. price double precision,
  17. pubDate datetime,
  18. primary key (id)
  19. )
  20. alter table t_book
  21. add constraint FK_cm584cq6cv5yht4jrqal0ocaq
  22. foreign key (category_id)
  23. references Category (id)

HibernateTest类中的TestSave()方法代码:

  1. /**
  2. * 保存数据
  3. */
  4. @Test
  5. public void testSave() {
  6. Session session = HibernateUtil.getSession();
  7. Transaction tx = session.beginTransaction();
  8. Category category = new Category();
  9. category.setName("文学");
  10. Category category1 = new Category();
  11. category1.setName("历史");
  12. Category category2 = new Category();
  13. category2.setName("仙侠");
  14. Category category3 = new Category();
  15. category3.setName("科幻");
  16. Category category4 = new Category();
  17. category4.setName("恐怖");
  18. Book book = new Book();
  19. book.setName("读者");
  20. book.setPrice(5.6);
  21. book.setAuthor("众人");
  22. book.setPubDate(new Date());
  23. book.setCategory(category);
  24. Book book1 = new Book();
  25. book1.setName("傲慢与偏见");
  26. book1.setPrice(80.0);
  27. book1.setAuthor("简.奥斯汀");
  28. book1.setPubDate(new Date());
  29. book1.setCategory(category1);
  30. Book book2 = new Book();
  31. book2.setName("中国历史");
  32. book2.setPrice(30.0);
  33. book2.setAuthor("人民出版社");
  34. book2.setPubDate(new Date());
  35. book2.setCategory(category1);
  36. Book book3 = new Book();
  37. book3.setName("翩眇之旅");
  38. book3.setPrice(70.0);
  39. book3.setAuthor("萧鼎");
  40. book3.setPubDate(new Date());
  41. book3.setCategory(category2);
  42. Book book4 = new Book();
  43. book4.setName("蓝血人");
  44. book4.setPrice(60.0);
  45. book4.setAuthor("卫斯理");
  46. book4.setPubDate(new Date());
  47. book4.setCategory(category3);
  48. Book book5 = new Book();
  49. book5.setName("我的大学");
  50. book5.setPrice(60.5);
  51. book5.setAuthor("高尔基");
  52. book5.setPubDate(new Date());
  53. book5.setCategory(category);
  54. session.save(book);
  55. session.save(book1);
  56. session.save(book2);
  57. session.save(book3);
  58. session.save(book4);
  59. session.save(book5);
  60. session.save(category4);
  61. tx.commit();
  62. HibernateUtil.closeSession();
  63. }

执行保存数据,sql语句如下:

  1. Hibernate:
  2. insert
  3. into
  4. Category
  5. (name)
  6. values
  7. (?)
  8. Hibernate:
  9. insert
  10. into
  11. t_book
  12. (category_id, author, book_name, price, pubDate)
  13. values
  14. (?, ?, ?, ?, ?)
  15. Hibernate:
  16. insert
  17. into
  18. Category
  19. (name)
  20. values
  21. (?)
  22. Hibernate:
  23. insert
  24. into
  25. t_book
  26. (category_id, author, book_name, price, pubDate)
  27. values
  28. (?, ?, ?, ?, ?)
  29. Hibernate:
  30. insert
  31. into
  32. t_book
  33. (category_id, author, book_name, price, pubDate)
  34. values
  35. (?, ?, ?, ?, ?)
  36. Hibernate:
  37. insert
  38. into
  39. Category
  40. (name)
  41. values
  42. (?)
  43. Hibernate:
  44. insert
  45. into
  46. t_book
  47. (category_id, author, book_name, price, pubDate)
  48. values
  49. (?, ?, ?, ?, ?)
  50. Hibernate:
  51. insert
  52. into
  53. Category
  54. (name)
  55. values
  56. (?)
  57. Hibernate:
  58. insert
  59. into
  60. t_book
  61. (category_id, author, book_name, price, pubDate)
  62. values
  63. (?, ?, ?, ?, ?)
  64. Hibernate:
  65. insert
  66. into
  67. t_book
  68. (category_id, author, book_name, price, pubDate)
  69. values
  70. (?, ?, ?, ?, ?)
  71. Hibernate:
  72. insert
  73. into
  74. Category
  75. (name)
  76. values
  77. (?)

数据库表中的数据如图:

Center 1

HibernateTest类中的查询代码如下:

  1. /**
  2. * 查询图书
  3. */
  4. @Test
  5. public void testLoadBook() {
  6. Session session = HibernateUtil.getSession();
  7. Transaction tx = session.beginTransaction();
  8. Book book = (Book) session.load(Book.class, 1) ;
  9. System.out.println("-------------------------");
  10. System.out.println("bookName = "+book.getName());
  11. System.out.println("===========================");
  12. System.out.println("categoryName = "+book.getCategory().getName());
  13. tx.commit();
  14. HibernateUtil.closeSession();
  15. }

打印的sql语句如下:

  1. INFO: HHH000232: Schema update complete
  2. -------------------------
  3. Hibernate:
  4. select
  5. book0_.id as id1_1_0_,
  6. book0_.category_id as category2_1_0_,
  7. book0_.author as author3_1_0_,
  8. book0_.book_name as book_nam4_1_0_,
  9. book0_.price as price5_1_0_,
  10. book0_.pubDate as pubDate6_1_0_
  11. from
  12. t_book book0_
  13. where
  14. book0_.id=?
  15. Hibernate:
  16. select
  17. category0_.id as id1_0_0_,
  18. category0_.name as name2_0_0_
  19. from
  20. Category category0_
  21. where
  22. category0_.id=?
  23. bookName = 读者
  24. ===========================
  25. categoryName = 文学

从sql语句可以看出,

当执行Book book = (Book) session.load(Book.class, 1) ;这句话时,并没有打印sql语句,

当查询book.getName(),图书的Name时,打印了查询sql语句:

  1. Hibernate:
  2. select
  3. book0_.id as id1_1_0_,
  4. book0_.category_id as category2_1_0_,
  5. book0_.author as author3_1_0_,
  6. book0_.book_name as book_nam4_1_0_,
  7. book0_.price as price5_1_0_,
  8. book0_.pubDate as pubDate6_1_0_
  9. from
  10. t_book book0_
  11. where
  12. book0_.id=?
  13. Hibernate:
  14. select
  15. category0_.id as id1_0_0_,
  16. category0_.name as name2_0_0_
  17. from
  18. Category category0_
  19. where
  20. category0_.id=?

这两条sql语句可以看出,先根据book的id 查询了t_book表,t_book表中有category_id,又根据category_id查询了Category表。

当我们把Book.hbm.xml中的lazy改成proxy时,如图:

Center 2

执行TestLoadBook()方法,打印的sql语句,如下:

  1. -------------------------
  2. Hibernate:
  3. select
  4. book0_.id as id1_1_0_,
  5. book0_.category_id as category2_1_0_,
  6. book0_.author as author3_1_0_,
  7. book0_.book_name as book_nam4_1_0_,
  8. book0_.price as price5_1_0_,
  9. book0_.pubDate as pubDate6_1_0_
  10. from
  11. t_book book0_
  12. where
  13. book0_.id=?
  14. bookName = 读者
  15. ===========================
  16. Hibernate:
  17. select
  18. category0_.id as id1_0_0_,
  19. category0_.name as name2_0_0_
  20. from
  21. Category category0_
  22. where
  23. category0_.id=?
  24. categoryName = 文学

此时当需要获取对应的数据时,才会执行相应的sql语句。

下面在一的一端设置fetch

Category.hbm.xml代码如下:

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="com.myeclipse.pojo">
  6. <class name="Category" >
  7. <id name="id" >
  8. <generator class="identity" />
  9. </id>
  10. <property name="name" />
  11. <set name="books" inverse="true" fetch="select">
  12. <key>
  13. <column name="category_id" />
  14. </key>
  15. <one-to-many class="Book" />
  16. </set>
  17. </class>
  18. </hibernate-mapping>

fetch设置为select,

testGetCategory()方法的代码如下:

  1. @Test
  2. public void testGetCategory() {
  3. Session session = HibernateUtil.getSession();
  4. Transaction tx = session.beginTransaction();
  5. Category category = (Category) session.get(Category.class, 1);
  6. System.out.println("---------------------------------------------");
  7. System.out.println("category_name=" + category.getName());
  8. System.out.println("============================================");
  9. for (Iterator<Book> iter = category.getBooks().iterator(); iter
  10. .hasNext();) {
  11. System.out.println(iter.next().getName());
  12. }
  13. tx.commit();
  14. HibernateUtil.closeSession();
  15. }

打印的sql语句如下:

  1. Hibernate:
  2. select
  3. category0_.id as id1_0_0_,
  4. category0_.name as name2_0_0_
  5. from
  6. Category category0_
  7. where
  8. category0_.id=?
  9. ---------------------------------------------
  10. category_name=文学
  11. ============================================
  12. Hibernate:
  13. select
  14. books0_.category_id as category2_0_0_,
  15. books0_.id as id1_1_0_,
  16. books0_.id as id1_1_1_,
  17. books0_.category_id as category2_1_1_,
  18. books0_.author as author3_1_1_,
  19. books0_.book_name as book_nam4_1_1_,
  20. books0_.price as price5_1_1_,
  21. books0_.pubDate as pubDate6_1_1_
  22. from
  23. t_book books0_
  24. where
  25. books0_.category_id=?
  26. 我的大学
  27. 读者

从上面可以看出来,get方法,当时就查询了Category表的sql语句,当需要查询Book的数据时,才会执行t_book的数据库表信息。

连接抓取(Join fetching)

先说多段的Book

Book.hbm.xml代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="com.myeclipse.pojo">
  6. <class name="Book" table="t_book">
  7. <id name="id">
  8. <generator class="identity" />
  9. </id>
  10. <many-to-one name="category" class="Category" column="category_id"
  11. cascade="save-update" fetch="join" lazy="proxy"/>
  12. <property name="author" />
  13. <property name="name" column="book_name" />
  14. <property name="price" />
  15. <property name="pubDate" />
  16. <!-- 使用过滤器 -->
  17. <filter name="bookFilter" condition="id=:id"></filter>
  18. </class>
  19. <!-- 过滤器定义 : 定义参数 -->
  20. <filter-def name="bookFilter">
  21. <filter-param name="id" type="integer" />
  22. </filter-def>
  23. </hibernate-mapping>

fetch是join,lazy是proxy

HIbernateTest类中的testLoadBook()方法,代码:

  1. /**
  2. * 查询图书
  3. */
  4. @Test
  5. public void testLoadBook() {
  6. Session session = HibernateUtil.getSession();
  7. Transaction tx = session.beginTransaction();
  8. Book book = (Book) session.load(Book.class, 1);
  9. System.out.println("-------------------------");
  10. System.out.println("bookName = " + book.getName());
  11. System.out.println("===========================");
  12. System.out.println("categoryName = " + book.getCategory().getName());
  13. tx.commit();
  14. HibernateUtil.closeSession();
  15. }

控制台打印的sql语句是:

  1. INFO: HHH000232: Schema update complete
  2. -------------------------
  3. Hibernate:
  4. select
  5. book0_.id as id1_1_0_,
  6. book0_.category_id as category2_1_0_,
  7. book0_.author as author3_1_0_,
  8. book0_.book_name as book_nam4_1_0_,
  9. book0_.price as price5_1_0_,
  10. book0_.pubDate as pubDate6_1_0_,
  11. category1_.id as id1_0_1_,
  12. category1_.name as name2_0_1_
  13. from
  14. t_book book0_
  15. left outer join
  16. Category category1_
  17. on book0_.category_id=category1_.id
  18. where
  19. book0_.id=?
  20. bookName = 读者
  21. ===========================
  22. categoryName = 文学

由上面可以看到,在需要book的信息时,sql语句使用 left outer join 把Category表中的信息也查询了出来。

下面从一的一端Category来查询

HibernateTest类中的testGetCategory()方法代码:

  1. @Test
  2. public void testGetCategory() {
  3. Session session = HibernateUtil.getSession();
  4. Transaction tx = session.beginTransaction();
  5. Category category = (Category) session.get(Category.class, 1);
  6. System.out.println("---------------------------------------------");
  7. System.out.println("category_name=" + category.getName());
  8. System.out.println("============================================");
  9. for (Iterator<Book> iter = category.getBooks().iterator(); iter
  10. .hasNext();) {
  11. System.out.println(iter.next().getName());
  12. }
  13. tx.commit();
  14. HibernateUtil.closeSession();
  15. }

控制台打印的sql语句如下:

  1. Hibernate:
  2. select
  3. category0_.id as id1_0_0_,
  4. category0_.name as name2_0_0_,
  5. books1_.category_id as category2_0_1_,
  6. books1_.id as id1_1_1_,
  7. books1_.id as id1_1_2_,
  8. books1_.category_id as category2_1_2_,
  9. books1_.author as author3_1_2_,
  10. books1_.book_name as book_nam4_1_2_,
  11. books1_.price as price5_1_2_,
  12. books1_.pubDate as pubDate6_1_2_
  13. from
  14. Category category0_
  15. left outer join
  16. t_book books1_
  17. on category0_.id=books1_.category_id
  18. where
  19. category0_.id=?
  20. ---------------------------------------------
  21. category_name=文学
  22. ============================================
  23. 读者
  24. 我的大学

从一的一端也是从使用left outer join 方法把t_book表的数据一起查询了出来

接下来我们把Category.hbm.xml和Book.hbm.xml中的lazy和fetch都去掉,执行testLoad()方法,代码如下:

  1. @Test
  2. public void testLoad() {
  3. Session session = HibernateUtil.getSession();
  4. Transaction tx = session.beginTransaction();
  5. List<Category> list = session.createCriteria(Category.class).list() ;
  6. System.out.println("---------------------------------");
  7. System.out.println("类型个数:"+list.size());
  8. for (Category category : list) {
  9. System.out.println("================================");
  10. System.out.println(category.getName()+"----数据本书:"+category.getBooks().size());
  11. }
  12. tx.commit();
  13. HibernateUtil.closeSession();
  14. }

执行后,打印的sql语句如下:

  1. Hibernate:
  2. select
  3. this_.id as id1_0_0_,
  4. this_.name as name2_0_0_
  5. from
  6. Category this_
  7. ---------------------------------
  8. 类型个数:5
  9. ================================
  10. Hibernate:
  11. select
  12. books0_.category_id as category2_0_0_,
  13. books0_.id as id1_1_0_,
  14. books0_.id as id1_1_1_,
  15. books0_.category_id as category2_1_1_,
  16. books0_.author as author3_1_1_,
  17. books0_.book_name as book_nam4_1_1_,
  18. books0_.price as price5_1_1_,
  19. books0_.pubDate as pubDate6_1_1_
  20. from
  21. t_book books0_
  22. where
  23. books0_.category_id=?
  24. 文学----数据本书:2
  25. ================================
  26. Hibernate:
  27. select
  28. books0_.category_id as category2_0_0_,
  29. books0_.id as id1_1_0_,
  30. books0_.id as id1_1_1_,
  31. books0_.category_id as category2_1_1_,
  32. books0_.author as author3_1_1_,
  33. books0_.book_name as book_nam4_1_1_,
  34. books0_.price as price5_1_1_,
  35. books0_.pubDate as pubDate6_1_1_
  36. from
  37. t_book books0_
  38. where
  39. books0_.category_id=?
  40. 历史----数据本书:2
  41. ================================
  42. Hibernate:
  43. select
  44. books0_.category_id as category2_0_0_,
  45. books0_.id as id1_1_0_,
  46. books0_.id as id1_1_1_,
  47. books0_.category_id as category2_1_1_,
  48. books0_.author as author3_1_1_,
  49. books0_.book_name as book_nam4_1_1_,
  50. books0_.price as price5_1_1_,
  51. books0_.pubDate as pubDate6_1_1_
  52. from
  53. t_book books0_
  54. where
  55. books0_.category_id=?
  56. 仙侠----数据本书:1
  57. ================================
  58. Hibernate:
  59. select
  60. books0_.category_id as category2_0_0_,
  61. books0_.id as id1_1_0_,
  62. books0_.id as id1_1_1_,
  63. books0_.category_id as category2_1_1_,
  64. books0_.author as author3_1_1_,
  65. books0_.book_name as book_nam4_1_1_,
  66. books0_.price as price5_1_1_,
  67. books0_.pubDate as pubDate6_1_1_
  68. from
  69. t_book books0_
  70. where
  71. books0_.category_id=?
  72. 科幻----数据本书:1
  73. ================================
  74. Hibernate:
  75. select
  76. books0_.category_id as category2_0_0_,
  77. books0_.id as id1_1_0_,
  78. books0_.id as id1_1_1_,
  79. books0_.category_id as category2_1_1_,
  80. books0_.author as author3_1_1_,
  81. books0_.book_name as book_nam4_1_1_,
  82. books0_.price as price5_1_1_,
  83. books0_.pubDate as pubDate6_1_1_
  84. from
  85. t_book books0_
  86. where
  87. books0_.category_id=?
  88. 恐怖----数据本书:0

子查询(Subselect fetching)

接下来我们在Category.hbm.xml代码中,加入fetch=“subselect” ,如图:

Center 3

继续testLoad()方法,打印的sql语句如下:

  1. Hibernate:
  2. select
  3. this_.id as id1_0_0_,
  4. this_.name as name2_0_0_
  5. from
  6. Category this_
  7. ---------------------------------
  8. 类型个数:5
  9. ================================
  10. Hibernate:
  11. select
  12. books0_.category_id as category2_0_1_,
  13. books0_.id as id1_1_1_,
  14. books0_.id as id1_1_0_,
  15. books0_.category_id as category2_1_0_,
  16. books0_.author as author3_1_0_,
  17. books0_.book_name as book_nam4_1_0_,
  18. books0_.price as price5_1_0_,
  19. books0_.pubDate as pubDate6_1_0_
  20. from
  21. t_book books0_
  22. where
  23. books0_.category_id in (
  24. select
  25. this_.id
  26. from
  27. Category this_
  28. )
  29. 文学----数据本书:2
  30. ================================
  31. 历史----数据本书:2
  32. ================================
  33. 仙侠----数据本书:1
  34. ================================
  35. 科幻----数据本书:1
  36. ================================
  37. 恐怖----数据本书:0

由sql语句可以看出,当我们需要查询t_book数据时,sql语句的where语句是:

  1. where
  2. books0_.category_id in (
  3. select
  4. this_.id
  5. from
  6. Category this_
  7. )

一次性将所有的分类Category数据全部查询出来。

假设现在我想查询Category的id是1,3,5的数据,那么方法testLoadWhere代码是:

  1. @Test
  2. public void testLoadWhere() {
  3. Session session = HibernateUtil.getSession();
  4. Transaction tx = session.beginTransaction();
  5. List<Category> list = session.createCriteria(Category.class)
  6. .add(Restrictions.in("id", new Integer[]{1,3,5}))
  7. .list() ;
  8. System.out.println("---------------------------------");
  9. System.out.println("类型个数:"+list.size());
  10. for (Category category : list) {
  11. System.out.println("================================");
  12. System.out.println(category.getName()+"----数据本书:"+category.getBooks().size());
  13. }
  14. tx.commit();
  15. HibernateUtil.closeSession();
  16. }

控制台打印的sql语句如下:

  1. Hibernate:
  2. select
  3. this_.id as id1_0_0_,
  4. this_.name as name2_0_0_
  5. from
  6. Category this_
  7. where
  8. this_.id in (
  9. ?, ?, ?
  10. )
  11. ---------------------------------
  12. 类型个数:3
  13. ================================
  14. Hibernate:
  15. select
  16. books0_.category_id as category2_0_1_,
  17. books0_.id as id1_1_1_,
  18. books0_.id as id1_1_0_,
  19. books0_.category_id as category2_1_0_,
  20. books0_.author as author3_1_0_,
  21. books0_.book_name as book_nam4_1_0_,
  22. books0_.price as price5_1_0_,
  23. books0_.pubDate as pubDate6_1_0_
  24. from
  25. t_book books0_
  26. where
  27. books0_.category_id in (
  28. select
  29. this_.id
  30. from
  31. Category this_
  32. where
  33. this_.id in (
  34. ?, ?, ?
  35. )
  36. )
  37. 文学----数据本书:2
  38. ================================
  39. 仙侠----数据本书:1
  40. ================================
  41. 恐怖----数据本书:0

批量抓取(Batch fetching

Book.hbm.xml代码修改为:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="com.myeclipse.pojo">
  6. <class name="Book" table="t_book" batch-size="3">
  7. <id name="id">
  8. <generator class="identity" />
  9. </id>
  10. <many-to-one name="category" class="Category" column="category_id"
  11. cascade="save-update" />
  12. <property name="author" />
  13. <property name="name" column="book_name" />
  14. <property name="price" />
  15. <property name="pubDate" />
  16. <!-- 使用过滤器 -->
  17. <filter name="bookFilter" condition="id=:id"></filter>
  18. </class>
  19. <!-- 过滤器定义 : 定义参数 -->
  20. <filter-def name="bookFilter">
  21. <filter-param name="id" type="integer" />
  22. </filter-def>
  23. </hibernate-mapping>

上面的代码中增加了一个batch-size属性,如图:

Center 4

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Hibernate抓取策略

    一、延迟载的概述           延迟加载:lazy(懒加载)。执行到该行代码的时候,不会发送语句去进行查询,在真正使用这个对象的属性的时候才会      发送SQL语句