Hibernate加载数据时get和load方法的区别

来源:互联网 发布:如何买域名 编辑:程序博客网 时间:2024/05/10 17:47

 Hibernate加载数据时get和load方法的区别,让我们先看一下方法原型吧:

1.get方法

 /**
  * Return the persistent instance of the given entity class with the given identifier,
  * or null if there is no such persistent instance. (If the instance, or a proxy for the
  * instance, is already associated with the session, return that instance or proxy.)
  *
  * @param clazz a persistent class
  * @param id an identifier
  * @return a persistent instance or null
  * @throws HibernateException
  */
 public Object get(Class clazz, Serializable id) throws HibernateException;

2.load方法


 /**
  * Return the persistent instance of the given entity class with the given identifier,
  * assuming that the instance exists.
  * <br><br>
  * You should not use this method to determine if an instance exists (use <tt>get()</tt>
  * instead). Use this only to retrieve an instance that you assume exists, where non-existence
  * would be an actual error.
  *
  * @param theClass a persistent class
  * @param id a valid identifier of an existing persistent instance of the class
  * @return the persistent instance or proxy
  * @throws HibernateException
  */

经过比较我们可以发现:Session.load/get方法均可以根据指定的实体类和id从数据库读取记录,并返回与之对应的实体对象。其区别在于:

  1. 如果未能发现符合条件的记录,get方法返回null,而load方法会抛出一个ObjectNotFoundException。
  2. Load方法可返回实体的代理类实例,而get方法永远直接返回实体类。
  3. load方法可以充分利用内部缓存和二级缓存中的现有数据,而get方法则仅仅在内部缓存中进行数据查找,如没有发现对应数据,将越过二级缓存,直接调用SQL完成数据读取。

Session在加载实体对象时,将经过的过程:

  1. 首先,Hibernate中维持了两级缓存。第一级缓存由Session实例维护,其中保持了Session当前所有关联实体的数据,也称为内部缓存。而第二级缓存则存在于SessionFactory层次,由当前所有由本SessionFactory构造的Session实例共享。出于性能考虑,避免无谓的数据库访问,Session在调用数据库查询功能之前,会先在缓存中进行查询。首先在第一级缓存中,通过实体类型和id进行查找,如果第一级缓存查找命中,且数据状态合法,则直接返回。
  2. 之后,Session会在当前“NonExists”记录中进行查找,如果“NonExists”记录中存在同样的查询条件,则返回null。“NonExists”记录了当前Session实例在之前所有查询操作中,未能查询到有效数据的查询条件(相当于一个查询黑名单列表)。如此一来,如果Session中一个无效的查询条件重复出现,即可迅速作出判断,从而获得最佳的性能表现。
  3. 对于load方法而言,如果内部缓存中未发现有效数据,则查询第二级缓存,如果第二级缓存命中,则返回。
  4. 如在缓存中未发现有效数据,则发起数据库查询操作(Select SQL),如经过查询未发现对应记录,则将此次查询的信息在“NonExists”中加以记录,并返回null。
  5. 根据映射配置和Select SQL得到的ResultSet,创建对应的数据对象。
  6. 将其数据对象纳入当前Session实体管理容器(一级缓存)。
  7. 执行Interceptor.onLoad方法(如果有对应的Interceptor)。
  8. 将数据对象纳入二级缓存。
  9. 如果数据对象实现了LifeCycle接口,则调用数据对象的onLoad方法。
  10. 返回数据对象。