浅谈Hibernate里的Fetch的作用

开发 后端
今天只是想先说一说Hibernate Fetch的作用.大家都知道,在Hibernate里为了性能考虑,引进了lazy的概念,这里我们以Parent和Child为模型来说明。

现在越来越发现其实掌握Hibernate Fetch 并不容易,Spring用起来其实简单多了,但是在用Hibernate的时候真的是需要一定的时间积累,对一个项目组来说如果采用Hibernate***有一个对Hibernate比较清楚的人否则碰到问题就会成为项目的风险。
我想告诉各位的是,掌握Hibernate Fetch可能比你预期的难多了,当你轻松的告诉我,Hibernate Fetch很简单的时候该是你自己多反省了. (只有一种情况例外,你是一个牛人)

好了,一个引子废话那么多,其实今天只是想先说一说Hibernate Fetch的作用.

大家都知道,在Hibernate里为了性能考虑,引进了lazy的概念,这里我们以Parent和Child为模型来说明

  1. public class Parent implements Serializable {  
  2.  
  3.     /** identifier field */  
  4.     private Long id;  
  5.  
  6.     /** persistent field */  
  7.     private List childs;  
  8.  
  9.     //skip all getter/setter method  
  10.  
  11.      
  12. }     
  13.  
  14.  
  15.  
  16. public class Child implements Serializable {  
  17.  
  18.     /** identifier field */  
  19.     private Long id;  
  20.  
  21.     /** persistent field */  
  22.     private net.foxlog.model.Parent parent;  
  23.  
  24.     //skip all getter/setter method  
  25.  

在我们查询Parent对象的时候,默认只有Parent的内容,并不包含childs的信息,如果在Parent.hbm.xml里设置lazy="false"的话才同时取出关联的所有childs内容.

问题是我既想要Hibernate默认的性能又想要临时的灵活性该怎么办?  这就是Fetch的功能。我们可以把fetch与lazy="true"的关系类比为事务当中的编程式事务与声明式事务,不太准确,但是大概是这个意思。

总值,fetch就是在代码这一层给你一个主动抓取得机会.

  1. Parent parent = (Parent)hibernateTemplate.execute(new HibernateCallback() {  
  2.             public Object doInHibernate(Session session) throws HibernateException, SQLException {  
  3.                 Query q = session.createQuery(  
  4.                         "from Parent as parent "+  
  5.                                 " left outer join fetch parent.childs " +  
  6.                                 " where parent.id = :id"  
  7.                 );  
  8.                 q.setParameter("id",new Long(15));  
  9.                 return (Parent)q.uniqueResult();  
  10.             }  
  11.  
  12.         });  
  13.  
  14.         Assert.assertTrue(parent.getChilds().size() > 0); 

你可以在lazy="true"的情况下把Fetch去掉,就会报异常. 当然,如果lazy="false"就不需要fetch了有一个问题,使用Fetch会有重复记录的现象发生,我们可以理解为Fetch实际上不是为Parent服务的,而是为Child服务的.所以直接取Parent会有不匹配的问题.

参考一下下面的这篇文章 Hibernate集合初始化

update:以上有些结论错误,实际上在Hibernate3.2.1版本下测试,可以不出现重复记录,

  1. public void testNPlusOne() throws Exception{  
  2.         List list = (List)hibernateTemplate.execute(new HibernateCallback() {  
  3.             public Object doInHibernate(Session session) throws HibernateException, SQLException {  
  4.                 Query q = session.createQuery(  
  5.                         "select distinct p from net.foxlog.model.Parent p inner join fetch p.childs"  
  6.                 );  
  7.                 return q.list();  
  8.             }  
  9.  
  10.         });  
  11.  
  12.         //((Parent)(list.get(0))).getChilds();  
  13.         System.out.println("list size = " + list.size());  
  14.         for(int i=0;i<list.size();i++){  
  15.             Parent p = (Parent)list.get(i);  
  16.             System.out.println("===parent = " + p);  
  17.             System.out.println("===parent's child's length = " + p.getChilds().size());  
  18.         }  
  19.  
  20.     } 

打印结果如下:

  1. Hibernate: select distinct parent0_.id as id2_0_, childs1_.id as id0_1_, childs1_.parent_id as parent2_0_1_, childs1_.parent_id as parent2_0__, childs1_.id as id0__ from parent parent0_ inner join child childs1_ on parent0_.id=childs1_.parent_id  
  2. list size = 3 
  3. ===parent = net.foxlog.model.Parent@1401d28[id=14]  
  4. ===parent's child's length = 1 
  5. ===parent = net.foxlog.model.Parent@14e0e90[id=15]  
  6. ===parent's child's length = 2 
  7. ===parent = net.foxlog.model.Parent@62610b[id=17]  
  8. ===parent's child's length = 3 

另外,如果用open session in view模式的话一般不用Fetch,但首先推荐Fetch,如果非用的话因为有N+1的现象,所以可以结合batch模式来改善下性能.

【编辑推荐】

  1. Hibernate入门学习宝典
  2. Hibernate属性简单分析
  3. Struts-Spring-Hibernate案例
  4. 简述Hibernate配置连接池
  5. 对Hibernate中get()与load()不同点分析
责任编辑:仲衡 来源: blogjava
相关推荐

2009-09-23 17:07:31

Hibernate C

2009-09-21 13:05:18

Hibernate u

2009-09-28 15:47:59

Hibernate O

2009-02-11 09:37:32

Hibernate分页技术JSP

2009-09-25 09:21:27

Hibernate元数

2009-09-29 10:46:58

Hibernate领域

2009-09-22 10:09:21

Hibernate S

2009-09-25 10:53:40

Hibernate S

2009-09-28 13:43:28

使用Hibernate

2009-03-06 14:34:31

StrutsHibernateSpring

2009-06-05 09:52:25

struts分页Hibernate

2009-09-24 12:50:23

Hibernate F

2010-08-31 17:12:24

DHCP作用域

2009-09-22 13:41:10

直接使用Hiberna

2009-09-28 13:39:01

Hibernate工作

2009-09-27 10:02:29

定制Hibernate

2009-09-29 16:16:58

Hibernate H

2010-07-08 14:47:37

动态路由协议

2010-07-16 12:15:39

反病毒软件病毒

2013-02-01 10:32:06

点赞
收藏

51CTO技术栈公众号