﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-followith-随笔分类-Hibernate</title><link>http://www.blogjava.net/followith/category/32393.html</link><description>既然我们不能改变周遭的世界，我们就只好改变自己，用慈悲心和智慧心来面对这一切。</description><language>zh-cn</language><lastBuildDate>Sat, 23 Aug 2008 00:53:16 GMT</lastBuildDate><pubDate>Sat, 23 Aug 2008 00:53:16 GMT</pubDate><ttl>60</ttl><item><title>Hibernate数据加载（Session.get和load的不同）</title><link>http://www.blogjava.net/followith/archive/2008/08/22/223693.html</link><dc:creator>followith</dc:creator><author>followith</author><pubDate>Fri, 22 Aug 2008 03:12:00 GMT</pubDate><guid>http://www.blogjava.net/followith/archive/2008/08/22/223693.html</guid><wfw:comment>http://www.blogjava.net/followith/comments/223693.html</wfw:comment><comments>http://www.blogjava.net/followith/archive/2008/08/22/223693.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/followith/comments/commentRss/223693.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/followith/services/trackbacks/223693.html</trackback:ping><description><![CDATA[Hibernate数据加载——Session.get/load
<br />
Session.load/get方法均可以根据指定的实体类和id从数据库读取记录，并返回与之对应的实体对象。其区别在于：
<br />
<br />
1. 如果未能发现符合条件的记录，get方法返回null，而load方法会抛出一个ObjectNotFoundException。
<br />
2. Load方法可返回实体的代理类实例，而get方法永远直接返回实体类。
<br />
3. load方法可以充分利用内部缓存和二级缓存中的现有数据，而get方法则仅仅在内部缓存中进行数据查找，如没有发现对应数据，将越过二级缓存，直接调用SQL完成数据读取。
<br />
Session在加载实体对象时，将经过的过程：
<br />
<br />
1.
首先，Hibernate中维持了两级缓存。第一级缓存由Session实例维护，其中保持了Session当前所有关联实体的数据，也称为内部缓存。而
第二级缓存则存在于SessionFactory层次，由当前所有由本SessionFactory构造的Session实例共享。出于性能考虑，避免无
谓的数据库访问，Session在调用数据库查询功能之前，会先在缓存中进行查询。首先在第一级缓存中，通过实体类型和id进行查找，如果第一级缓存查找
命中，且数据状态合法，则直接返回。 <br />
2.
之后，Session会在当前&#8220;NonExists&#8221;记录中进行查找，如果&#8220;NonExists&#8221;记录中存在同样的查询条件，则返回null。
&#8220;NonExists&#8221;记录了当前Session实例在之前所有查询操作中，未能查询到有效数据的查询条件（相当于一个查询黑名单列表）。如此一来，如果
Session中一个无效的查询条件重复出现，即可迅速作出判断，从而获得最佳的性能表现。 <br />
3. 对于load方法而言，如果内部缓存中未发现有效数据，则查询第二级缓存，如果第二级缓存命中，则返回。
<br />
4. 如在缓存中未发现有效数据，则发起数据库查询操作（Select SQL），如经过查询未发现对应记录，则将此次查询的信息在&#8220;NonExists&#8221;中加以记录，并返回null。
<br />
5. 根据映射配置和Select SQL得到的ResultSet，创建对应的数据对象。
<br />
6. 将其数据对象纳入当前Session实体管理容器（一级缓存）。
<br />
7. 执行Interceptor.onLoad方法（如果有对应的Interceptor）。
<br />
8. 将数据对象纳入二级缓存。
<br />
9. 如果数据对象实现了LifeCycle接口，则调用数据对象的onLoad方法。
<br />
10. 返回数据对象。
<img src ="http://www.blogjava.net/followith/aggbug/223693.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/followith/" target="_blank">followith</a> 2008-08-22 11:12 <a href="http://www.blogjava.net/followith/archive/2008/08/22/223693.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>