Hibernate的批量抓取

  批量抓取理解:如果我们需要查找到客户的所有联系人的话,按照正常的思路,一般是首先查询所有的客户,得到返回的客户的List集合。然后遍历List集合,得到集合中的每一个客户,在取出客户中的联系人(客户表和联系人表是一个一对多的关系,一个客户有多个联系人),对于这种情况,我们就可以使用Hibernate的批量抓取,因为批量抓取进行了优化,比上面的先得到客户,在查询客户的联系人的效率更加的高效。

  

原始方法实现:

 1 // 批量抓取的原始做法
 2     @Test
 3     public void fun2() {
 4         Transaction ts = null;
 5         Session session = null;
 6         SessionFactory factory = null;
 7         try {
 8             factory = Tools.getSessionFactory();
 9             session = factory.openSession();
10             ts = session.beginTransaction();
11             // 通过id找到对应的数据记录
12             Criteria criteria = session.createCriteria(Customer.class);
13             List<Customer> customers = criteria.list();
14             for(Customer customer : customers) {
15                 System.out.println(customer.getCid() + " -- " + customer.getCustName());
16                 Set<LinkMan> sets = customer.getMans();
17                 for (LinkMan set : sets) {
18                     System.out.println("	"+set.getLid()+"::"+set.getLinkName());
19                 }
20             }
21             ts.commit();
22         } catch (Exception e) {
23             ts.rollback();
24             e.printStackTrace();
25         } finally {
26             session.close();
27         }
28     }
View Code

运行截图:

  对于原始的方式进行查询,每次查询一个联系人均会向数据库发送一条查询语句,这样的话,效率就低了。

控制台输出:

九月 12, 2017 9:33:49 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Tue Sep 12 21:33:49 PDT 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Hibernate: 
    select
        this_.cid as cid1_0_0_,
        this_.custLevel as custLeve2_0_0_,
        this_.custName as custName3_0_0_,
        this_.custSource as custSour4_0_0_,
        this_.custMobile as custMobi5_0_0_ 
    from
        t_customer this_
4028db335e71d615015e71d61bb60000 -- Geore
Hibernate: 
    select
        mans0_.clid as clid5_1_0_,
        mans0_.lid as lid1_1_0_,
        mans0_.lid as lid1_1_1_,
        mans0_.linkName as linkName2_1_1_,
        mans0_.linkGender as linkGend3_1_1_,
        mans0_.linkPhone as linkPhon4_1_1_,
        mans0_.clid as clid5_1_1_ 
    from
        t_linkman mans0_ 
    where
        mans0_.clid=?
4028db335e71d615015e71d61bf30001::Mr.Li

4028db335e71d646015e71d64c8e0000
-- Alley Hibernate: select mans0_.clid as clid5_1_0_, mans0_.lid as lid1_1_0_, mans0_.lid as lid1_1_1_, mans0_.linkName as linkName2_1_1_, mans0_.linkGender as linkGend3_1_1_, mans0_.linkPhone as linkPhon4_1_1_, mans0_.clid as clid5_1_1_ from t_linkman mans0_ where mans0_.clid=?
4028db335e71d6ff015e71d705ad0001::Mr.Zhang 4028db335e71d753015e71d759930001::Mr.Xie 4028db335e71d646015e71d64ccc0001::Mr.Wang

4028db335e721e35015e721e3b030000
-- Mary Hibernate: select mans0_.clid as clid5_1_0_, mans0_.lid as lid1_1_0_, mans0_.lid as lid1_1_1_, mans0_.linkName as linkName2_1_1_, mans0_.linkGender as linkGend3_1_1_, mans0_.linkPhone as linkPhon4_1_1_, mans0_.clid as clid5_1_1_ from t_linkman mans0_ where mans0_.clid=? 4028db335e721e35015e721e3b410001::Mr.Zhao

Hibernate批量抓取实现:

  对于Hibernate的批量抓取的实现,其实在代码上并不需要进行改变,而只要修改配置文件的set标签,在set标签中添加属性batch-size,对于batch-size的值是一个整形的数据,整形的数据越大越好,越大发送的sql语句越少

配置代码:

1 <set name="mans" cascade="save-update,delete" fetch="select" lazy="extra" batch-size="20">

控制台输出:

 1 INFO: HHH000228: Running hbm2ddl schema update
 2 Tue Sep 12 21:38:34 PDT 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
 3 Hibernate: 
 4     select
 5         this_.cid as cid1_0_0_,
 6         this_.custLevel as custLeve2_0_0_,
 7         this_.custName as custName3_0_0_,
 8         this_.custSource as custSour4_0_0_,
 9         this_.custMobile as custMobi5_0_0_ 
10     from
11         t_customer this_
12 4028db335e71d615015e71d61bb60000 -- Geore
13 Hibernate: 
14     select
15         mans0_.clid as clid5_1_1_,
16         mans0_.lid as lid1_1_1_,
17         mans0_.lid as lid1_1_0_,
18         mans0_.linkName as linkName2_1_0_,
19         mans0_.linkGender as linkGend3_1_0_,
20         mans0_.linkPhone as linkPhon4_1_0_,
21         mans0_.clid as clid5_1_0_ 
22     from
23         t_linkman mans0_ 
24     where
25         mans0_.clid in (
26             ?, ?, ?
27         )
28     4028db335e71d615015e71d61bf30001::Mr.Li
29 4028db335e71d646015e71d64c8e0000 -- Alley
30     4028db335e71d753015e71d759930001::Mr.Xie
31     4028db335e71d646015e71d64ccc0001::Mr.Wang
32     4028db335e71d6ff015e71d705ad0001::Mr.Zhang
33 4028db335e721e35015e721e3b030000 -- Mary
34     4028db335e721e35015e721e3b410001::Mr.Zhao
原文地址:https://www.cnblogs.com/geore/p/7512364.html