﻿<?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-konhon-随笔分类-Hibernate</title><link>http://www.blogjava.net/konhon/category/3027.html</link><description>忘掉過去，展望未來。找回自我，超越自我。
&lt;br&gt;
&lt;b&gt;逃避不一定躲的过, 面对不一定最难过, 孤单不一定不快乐, 得到不一定能长久, 失去不一定不再拥有, 可能因为某个理由而伤心难过, 但我却能找个理由让自己快乐.&lt;/b&gt;&lt;br&gt;&lt;p&gt;
&lt;!-- Search Google --&gt;
&lt;left&gt;
&lt;form method="get" action="http://www.google.com/custom" target="google_window"&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td nowrap="nowrap" valign="top" align="left" height="32"&gt;
&lt;a href="http://www.google.com/"&gt;
&lt;img src="http://www.google.com/logos/Logo_25wht.gif" border="0" alt="Google" align="middle"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;input type="text" name="q" size="31" maxlength="255" value=""&gt;&lt;/input&gt;
&lt;input type="submit" name="sa" value="搜索"&gt;&lt;/input&gt;
&lt;input type="hidden" name="client" value="pub-5408663347953425"&gt;&lt;/input&gt;
&lt;input type="hidden" name="forid" value="1"&gt;&lt;/input&gt;
&lt;input type="hidden" name="ie" value="UTF-8"&gt;&lt;/input&gt;
&lt;input type="hidden" name="oe" value="UTF-8"&gt;&lt;/input&gt;
&lt;input type="hidden" name="cof" value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:1;"&gt;&lt;/input&gt;
&lt;input type="hidden" name="hl" value="zh-CN"&gt;&lt;/input&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/form&gt;
&lt;/left&gt;
&lt;!-- Search Google --&gt;
</description><language>zh-cn</language><lastBuildDate>Fri, 02 Mar 2007 06:57:09 GMT</lastBuildDate><pubDate>Fri, 02 Mar 2007 06:57:09 GMT</pubDate><ttl>60</ttl><item><title>应用Hibernate3的DetachedCriteria实现分页查询</title><link>http://www.blogjava.net/konhon/archive/2006/04/07/39756.html</link><dc:creator>konhon 优华</dc:creator><author>konhon 优华</author><pubDate>Fri, 07 Apr 2006 02:37:00 GMT</pubDate><guid>http://www.blogjava.net/konhon/archive/2006/04/07/39756.html</guid><wfw:comment>http://www.blogjava.net/konhon/comments/39756.html</wfw:comment><comments>http://www.blogjava.net/konhon/archive/2006/04/07/39756.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/konhon/comments/commentRss/39756.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/konhon/services/trackbacks/39756.html</trackback:ping><description><![CDATA[<span class="postbody">Hibernate3提供了DetachedCriteria，使得我们可以在Web层构造detachedCriteria，然后调用业务层Bean，进行动态条件查询，根据这一功能，我设计了通用的抽象Bean基类和分页类支持，代码来自于Quake Wang的javaeye-core包的相应类，然后又做了很多修改。 <br /><br />分页支持类： <br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">package</span><span style="COLOR: #000000"> com.javaeye.common.util; <br /><br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> java.util.List; <br /><br /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> PaginationSupport { <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> PAGESIZE </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">30</span><span style="COLOR: #000000">; <br /><br />        </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> pageSize </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> PAGESIZE; <br /><br />        </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> List items; <br /><br />        </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> totalCount; <br /><br />        </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[] indexes </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]; <br /><br />        </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> startIndex </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> PaginationSupport(List items, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> totalCount) { <br />                setPageSize(PAGESIZE); <br />                setTotalCount(totalCount); <br />                setItems(items);                <br />                setStartIndex(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> PaginationSupport(List items, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> totalCount, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> startIndex) { <br />                setPageSize(PAGESIZE); <br />                setTotalCount(totalCount); <br />                setItems(items);                <br />                setStartIndex(startIndex); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> PaginationSupport(List items, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> totalCount, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> pageSize, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> startIndex) { <br />                setPageSize(pageSize); <br />                setTotalCount(totalCount); <br />                setItems(items); <br />                setStartIndex(startIndex); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> List getItems() { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> items; <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> setItems(List items) { <br />                </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.items </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> items; <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> getPageSize() { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> pageSize; <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> setPageSize(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> pageSize) { <br />                </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.pageSize </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> pageSize; <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> getTotalCount() { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> totalCount; <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> setTotalCount(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> totalCount) { <br />                </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (totalCount </span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">) { <br />                        </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.totalCount </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> totalCount; <br />                        </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> count </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> totalCount </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000"> pageSize; <br />                        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (totalCount </span><span style="COLOR: #000000">%</span><span style="COLOR: #000000"> pageSize </span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">) <br />                                count</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">; <br />                        indexes </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[count]; <br />                        </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; i </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000"> count; i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">) { <br />                                indexes[i] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> pageSize </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> i; <br />                        } <br />                } </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> { <br />                        </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.totalCount </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br />                } <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[] getIndexes() { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> indexes; <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> setIndexes(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[] indexes) { <br />                </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.indexes </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> indexes; <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> getStartIndex() { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> startIndex; <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> setStartIndex(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> startIndex) { <br />                </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (totalCount </span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">) <br />                        </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.startIndex </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br />                </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (startIndex </span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000"> totalCount) <br />                        </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.startIndex </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> indexes[indexes.length </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]; <br />                </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (startIndex </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">) <br />                        </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.startIndex </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br />                </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> { <br />                        </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.startIndex </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> indexes[startIndex </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000"> pageSize]; <br />                } <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> getNextIndex() { <br />                </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> nextIndex </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> getStartIndex() </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> pageSize; <br />                </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (nextIndex </span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000"> totalCount) <br />                        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> getStartIndex(); <br />                </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> <br />                        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> nextIndex; <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> getPreviousIndex() { <br />                </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> previousIndex </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> getStartIndex() </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> pageSize; <br />                </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (previousIndex </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">) <br />                        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br />                </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> <br />                        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> previousIndex; <br />        } <br /><br />}</span></div></span>
		<br />抽象业务类 <br /><table cellspacing="1" cellpadding="3" width="90%" align="center" border="0"><tbody><tr><td><span class="genmed"><b>java代码: </b></span></td></tr></tbody></table><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"> <br />* Created on 2005-7-12 <br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br /></span><span style="COLOR: #0000ff">package</span><span style="COLOR: #000000"> com.javaeye.common.business; <br /><br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> java.io.Serializable; <br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> java.util.List; <br /><br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.hibernate.Criteria; <br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.hibernate.HibernateException; <br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.hibernate.Session; <br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.hibernate.criterion.DetachedCriteria; <br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.hibernate.criterion.Projections; <br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.springframework.orm.hibernate3.HibernateCallback; <br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.springframework.orm.hibernate3.support.HibernateDaoSupport; <br /><br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> com.javaeye.common.util.PaginationSupport; <br /><br /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">abstract</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> AbstractManager </span><span style="COLOR: #0000ff">extends</span><span style="COLOR: #000000"> HibernateDaoSupport { <br /><br />        </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">boolean</span><span style="COLOR: #000000"> cacheQueries </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">; <br /><br />        </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> String queryCacheRegion; <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> setCacheQueries(</span><span style="COLOR: #0000ff">boolean</span><span style="COLOR: #000000"> cacheQueries) { <br />                </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.cacheQueries </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> cacheQueries; <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> setQueryCacheRegion(String queryCacheRegion) { <br />                </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.queryCacheRegion </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> queryCacheRegion; <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> save(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> Object entity) { <br />                getHibernateTemplate().save(entity); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> persist(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> Object entity) { <br />                getHibernateTemplate().save(entity); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> update(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> Object entity) { <br />                getHibernateTemplate().update(entity); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> delete(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> Object entity) { <br />                getHibernateTemplate().delete(entity); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> Object load(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> Class entity, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> Serializable id) { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> getHibernateTemplate().load(entity, id); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> Object get(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> Class entity, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> Serializable id) { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> getHibernateTemplate().get(entity, id); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> List findAll(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> Class entity) { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> getHibernateTemplate().find(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">from </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> entity.getName()); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> List findByNamedQuery(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> String namedQuery) { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> getHibernateTemplate().findByNamedQuery(namedQuery); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> List findByNamedQuery(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> String query, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> Object parameter) { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> getHibernateTemplate().findByNamedQuery(query, parameter); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> List findByNamedQuery(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> String query, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> Object[] parameters) { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> getHibernateTemplate().findByNamedQuery(query, parameters); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> List find(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> String query) { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> getHibernateTemplate().find(query); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> List find(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> String query, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> Object parameter) { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> getHibernateTemplate().find(query, parameter); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> PaginationSupport findPageByCriteria(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> DetachedCriteria detachedCriteria) { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> PaginationSupport findPageByCriteria(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> DetachedCriteria detachedCriteria, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> startIndex) { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> PaginationSupport findPageByCriteria(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> DetachedCriteria detachedCriteria, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> pageSize, <br />                        </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> startIndex) { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> (PaginationSupport) getHibernateTemplate().execute(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> HibernateCallback() { <br />                        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> Object doInHibernate(Session session) </span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> HibernateException { <br />                                Criteria criteria </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> detachedCriteria.getExecutableCriteria(session); <br />                                </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> totalCount </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue(); <br />                                criteria.setProjection(</span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">); <br />                                List items </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> criteria.setFirstResult(startIndex).setMaxResults(pageSize).list(); <br />                                PaginationSupport ps </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> PaginationSupport(items, totalCount, pageSize, startIndex); <br />                                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> ps; <br />                        } <br />                }, </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> List findAllByCriteria(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> DetachedCriteria detachedCriteria) { <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> (List) getHibernateTemplate().execute(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> HibernateCallback() { <br />                        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> Object doInHibernate(Session session) </span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> HibernateException { <br />                                Criteria criteria </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> detachedCriteria.getExecutableCriteria(session); <br />                                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> criteria.list(); <br />                        } <br />                }, </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">); <br />        } <br /><br />        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> getCountByCriteria(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> DetachedCriteria detachedCriteria) { <br />                Integer count </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> (Integer) getHibernateTemplate().execute(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> HibernateCallback() { <br />                        </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> Object doInHibernate(Session session) </span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> HibernateException { <br />                                Criteria criteria </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> detachedCriteria.getExecutableCriteria(session); <br />                                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> criteria.setProjection(Projections.rowCount()).uniqueResult(); <br />                        } <br />                }, </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">); <br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> count.intValue(); <br />        } <br />} <br /></span></div>用户在web层构造查询条件detachedCriteria，和可选的startIndex，调用业务bean的相应findByCriteria方法，返回一个PaginationSupport的实例ps。 <br /><br />ps.getItems()得到已分页好的结果集 <br />ps.getIndexes()得到分页索引的数组 <br />ps.getTotalCount()得到总结果数 <br />ps.getStartIndex()当前分页索引 <br />ps.getNextIndex()下一页索引 <br />ps.getPreviousIndex()上一页索引
<p> </p><p> </p><img src ="http://www.blogjava.net/konhon/aggbug/39756.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/konhon/" target="_blank">konhon 优华</a> 2006-04-07 10:37 <a href="http://www.blogjava.net/konhon/archive/2006/04/07/39756.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate配置文件模板</title><link>http://www.blogjava.net/konhon/archive/2006/04/06/39538.html</link><dc:creator>konhon 优华</dc:creator><author>konhon 优华</author><pubDate>Thu, 06 Apr 2006 02:45:00 GMT</pubDate><guid>http://www.blogjava.net/konhon/archive/2006/04/06/39538.html</guid><wfw:comment>http://www.blogjava.net/konhon/comments/39538.html</wfw:comment><comments>http://www.blogjava.net/konhon/archive/2006/04/06/39538.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/konhon/comments/commentRss/39538.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/konhon/services/trackbacks/39538.html</trackback:ping><description><![CDATA[<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">&lt;?</span>
				<span style="COLOR: #ff00ff">xml version="1.0" encoding="UTF-8"</span>
				<span style="COLOR: #0000ff">?&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<br />
				</span>
				<span style="COLOR: #0000ff">&lt;!</span>
				<span style="COLOR: #ff00ff">DOCTYPE hibernate-mapping PUBLIC<br /> "-//Hibernate/Hibernate Mapping DTD 3.0//EN"<br /> "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"<br /></span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<br />
				</span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">hibernate-mapping</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />  </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">class </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="Tuser"</span>
				<span style="COLOR: #ff0000"> table</span>
				<span style="COLOR: #0000ff">="T_User"</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">id </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="id"</span>
				<span style="COLOR: #ff0000"> column</span>
				<span style="COLOR: #0000ff">="id"</span>
				<span style="COLOR: #ff0000"> type</span>
				<span style="COLOR: #0000ff">="java.lang.Integer"</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />      </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">generator </span>
				<span style="COLOR: #ff0000">class</span>
				<span style="COLOR: #0000ff">="native"</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">&lt;/</span>
				<span style="COLOR: #800000">id</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="name"</span>
				<span style="COLOR: #ff0000"> column</span>
				<span style="COLOR: #0000ff">="name"</span>
				<span style="COLOR: #ff0000"> type</span>
				<span style="COLOR: #0000ff">="java.lang.String"</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #008000">&lt;!--</span>
				<span style="COLOR: #008000">&lt;property name="age" column="age" type="java.lang.Integer"/&gt;</span>
				<span style="COLOR: #008000">--&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #008000">&lt;!--</span>
				<span style="COLOR: #008000">&lt;property name="email" column="email" type="EMailList"/&gt;</span>
				<span style="COLOR: #008000">--&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #008000">&lt;!--</span>
				<span style="COLOR: #008000">&lt;property name="image" column="image" type="java.sql.Blob"/&gt;</span>
				<span style="COLOR: #008000">--&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="resume"</span>
				<span style="COLOR: #ff0000"> column</span>
				<span style="COLOR: #0000ff">="resume"</span>
				<span style="COLOR: #ff0000"> type</span>
				<span style="COLOR: #0000ff">="clob"</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />  </span>
				<span style="COLOR: #0000ff">&lt;/</span>
				<span style="COLOR: #800000">class</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<span style="COLOR: #0000ff">&lt;/</span>
				<span style="COLOR: #800000">hibernate-mapping</span>
				<span style="COLOR: #0000ff">&gt;</span>
		</div>
		<br />復合主鍵<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">&lt;?</span><span style="COLOR: #ff00ff">xml version="1.0" encoding="UTF-8"</span><span style="COLOR: #0000ff">?&gt;</span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #0000ff">&lt;!</span><span style="COLOR: #ff00ff">DOCTYPE hibernate-mapping PUBLIC<br /> "-//Hibernate/Hibernate Mapping DTD 3.0//EN"<br /> "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"<br /></span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">hibernate-mapping</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />  </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">class </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="TUser2"</span><span style="COLOR: #ff0000"> table</span><span style="COLOR: #0000ff">="T_User2"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">composite-id </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="userPk"</span><span style="COLOR: #ff0000"> class</span><span style="COLOR: #0000ff">="TUserPk"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />      </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">key-property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="firstName"</span><span style="COLOR: #ff0000"> column</span><span style="COLOR: #0000ff">="firstname"</span><span style="COLOR: #ff0000"> type</span><span style="COLOR: #0000ff">="java.lang.String"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br />      </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">key-property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="lastName"</span><span style="COLOR: #ff0000"> column</span><span style="COLOR: #0000ff">="lastname"</span><span style="COLOR: #ff0000"> type</span><span style="COLOR: #0000ff">="java.lang.String"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">composite-id</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="age"</span><span style="COLOR: #ff0000"> column</span><span style="COLOR: #0000ff">="age"</span><span style="COLOR: #ff0000"> type</span><span style="COLOR: #0000ff">="java.lang.Integer"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000">    <br />  </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">class</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">hibernate-mapping</span><span style="COLOR: #0000ff">&gt;</span></div><br />DISCRIMINATOR<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">&lt;?</span><span style="COLOR: #ff00ff">xml version="1.0" encoding="UTF-8"</span><span style="COLOR: #0000ff">?&gt;</span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #0000ff">&lt;!</span><span style="COLOR: #ff00ff">DOCTYPE hibernate-mapping PUBLIC<br /> "-//Hibernate/Hibernate Mapping DTD 3.0//EN"<br /> "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"<br /></span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">hibernate-mapping</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />  </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">class </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="TItem"</span><span style="COLOR: #ff0000"> table</span><span style="COLOR: #0000ff">="T_Item"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">id </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="id"</span><span style="COLOR: #ff0000"> column</span><span style="COLOR: #0000ff">="id"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />      </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">generator </span><span style="COLOR: #ff0000">class</span><span style="COLOR: #0000ff">="native"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">id</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">discriminator </span><span style="COLOR: #ff0000">column</span><span style="COLOR: #0000ff">="category"</span><span style="COLOR: #ff0000"> type</span><span style="COLOR: #0000ff">="java.lang.String"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="manufacturer"</span><span style="COLOR: #ff0000"> column</span><span style="COLOR: #0000ff">="manufacturer"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="name"</span><span style="COLOR: #ff0000"> column</span><span style="COLOR: #0000ff">="name"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000">    <br />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">subclass </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="TBook"</span><span style="COLOR: #ff0000"> discriminator-value</span><span style="COLOR: #0000ff">="1"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />      </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="pageCount"</span><span style="COLOR: #ff0000"> column</span><span style="COLOR: #0000ff">="pagecount"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">subclass</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">subclass </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="TDVD"</span><span style="COLOR: #ff0000"> discriminator-value</span><span style="COLOR: #0000ff">="2"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />      </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="regionCode"</span><span style="COLOR: #ff0000"> column</span><span style="COLOR: #0000ff">="regionCode"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">subclass</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />  </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">class</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">hibernate-mapping</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span></div><img src ="http://www.blogjava.net/konhon/aggbug/39538.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/konhon/" target="_blank">konhon 优华</a> 2006-04-06 10:45 <a href="http://www.blogjava.net/konhon/archive/2006/04/06/39538.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>標准的Hibernate.cfg.xml文件 SQL Server</title><link>http://www.blogjava.net/konhon/archive/2006/04/06/39536.html</link><dc:creator>konhon 优华</dc:creator><author>konhon 优华</author><pubDate>Thu, 06 Apr 2006 02:42:00 GMT</pubDate><guid>http://www.blogjava.net/konhon/archive/2006/04/06/39536.html</guid><wfw:comment>http://www.blogjava.net/konhon/comments/39536.html</wfw:comment><comments>http://www.blogjava.net/konhon/archive/2006/04/06/39536.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/konhon/comments/commentRss/39536.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/konhon/services/trackbacks/39536.html</trackback:ping><description><![CDATA[<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">&lt;!</span>
				<span style="COLOR: #ff00ff">DOCTYPE hibernate-configuration PUBLIC<br />    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"<br />    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<br />
				</span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">hibernate-configuration</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />  </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">session-factory</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="hibernate.connection.url"</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />       jdbc:Microsoft:sqlserver://192.168.10.236:1433;databaseName=Sample<br />    </span>
				<span style="COLOR: #0000ff">&lt;/</span>
				<span style="COLOR: #800000">property</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="hibernate.connection.driver_class"</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />       com.microsoft.jdbc.sqlserver.SQLServerDriver<br />    </span>
				<span style="COLOR: #0000ff">&lt;/</span>
				<span style="COLOR: #800000">property</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="hibernate.connection.username"</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />       sa<br />    </span>
				<span style="COLOR: #0000ff">&lt;/</span>
				<span style="COLOR: #800000">property</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="hibernate.connection.password"</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />       55<br />    </span>
				<span style="COLOR: #0000ff">&lt;/</span>
				<span style="COLOR: #800000">property</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="dialect"</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />       org.hibernate.dialect.SQLServerDialect<br />    </span>
				<span style="COLOR: #0000ff">&lt;/</span>
				<span style="COLOR: #800000">property</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="hibernate.show_sql"</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />       true<br />    </span>
				<span style="COLOR: #0000ff">&lt;/</span>
				<span style="COLOR: #800000">property</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="hibernate.transaction.factory_class"</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />       org.hibernate.transaction.JDBCTransactionFactory<br />    </span>
				<span style="COLOR: #0000ff">&lt;/</span>
				<span style="COLOR: #800000">property</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">mapping </span>
				<span style="COLOR: #ff0000">resource</span>
				<span style="COLOR: #0000ff">="TItem.hbm.xml"</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">    <br />  </span>
				<span style="COLOR: #0000ff">&lt;/</span>
				<span style="COLOR: #800000">session-factory</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<span style="COLOR: #0000ff">&lt;/</span>
				<span style="COLOR: #800000">hibernate-configuration</span>
				<span style="COLOR: #0000ff">&gt;</span>
				<span style="COLOR: #000000">
						<br />
				</span>
		</div><img src ="http://www.blogjava.net/konhon/aggbug/39536.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/konhon/" target="_blank">konhon 优华</a> 2006-04-06 10:42 <a href="http://www.blogjava.net/konhon/archive/2006/04/06/39536.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate 的CRUD</title><link>http://www.blogjava.net/konhon/archive/2006/04/05/39306.html</link><dc:creator>konhon 优华</dc:creator><author>konhon 优华</author><pubDate>Wed, 05 Apr 2006 01:37:00 GMT</pubDate><guid>http://www.blogjava.net/konhon/archive/2006/04/05/39306.html</guid><wfw:comment>http://www.blogjava.net/konhon/comments/39306.html</wfw:comment><comments>http://www.blogjava.net/konhon/archive/2006/04/05/39306.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/konhon/comments/commentRss/39306.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/konhon/services/trackbacks/39306.html</trackback:ping><description><![CDATA[<p>
				<strong>Save<br /></strong>// 新增名為"Emma"的用戶記錄<br />Tuser user = new Tuser();<br />user.setName("Emma");<br />session.save(user);<br /><br /><strong>Get</strong><br />// 假設T_User表中存在ID = 1的記錄<br />Tuser user = (Tuser)session.get(Tuser.class, new Integer(1));<br /><br /><strong>Delete<br /></strong>// 假設T_User表中存在ID = 1的記錄<br />Tuser user = (Tuser)session.get(Tuser.class, new Integer(1));<br />session.delete(user);<br /><br />// 也可以通過HQL指定刪除條件<br />Session.delete(" from Tuser where id = 1");<br /><br />// 通過Query接口進於基於HQL的刪除操作<br />Stirng hql = "delete Tuser where id = 1";<br />Query query = session.createQuery(hql);<br />query.executeUpdate();<br /><br /><strong>Find<br /></strong>// 通過Query接口進行數據查詢<br />String hql="from Tuser user where user.name like ?";<br />Query query = session.createQuery(hql);<br />query.setParameter(0, "Cartier");<br /><br />List list = query.list();<br /><br />Iterator it = list.iterator();<br />while(it.hasNext()){<br />  Tuser user = (Tuser)it.next();<br />  System.out.println(user.getName());<br />}<br /><br />// 通過Criteria接口進行數據查詢<br />Criteria criteria = session.createCriteria(Tuser.class);<br />criteria.add(Expression.eq("name","Cariter"));<br /><br />List list = criteria.list();<br /><br />Iterator it = list.iterator();<br />while(it.hasNext()){<br />  Tuser user = (Tuser)it.next();<br />  System.out.println(user.getName());<br />}</p><img src ="http://www.blogjava.net/konhon/aggbug/39306.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/konhon/" target="_blank">konhon 优华</a> 2006-04-05 09:37 <a href="http://www.blogjava.net/konhon/archive/2006/04/05/39306.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate  通過數據產生基礎代碼之數據連接配置文件</title><link>http://www.blogjava.net/konhon/archive/2006/04/04/39177.html</link><dc:creator>konhon 优华</dc:creator><author>konhon 优华</author><pubDate>Tue, 04 Apr 2006 07:42:00 GMT</pubDate><guid>http://www.blogjava.net/konhon/archive/2006/04/04/39177.html</guid><wfw:comment>http://www.blogjava.net/konhon/comments/39177.html</wfw:comment><comments>http://www.blogjava.net/konhon/archive/2006/04/04/39177.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/konhon/comments/commentRss/39177.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/konhon/services/trackbacks/39177.html</trackback:ping><description><![CDATA[<p>使用MiddleGen for Hibernate工具.<br /><br />配置./config/database下的mssql.xml文件<br /></p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #000000">    </span>
				<span style="COLOR: #008000">&lt;!--</span>
				<span style="COLOR: #008000"> =================================================================== </span>
				<span style="COLOR: #008000">--&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #008000">&lt;!--</span>
				<span style="COLOR: #008000"> ant properties/targets for MS SQL Server                            </span>
				<span style="COLOR: #008000">--&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #008000">&lt;!--</span>
				<span style="COLOR: #008000"> note: this is not a proper xml file (there is no root element)      </span>
				<span style="COLOR: #008000">--&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #008000">&lt;!--</span>
				<span style="COLOR: #008000">       it is intended to be imported from a *real* xml file          </span>
				<span style="COLOR: #008000">--&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #008000">&lt;!--</span>
				<span style="COLOR: #008000"> =================================================================== </span>
				<span style="COLOR: #008000">--&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="database.script.file"</span>
				<span style="COLOR: #ff0000">           value</span>
				<span style="COLOR: #0000ff">=""</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="database.driver.file"</span>
				<span style="COLOR: #ff0000">           value</span>
				<span style="COLOR: #0000ff">="${lib.dir}/mssqlserver.jar"</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="database.driver"</span>
				<span style="COLOR: #ff0000">                value</span>
				<span style="COLOR: #0000ff">="com.microsoft.jdbc.sqlserver.SQLServerDriver"</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="database.url"</span>
				<span style="COLOR: #ff0000">                   value</span>
				<span style="COLOR: #0000ff">="jdbc:Microsoft:sqlserver://192.168.10.236:1433;databaseName=Sample"</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="database.userid"</span>
				<span style="COLOR: #ff0000">                value</span>
				<span style="COLOR: #0000ff">="sa"</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="database.password"</span>
				<span style="COLOR: #ff0000">              value</span>
				<span style="COLOR: #0000ff">="kidsland%55"</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="database.schema"</span>
				<span style="COLOR: #ff0000">                value</span>
				<span style="COLOR: #0000ff">="dbo"</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="database.catalog"</span>
				<span style="COLOR: #ff0000">               value</span>
				<span style="COLOR: #0000ff">="Sample"</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property </span>
				<span style="COLOR: #ff0000">name</span>
				<span style="COLOR: #0000ff">="jboss.datasource.mapping"</span>
				<span style="COLOR: #ff0000">       value</span>
				<span style="COLOR: #0000ff">="MS SQLSERVER"</span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span>
				<span style="COLOR: #008000">&lt;!--</span>
				<span style="COLOR: #008000">property name="jboss.datasource.mapping"       value="MS SQLSERVER2000"/</span>
				<span style="COLOR: #008000">--&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
		</div>
		<br />注意: <font color="#ff1493">database.schema 與 database.catalog 不能為空.<br /><br /></font><img src ="http://www.blogjava.net/konhon/aggbug/39177.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/konhon/" target="_blank">konhon 优华</a> 2006-04-04 15:42 <a href="http://www.blogjava.net/konhon/archive/2006/04/04/39177.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate : Query.list()、Query.iterator()的區別</title><link>http://www.blogjava.net/konhon/archive/2005/08/12/9909.html</link><dc:creator>konhon 优华</dc:creator><author>konhon 优华</author><pubDate>Fri, 12 Aug 2005 03:25:00 GMT</pubDate><guid>http://www.blogjava.net/konhon/archive/2005/08/12/9909.html</guid><wfw:comment>http://www.blogjava.net/konhon/comments/9909.html</wfw:comment><comments>http://www.blogjava.net/konhon/archive/2005/08/12/9909.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/konhon/comments/commentRss/9909.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/konhon/services/trackbacks/9909.html</trackback:ping><description><![CDATA[<H1><A href="file:///E:/Java/HibernateGossip/HibernateGossip/HibernateGossip.html"></A>&nbsp;</H1>Query上有list()與iterator()方法，兩者的差別在於list()方法在讀取資料時，並不會利用到快取，而是直接再向資料庫查詢，而iterator()則將讀取到的資料寫到快取，並於讀取時再次利用。<BR><BR>來看看下面的程式：<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN style="FONT-WEIGHT: bold">Session session = sessionFactory.openSession();</SPAN><BR><SPAN style="FONT-WEIGHT: bold">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><BR><SPAN style="FONT-WEIGHT: bold">Query query = session.createQuery("from User");</SPAN><BR><SPAN style="FONT-WEIGHT: bold">List users = query.list();</SPAN><BR><SPAN style="FONT-WEIGHT: bold">users = query.list();</SPAN><BR><SPAN style="FONT-WEIGHT: bold"></SPAN><BR><SPAN style="FONT-WEIGHT: bold">session.close();</SPAN><BR><SPAN style="FONT-WEIGHT: bold"></SPAN></DIV><SPAN style="FONT-WEIGHT: bold"><BR></SPAN>這個程式片段會使用兩次SQL來查詢資料庫：<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN style="FONT-WEIGHT: bold">Hibernate: select user0_.id as id, user0_.name as name0_, user0_.age as age0_ from user user0_</SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">Hibernate: select user0_.id as id, user0_.name as name0_, user0_.age as age0_ from user user0_</SPAN><BR></DIV><SPAN style="FONT-WEIGHT: bold"><BR></SPAN>如果在Session關閉之前，要再將所有資料在取出，可以使用iterator()方法，例如：<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN style="FONT-WEIGHT: bold">Session session = sessionFactory.openSession();</SPAN><BR><SPAN style="FONT-WEIGHT: bold"></SPAN><BR><SPAN style="FONT-WEIGHT: bold">Query query = session.createQuery("from User");</SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">Iterator users = query.iterate();</SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">users = query.iterate();</SPAN><BR style="FONT-WEIGHT: bold"><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">session.close();</SPAN><BR></DIV><BR>這個程式片段會使用一次SQL向資料庫查詢，第二次則直接從快取中取得資料：<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN style="FONT-WEIGHT: bold">Hibernate: select user0_.id as col_0_0_ from user user0_</SPAN><BR></DIV><BR>由於使用iterator()方法時會使用到Session level快取，所以在查詢大量資料時，會耗用大量的記憶體，必要時可以使用Session的evict()或clear()方法來清除快取。<BR><img src ="http://www.blogjava.net/konhon/aggbug/9909.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/konhon/" target="_blank">konhon 优华</a> 2005-08-12 11:25 <a href="http://www.blogjava.net/konhon/archive/2005/08/12/9909.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate之SessionFactory与Session</title><link>http://www.blogjava.net/konhon/archive/2005/08/11/9767.html</link><dc:creator>konhon 优华</dc:creator><author>konhon 优华</author><pubDate>Thu, 11 Aug 2005 01:14:00 GMT</pubDate><guid>http://www.blogjava.net/konhon/archive/2005/08/11/9767.html</guid><wfw:comment>http://www.blogjava.net/konhon/comments/9767.html</wfw:comment><comments>http://www.blogjava.net/konhon/archive/2005/08/11/9767.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/konhon/comments/commentRss/9767.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/konhon/services/trackbacks/9767.html</trackback:ping><description><![CDATA[Session是由SessionFactory所創建，SessionFactory是執行緒安全的（Thread-safe），您可以讓多個執行緒同時存取SessionFactory而不會有資料共用的問題，然而Session則不是設計為執行緒安全的，所以試圖讓多個執行緒共用一個 Session，將會产生因資料共用而發生混亂的問題。 <BR><BR>在Hibernate參考手冊中的 <A href="http://www.hibernate.org/hib_docs/v3/reference/en/html/quickstart.html"><FONT color=#000000>Quickstart with Tomcat</FONT></A> 中，示範了一個HibernateUtil，它使用了ThreadLocal類別來建立一個Session管理的輔助類，這是Hibernate的Session管理一個廣為應用的解決方案，ThreadLocal是 <A href="http://caterpillar.onlyfun.net/Gossip/DesignPattern/ThreadSpecificStorage.htm"><FONT color=#000000>Thread-Specific Storage 模式</FONT></A> 的一個運作實例。<BR><BR>由於Thread-Specific Stroage模式可以有效隔離執行緒所使用的資料，所以避開Session的多執行緒之間的資料共用問題，以下列出Hibernate參考手冊中的HibernateUtil類：<BR><BR>HibernateUtil.java <BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">import&nbsp;org.apache.commons.logging.Log;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>import&nbsp;org.apache.commons.logging.LogFactory;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>import&nbsp;org.hibernate.Session;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>import&nbsp;org.hibernate.SessionFactory;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>import&nbsp;org.hibernate.cfg.Configuration;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_193_358_Open_Image onclick="this.style.display='none'; Codehighlighter1_193_358_Open_Text.style.display='none'; Codehighlighter1_193_358_Closed_Image.style.display='inline'; Codehighlighter1_193_358_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_193_358_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_193_358_Closed_Text.style.display='none'; Codehighlighter1_193_358_Open_Image.style.display='inline'; Codehighlighter1_193_358_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_193_358_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_193_358_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;@author&nbsp;Administrator<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;TODO&nbsp;To&nbsp;change&nbsp;the&nbsp;template&nbsp;for&nbsp;this&nbsp;generated&nbsp;type&nbsp;comment&nbsp;go&nbsp;to&nbsp;Window&nbsp;-<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;Preferences&nbsp;-&nbsp;Java&nbsp;-&nbsp;Code&nbsp;Style&nbsp;-&nbsp;Code&nbsp;Templates<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_387_1276_Open_Image onclick="this.style.display='none'; Codehighlighter1_387_1276_Open_Text.style.display='none'; Codehighlighter1_387_1276_Closed_Image.style.display='inline'; Codehighlighter1_387_1276_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_387_1276_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_387_1276_Closed_Text.style.display='none'; Codehighlighter1_387_1276_Open_Image.style.display='inline'; Codehighlighter1_387_1276_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;HibernateUtil&nbsp;</SPAN><SPAN id=Codehighlighter1_387_1276_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_387_1276_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;Log&nbsp;log&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;LogFactory.getLog(HibernateUtil.</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;final&nbsp;SessionFactory&nbsp;sessionFactory;<BR><IMG id=Codehighlighter1_526_819_Open_Image onclick="this.style.display='none'; Codehighlighter1_526_819_Open_Text.style.display='none'; Codehighlighter1_526_819_Closed_Image.style.display='inline'; Codehighlighter1_526_819_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_526_819_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_526_819_Closed_Text.style.display='none'; Codehighlighter1_526_819_Open_Image.style.display='inline'; Codehighlighter1_526_819_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_526_819_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_526_819_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG id=Codehighlighter1_540_655_Open_Image onclick="this.style.display='none'; Codehighlighter1_540_655_Open_Text.style.display='none'; Codehighlighter1_540_655_Closed_Image.style.display='inline'; Codehighlighter1_540_655_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_540_655_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_540_655_Closed_Text.style.display='none'; Codehighlighter1_540_655_Open_Image.style.display='inline'; Codehighlighter1_540_655_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_540_655_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_540_655_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sessionFactory&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Configuration().configure()<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.buildSessionFactory();<BR><IMG id=Codehighlighter1_678_813_Open_Image onclick="this.style.display='none'; Codehighlighter1_678_813_Open_Text.style.display='none'; Codehighlighter1_678_813_Closed_Image.style.display='inline'; Codehighlighter1_678_813_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_678_813_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_678_813_Closed_Text.style.display='none'; Codehighlighter1_678_813_Open_Image.style.display='inline'; Codehighlighter1_678_813_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">&nbsp;(Throwable&nbsp;ex)&nbsp;</SPAN><SPAN id=Codehighlighter1_678_813_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_678_813_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.error(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Initial&nbsp;SessionFactory&nbsp;creation&nbsp;failed.</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;ex);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">throw</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ExceptionInInitializerError(ex);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;final&nbsp;ThreadLocal&nbsp;session&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ThreadLocal();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG id=Codehighlighter1_931_1109_Open_Image onclick="this.style.display='none'; Codehighlighter1_931_1109_Open_Text.style.display='none'; Codehighlighter1_931_1109_Closed_Image.style.display='inline'; Codehighlighter1_931_1109_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_931_1109_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_931_1109_Closed_Text.style.display='none'; Codehighlighter1_931_1109_Open_Image.style.display='inline'; Codehighlighter1_931_1109_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;Session&nbsp;currentSession()&nbsp;</SPAN><SPAN id=Codehighlighter1_931_1109_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_931_1109_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Session&nbsp;s&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(Session)&nbsp;session.</SPAN><SPAN style="COLOR: #0000ff">get</SPAN><SPAN style="COLOR: #000000">();<BR><IMG id=Codehighlighter1_1001_1085_Open_Image onclick="this.style.display='none'; Codehighlighter1_1001_1085_Open_Text.style.display='none'; Codehighlighter1_1001_1085_Closed_Image.style.display='inline'; Codehighlighter1_1001_1085_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1001_1085_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1001_1085_Closed_Text.style.display='none'; Codehighlighter1_1001_1085_Open_Image.style.display='inline'; Codehighlighter1_1001_1085_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(s&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_1001_1085_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1001_1085_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;sessionFactory.openSession();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">(s);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;s;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG id=Codehighlighter1_1150_1274_Open_Image onclick="this.style.display='none'; Codehighlighter1_1150_1274_Open_Text.style.display='none'; Codehighlighter1_1150_1274_Closed_Image.style.display='inline'; Codehighlighter1_1150_1274_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1150_1274_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1150_1274_Closed_Text.style.display='none'; Codehighlighter1_1150_1274_Open_Image.style.display='inline'; Codehighlighter1_1150_1274_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;closeSession()&nbsp;</SPAN><SPAN id=Codehighlighter1_1150_1274_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1150_1274_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Session&nbsp;s&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(Session)&nbsp;session.</SPAN><SPAN style="COLOR: #0000ff">get</SPAN><SPAN style="COLOR: #000000">();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(s&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.clear();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV></DIV><BR><BR>在同一個執行緒中，Session被暫存下來了，但無須擔心資料庫連結Connection持續占用問題，Hibernate會在真正需要資料庫操作時才（從連接池中）取得Connection。 <BR><BR>在程式中可以這麼使用HibernateUtil：<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN style="FONT-WEIGHT: bold">Session session = HibernateUtil.currentSession();</SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">User user = (User) session.load(User.class, new Integer(1));</SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">System.out.println(user.getName());</SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">HibernateUtil.closeSession();</SPAN><BR></DIV><BR>在Web應用程式中，可以藉助Filter來進行Session管理，在需要的時候開啟Session，並在Request結束之後關閉Session，這個部份，在 <A href="http://www.javaworld.com.tw/"><FONT color=#002c99>JavaWorld 技術論壇</FONT></A> 的 <A href="http://www.javaworld.com.tw/confluence/homepage.action"><FONT color=#002c99>Wiki</FONT></A> 上有篇 <A href="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=805"><FONT color=#002c99>在filter中關閉session 可以參考</FONT></A>。<BR><SPAN id=Codehighlighter1_160_1169_Open_Text><SPAN id=Codehighlighter1_295_1169_Open_Text><SPAN id=Codehighlighter1_308_1169_Open_Text></SPAN></SPAN></SPAN><img src ="http://www.blogjava.net/konhon/aggbug/9767.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/konhon/" target="_blank">konhon 优华</a> 2005-08-11 09:14 <a href="http://www.blogjava.net/konhon/archive/2005/08/11/9767.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何学习Hibernate[轉]</title><link>http://www.blogjava.net/konhon/archive/2005/08/09/9665.html</link><dc:creator>konhon 优华</dc:creator><author>konhon 优华</author><pubDate>Tue, 09 Aug 2005 08:14:00 GMT</pubDate><guid>http://www.blogjava.net/konhon/archive/2005/08/09/9665.html</guid><wfw:comment>http://www.blogjava.net/konhon/comments/9665.html</wfw:comment><comments>http://www.blogjava.net/konhon/archive/2005/08/09/9665.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/konhon/comments/commentRss/9665.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/konhon/services/trackbacks/9665.html</trackback:ping><description><![CDATA[<EM><FONT size=2>http://www.javajia.com<BR></FONT></EM><BR>Hibernate入门容易，掌握精通我也不敢自夸。我第一遍看Hibernate文档的时候也觉得很吃力，但不是因为Hibernate难掌握而感到吃力，是因为Hibernate文档处处都是持久层设计的经验和最佳实践。Hibernate文档准确的来说，绝大部分内容都在讲对象的持久层设计，而不是简单的Hibernate使用，使用问题查Java&nbsp;doc就够了。所以学习Hibernate，主要是在学习持久层的设计模式，如果你把Hibernate文档都看完了，还整天只会提那些&nbsp;Hibernate的配置问题，Hibernate的类调用问题，我觉得这样的人还没有真正的入门，算是白学了。&nbsp; <BR><BR>我对Hibernate&nbsp;的那些配置也不是特别纯熟，每次写hbm，都要对照文档一点点的检查；类调用参数也不太记得，写代码也要Java&nbsp;doc随时备查。但是我在学习Hibernate的时候即集中所有精力来理解Hibernate的运行原理，集中精力来掌握持久层设计应该把握的原则和技巧，这些才对我是最重用的东西。毫不夸张的说，学习完Hibernate，我对JDBC的编程也提高了一大截，更不要说对于J2EE架构的持久层的框架设计，基本上是了然于胸了，即使将来换了API，不用Hibernate的，改用JDO，Castor什么的，这些经验一样照用。&nbsp; <BR><BR>学习Hibernate主要不是在学习Hibernat怎么配置，用工具怎么生成hbm文件，如果你把重点放在这里，基本上等于白学了Hibernate。Hibernate的精华在于无与伦比的灵巧的对象持久层设计，这些持久层设计经验不会因为你不用Hibernate而丧失掉，我自己学习Hibernate，已经明显感觉到对持久层设计能力已经长了很多经验值了，这些经验甚至不光可以用在Java上，用在.net上也是一样。所以Hibernate配置的学习，我只是简单看看，用的时候知道到那里去查就行了，一堆复杂的生成工具我根本就看都不去看，这样算下来，掌握Hibernate的配置，可以用Hibernate来替代JDBC写程序，不过花上3天时间就足够了。我想3天时间对你来说不算很奢侈的学习代价吧。&nbsp; <BR><BR>为什么我这么强调学习Hibernate的对象持久层设计理念呢？那就看你的理想是想一辈子做一个程序员呢？还是想向更高的方向发展呢？从纯做技术的角度来说，职业发展的最高点是“系统架构师”，Bill&nbsp;Gates不是还叫做微软的首席系统架构师吗？System&nbsp;Architect职位需要的是你的学习和领悟能力，如果你不能把学习Hibernate得到的设计经验运用到其它地方，那么你是失败的，也没有资格做&nbsp;System&nbsp;Architect。&nbsp; <BR><BR>不管JDO也好，Hibernate也好，TopLink也好，CocoBase也好，还是&nbsp;Castor，还是什么Torque，OJB，软件的使用和配置方法可以各异，但本质上都是ORM，都是对JDBC的对象持久层封装，所以万变不离其宗，如果你完整的学习和掌握Hibernate花了1个月的时间，那么你再学习OJB的时间不应该超过1个星期，因为你已经把对象持久层设计都了然于胸了，你需要的只是熟悉一下OJB的API和配置罢了，至于怎么运用OJB进行持久层的开发你早就已经熟悉了。&nbsp; <BR><BR>所以当你掌握了两种以上的ORM，你应该能够不拘于使用的ORM软件的限制，设计出适合于你的项目的持久层来，这才是System&nbsp;Architect的水准。用金庸小说来打个比方来说吧，张无忌学太极剑，只记剑意，不记剑招，这才是真正的高手，而低手就只会去学习剑招，而不去领会剑招背后蕴含的剑意，所以一辈子都是低手，永远不能真正学会太极剑。所以周颠看到张三丰第二次演示太极剑，招式完全不同就以为是另一套东西，其实本质上都一样。学习Hibernate也不要舍本逐末的去学各种五花八门的工具，重点掌握它的对象持久层设计理念。 <img src ="http://www.blogjava.net/konhon/aggbug/9665.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/konhon/" target="_blank">konhon 优华</a> 2005-08-09 16:14 <a href="http://www.blogjava.net/konhon/archive/2005/08/09/9665.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>