﻿<?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-★yesjoy★-文章分类-Hibernate学习</title><link>http://www.blogjava.net/yesjoy/category/7321.html</link><description>&lt;font color="red"&gt;★&lt;/font&gt;&lt;font color="blue"&gt;总在爬山 所以艰辛;总在寻梦 所以苦痛&lt;/font&gt;&lt;font color="red"&gt;★&lt;/font&gt;</description><language>zh-cn</language><lastBuildDate>Tue, 31 Aug 2010 08:05:17 GMT</lastBuildDate><pubDate>Tue, 31 Aug 2010 08:05:17 GMT</pubDate><ttl>60</ttl><item><title>Hibernate中Cascade和Inverse </title><link>http://www.blogjava.net/yesjoy/articles/80164.html</link><dc:creator>★yesjoy★</dc:creator><author>★yesjoy★</author><pubDate>Thu, 09 Nov 2006 07:26:00 GMT</pubDate><guid>http://www.blogjava.net/yesjoy/articles/80164.html</guid><wfw:comment>http://www.blogjava.net/yesjoy/comments/80164.html</wfw:comment><comments>http://www.blogjava.net/yesjoy/articles/80164.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yesjoy/comments/commentRss/80164.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yesjoy/services/trackbacks/80164.html</trackback:ping><description><![CDATA[
		<p>１、到底在哪用cascade="..."？</p>
		<p>cascade属性并不是多对多关系一定要用的，有了它只是让我们在插入或删除对像时更方便一些，只要在cascade的源头上插入或是删除，所有cascade的关系就会被自己动的插入或是删除。便是为了能正确的cascade，unsaved-value是个很重要的属性。Hibernate通过这个属性来判断一个对象应该save还是update，如果这个对象的id是unsaved-value的话，那说明这个对象不是persistence object要save（insert)；如果id是非unsaved-value的话，那说明这个对象是persistence object（数据库中已存在），只要update就行了。saveOrUpdate方法用的也是这个机制。</p>
		<p>２、到底在哪用inverse="ture"?</p>
		<p>inverse属性默认是false的，就是说关系的两端都来维护关系。这个意思就是说，如有一个Student, Teacher和TeacherStudent表，Student和Teacher是多对多对多关系，这个关系由TeacherStudent这个表来表现。那么什么时候插入或删除TeacherStudent表中的记录来维护关系呢？在用hibernate时，我们不会显示的对TeacherStudent表做操作。对TeacherStudent的操作是hibernate帮我们做的。hibernate就是看hbm文件中指定的是"谁"维护关系，那个在插入或删除"谁"时，就会处发对关系表的操作。前提是"谁"这个对象已经知道这个关系了，就是说关系另一头的对象已经set或是add到"谁"这个对象里来了。前面说过inverse默认是false，就是关系的两端都维护关系，对其中任一个操作都会处发对表系表的操作。当在关系的一头，如Student中的bag或set中用了inverse＝"true"时，那就代表关系是由另一关维护的（Teacher）。就是说当这插入Student时，不会操作TeacherStudent表，即使Student已经知道了关系。只有当Teacher插入或删除时才会处发对关系表的操作。所以，当关系的两头都用inverse="true"是不对的，就会导致任何操作都不处发对关系表的操作。当两端都是inverse="false"或是default值是，在代码对关系显示的维护也是不对的，会导致在关系表中插入两次关系。</p>
		<p>在一对多关系中inverse就更有意义了。在多对多中，在哪端inverse="true"效果差不多（在效率上）。但是在一对多中，如果要一方维护关系，就会使在插入或是删除"一"方时去update"多"方的每一个与这个"一"的对象有关系的对象。而如果让"多"方面维护关系时就不会有update操作，因为关系就是在多方的对象中的，直指插入或是删除多方对象就行了。当然这时也要遍历"多"方的每一个对象显示的操作修关系的变化体现到DB中。不管怎样说，还是让"多"方维护关系更直观一些。</p>
		<p>３、cascade和inverse有什么区别？</p>
		<p>可以这样理解，cascade定义的是关系两端对象到对象的级联关系；而inverse定义的是关系和对象的级联关系。</p>
		<p>Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=741468</p>
		<p>详见：http://www.bczs.net/xml/2005/12/21/4473293.xml</p>
		<p>
				<a href="http://welcomejianghome.spaces.msn.com/blog/cns!3196CAE67CDF2316!149.entry" target="blank">http://welcomejianghome.spaces.msn.com/blog/cns!3196CAE67CDF2316!149.entry</a>
		</p>
		<p>all : 所有情况下均进行关联操作。 <br />none：所有情况下均不进行关联操作。这是默认值。 <br />save-update:在执行save/update/saveOrUpdate时进行关联操作。 <br />delete：在执行delete时进行关联操作。 </p>
		<p>all的意思是save-update + delete <br />all-delete-orphan 的意思是当对象图中产生孤儿节点时,在数据库中删除该节点 <br />all比较好理解,举个例子说一下all-delete-orphan: <br />Category与Item是一对多的关系,也就是说Category类中有个Set类型的变量items. <br />举个例子,现items中存两个Item, item1,item2,如果定义关系为all-delete-orphan <br />当items中删除掉一个item(比如用remove()方法删除item1),那么被删除的Item类实例 <br />将变成孤儿节点,当执行category.update(),或session.flush()时 <br />hibernate同步缓存和数据库,会把数据库中item1对应的记录删掉 </p>
<img src ="http://www.blogjava.net/yesjoy/aggbug/80164.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yesjoy/" target="_blank">★yesjoy★</a> 2006-11-09 15:26 <a href="http://www.blogjava.net/yesjoy/articles/80164.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>DetachedCriteria关联查询</title><link>http://www.blogjava.net/yesjoy/articles/76346.html</link><dc:creator>★yesjoy★</dc:creator><author>★yesjoy★</author><pubDate>Fri, 20 Oct 2006 03:10:00 GMT</pubDate><guid>http://www.blogjava.net/yesjoy/articles/76346.html</guid><wfw:comment>http://www.blogjava.net/yesjoy/comments/76346.html</wfw:comment><comments>http://www.blogjava.net/yesjoy/articles/76346.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yesjoy/comments/commentRss/76346.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yesjoy/services/trackbacks/76346.html</trackback:ping><description><![CDATA[
		<font face="Arial" size="2">Spring+Hibernate3的框架中用到了关联查询<br /><br />表User：Address是一对多的关系<br />UserBean中有一个addresses的Set<br /><br />现在要查询出这样的User，拥有Address中门牌doorplate为"642"的User，<br />现在DB中只有一个这样的User，这个User拥有门牌doorplate为"642"的Address五个~~<br /><br />创建DetachedCriteria 的语句如下：<br /></font>
		<br />DetachedCriteria detachedCriteria = DetachedCriteria.forClass(User.<b>class</b>);<br />detachedCriteria.createCriteria(<font color="#00bb00">"才"</font><font color="black">).add(Restrictions.like(</font><font color="#00bb00">"doorplate"</font><font color="black">,</font><font color="#00bb00">"642"</font><font color="black">));<br /><br />实际查询语句如下：<br /><pre><br />List list = getHibernateTemplate().findByCriteria(queryCriteria);<br /></pre><br />理论上查询出来的list应该User的list是1<br /><br />总结如下<br />1：<br />DetachedCriteria addressCriteria = DetachedCriteria.forClass(User.class).createCriteria("<font color="#00bb00">addresses</font>");<br />addressCriteria.add(Restrictions.eq(<font color="#00bb00">"doorplate"</font><font color="black">,</font><font color="#00bb00">"642"</font>)):<br /><br />2：<br />DetachedCriteria addressCriteria = DetachedCriteria.forClass(User.class).createAlias("<font color="#00bb00">addresses</font>", "a");<br />addressCriteria.add(Restrictions.eq(<font color="#00bb00">"a.doorplate"</font><font color="black">,</font><font color="#00bb00">"642"</font>)):</font><img src ="http://www.blogjava.net/yesjoy/aggbug/76346.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yesjoy/" target="_blank">★yesjoy★</a> 2006-10-20 11:10 <a href="http://www.blogjava.net/yesjoy/articles/76346.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>case when then...的用法</title><link>http://www.blogjava.net/yesjoy/articles/75000.html</link><dc:creator>★yesjoy★</dc:creator><author>★yesjoy★</author><pubDate>Fri, 13 Oct 2006 06:56:00 GMT</pubDate><guid>http://www.blogjava.net/yesjoy/articles/75000.html</guid><wfw:comment>http://www.blogjava.net/yesjoy/comments/75000.html</wfw:comment><comments>http://www.blogjava.net/yesjoy/articles/75000.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yesjoy/comments/commentRss/75000.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yesjoy/services/trackbacks/75000.html</trackback:ping><description><![CDATA[
		<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">
				<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
				<span style="COLOR: #000000">、</span>
				<span style="COLOR: #0000ff">select</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff00ff">sum</span>
				<span style="COLOR: #000000">(t1.useHours) </span>
				<span style="COLOR: #0000ff">as</span>
				<span style="COLOR: #000000"> hours1 </span>
				<span style="COLOR: #0000ff">from</span>
				<span style="COLOR: #000000"> Table1 t1 </span>
				<span style="COLOR: #0000ff">where</span>
				<span style="COLOR: #000000"> condition1<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
				<span style="COLOR: #000000">、</span>
				<span style="COLOR: #0000ff">select</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff00ff">sum</span>
				<span style="COLOR: #000000">(t2.useHours) </span>
				<span style="COLOR: #0000ff">as</span>
				<span style="COLOR: #000000"> hours2 </span>
				<span style="COLOR: #0000ff">from</span>
				<span style="COLOR: #000000"> Table2 t2 </span>
				<span style="COLOR: #0000ff">where</span>
				<span style="COLOR: #000000"> condition2<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" />语句1得到结果为10<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />语句2得到结果为20<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" />hours1 hours2 <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">10</span>
				<span style="COLOR: #000000">     </span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">20</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" />直接在写sql时做一下数据连结就可以了<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #0000ff">select</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />(</span>
				<span style="COLOR: #0000ff">select</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff00ff">sum</span>
				<span style="COLOR: #000000">(t1.useHours) </span>
				<span style="COLOR: #0000ff">from</span>
				<span style="COLOR: #000000"> Table1 t1 </span>
				<span style="COLOR: #0000ff">where</span>
				<span style="COLOR: #000000"> condition1) hours1,<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />(</span>
				<span style="COLOR: #0000ff">select</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff00ff">sum</span>
				<span style="COLOR: #000000">(t2.useHours) </span>
				<span style="COLOR: #0000ff">from</span>
				<span style="COLOR: #000000"> Table2 t2 </span>
				<span style="COLOR: #0000ff">where</span>
				<span style="COLOR: #000000"> condition2) hours2<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" />hql </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> "</span>
				<span style="COLOR: #0000ff">select</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff00ff">sum</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #ff00ff">case</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">when</span>
				<span style="COLOR: #000000"> " </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> whereSql1 </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> " </span>
				<span style="COLOR: #0000ff">then</span>
				<span style="COLOR: #000000"> (pro.preengageHoursOrSamples</span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000">pro.adjustOfHours) </span>
				<span style="COLOR: #0000ff">else</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">end</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #0000ff">as</span>
				<span style="COLOR: #000000"> a,";<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />hql </span>
				<span style="COLOR: #808080">+=</span>
				<span style="COLOR: #000000"> " </span>
				<span style="COLOR: #ff00ff">sum</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #ff00ff">case</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">when</span>
				<span style="COLOR: #000000"> " </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> whereSql2 </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> " </span>
				<span style="COLOR: #0000ff">then</span>
				<span style="COLOR: #000000"> (pro.preengageHoursOrSamples</span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000">pro.adjustOfHours) </span>
				<span style="COLOR: #0000ff">else</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">end</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #0000ff">as</span>
				<span style="COLOR: #000000"> b,";<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />hql </span>
				<span style="COLOR: #808080">+=</span>
				<span style="COLOR: #000000"> " </span>
				<span style="COLOR: #ff00ff">sum</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #ff00ff">case</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">when</span>
				<span style="COLOR: #000000"> " </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> whereSql3 </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> " </span>
				<span style="COLOR: #0000ff">then</span>
				<span style="COLOR: #000000"> (pro.preengageHoursOrSamples</span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000">pro.adjustOfHours) </span>
				<span style="COLOR: #0000ff">else</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">end</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #0000ff">as</span>
				<span style="COLOR: #000000"> c";<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />hql </span>
				<span style="COLOR: #808080">+=</span>
				<span style="COLOR: #000000"> " </span>
				<span style="COLOR: #0000ff">from</span>
				<span style="COLOR: #000000"> PreengageOddPO pro </span>
				<span style="COLOR: #0000ff">where</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
				<span style="COLOR: #808080">=</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
				<span style="COLOR: #000000">";</span>
		</div>
<img src ="http://www.blogjava.net/yesjoy/aggbug/75000.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yesjoy/" target="_blank">★yesjoy★</a> 2006-10-13 14:56 <a href="http://www.blogjava.net/yesjoy/articles/75000.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate中使用本地sql的方法</title><link>http://www.blogjava.net/yesjoy/articles/74616.html</link><dc:creator>★yesjoy★</dc:creator><author>★yesjoy★</author><pubDate>Wed, 11 Oct 2006 08:56:00 GMT</pubDate><guid>http://www.blogjava.net/yesjoy/articles/74616.html</guid><wfw:comment>http://www.blogjava.net/yesjoy/comments/74616.html</wfw:comment><comments>http://www.blogjava.net/yesjoy/articles/74616.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yesjoy/comments/commentRss/74616.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yesjoy/services/trackbacks/74616.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">
				<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #000000">使用方法:<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />String hql </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> "</span>
				<span style="COLOR: #0000ff">select</span>
				<span style="COLOR: #000000"> {pro.</span>
				<span style="COLOR: #808080">*</span>
				<span style="COLOR: #000000">} </span>
				<span style="COLOR: #0000ff">from</span>
				<span style="COLOR: #000000"> T_ES_PreengageOdd pro </span>
				<span style="COLOR: #0000ff">where</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
				<span style="COLOR: #808080">=</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
				<span style="COLOR: #000000">";<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />SQLQuery sqlQuery </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> this.getSession().createSQLQuery(hql);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />sqlQuery.addEntity("pro",PreengageOddPO.class);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />List list </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> sqlQuery.list();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> list;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />说明:T_ES_PreengageOdd是你数据库中实际的表名.<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />PreengageOddPO是你PO对象,它对应映射成表T_ES_PreengageOdd.<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
		</div>
		<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">        SQLQuery sqlQuery1 </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> this.getSession().createSQLQuery(<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                "</span>
				<span style="COLOR: #0000ff">select</span>
				<span style="COLOR: #000000"> {ep.</span>
				<span style="COLOR: #808080">*</span>
				<span style="COLOR: #000000">} </span>
				<span style="COLOR: #0000ff">from</span>
				<span style="COLOR: #000000"> T_ES_Equipment ep </span>
				<span style="COLOR: #0000ff">where</span>
				<span style="COLOR: #000000"> ep.CHNNAME </span>
				<span style="COLOR: #808080">like</span>
				<span style="COLOR: #000000"> :equipname </span>
				<span style="COLOR: #808080">and</span>
				<span style="COLOR: #000000"> ep.equip_id </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000">:equipid");<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        sqlQuery1.addEntity("ep", EquipmentPO.class);<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" />        sqlQuery1.setString("equipname", "</span>
				<span style="COLOR: #808080">%</span>
				<span style="COLOR: #000000">系统</span>
				<span style="COLOR: #808080">%</span>
				<span style="COLOR: #000000">");<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        sqlQuery1.setString("equipid", "</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11020874</span>
				<span style="COLOR: #000000">");<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        List result </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> sqlQuery1.list();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (result </span>
				<span style="COLOR: #808080">!=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">null</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">&amp;&amp;</span>
				<span style="COLOR: #000000"> result.size() </span>
				<span style="COLOR: #808080">&gt;</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
				<span style="COLOR: #000000">) {<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span>
				<span style="COLOR: #0000ff">for</span>
				<span style="COLOR: #000000"> (</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
				<span style="COLOR: #000000"> i </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
				<span style="COLOR: #000000">; i </span>
				<span style="COLOR: #808080">&lt;</span>
				<span style="COLOR: #000000"> result.size(); i</span>
				<span style="COLOR: #808080">++</span>
				<span style="COLOR: #000000">) {<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                EquipmentPO equipmentPO </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> (EquipmentPO) result.get(i);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                System.out.println("equipname</span>
				<span style="COLOR: #808080">===&gt;</span>
				<span style="COLOR: #000000">" </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> equipmentPO.getChineseName());<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" />        }<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />打印结果:</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />equipname</span>
				<span style="COLOR: #808080">===&gt;</span>
				<span style="COLOR: #000000">气质系统</span>
		</div>
<img src ="http://www.blogjava.net/yesjoy/aggbug/74616.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yesjoy/" target="_blank">★yesjoy★</a> 2006-10-11 16:56 <a href="http://www.blogjava.net/yesjoy/articles/74616.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate QBC的查询方式的总结</title><link>http://www.blogjava.net/yesjoy/articles/65782.html</link><dc:creator>★yesjoy★</dc:creator><author>★yesjoy★</author><pubDate>Fri, 25 Aug 2006 06:24:00 GMT</pubDate><guid>http://www.blogjava.net/yesjoy/articles/65782.html</guid><wfw:comment>http://www.blogjava.net/yesjoy/comments/65782.html</wfw:comment><comments>http://www.blogjava.net/yesjoy/articles/65782.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yesjoy/comments/commentRss/65782.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yesjoy/services/trackbacks/65782.html</trackback:ping><description><![CDATA[
		<p> </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">
				<span style="COLOR: #008080"> 1</span>
				<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #000000">        Criteria criteria </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> this.getSession().createCriteria(EquipmentPO.class);<br /></span>
				<span style="COLOR: #008080"> 2</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.allEq(new Map(Restrictions.eq("equipID",new String("</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11020449</span>
				<span style="COLOR: #000000">")),Restrictions.eq("equipID",new String("</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11020449</span>
				<span style="COLOR: #000000">"))));        <br /></span>
				<span style="COLOR: #008080"> 3</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">注意：between查询条件可解释为查询EquipmentPO对象中的equipID属性值在new String("</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11020449</span>
				<span style="COLOR: #000000">")和new String("</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11030137</span>
				<span style="COLOR: #000000">")之间的所有记录值（包含两个端点）<br /></span>
				<span style="COLOR: #008080"> 4</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.</span>
				<span style="COLOR: #808080">between</span>
				<span style="COLOR: #000000">("equipID",new String("</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11020449</span>
				<span style="COLOR: #000000">"),new String("</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11030137</span>
				<span style="COLOR: #000000">")));<br /></span>
				<span style="COLOR: #008080"> 5</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">查询结果列表记录按照equipID的属性值来升序排序<br /></span>
				<span style="COLOR: #008080"> 6</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.addOrder(</span>
				<span style="COLOR: #0000ff">Order</span>
				<span style="COLOR: #000000">.</span>
				<span style="COLOR: #0000ff">asc</span>
				<span style="COLOR: #000000">("equipID"));</span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">降序方式为：</span>
				<span style="COLOR: #0000ff">Order</span>
				<span style="COLOR: #000000">.</span>
				<span style="COLOR: #0000ff">desc</span>
				<span style="COLOR: #000000">("equipID")        <br /></span>
				<span style="COLOR: #008080"> 7</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">通过EquipmentPO对象的主键id来查询<br /></span>
				<span style="COLOR: #008080"> 8</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.idEq("402882ac0d3f7ca8010d3f7ef869000b"));        <br /></span>
				<span style="COLOR: #008080"> 9</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        Restrictions.</span>
				<span style="COLOR: #808080">like</span>
				<span style="COLOR: #000000">(fieldName, "</span>
				<span style="COLOR: #808080">%</span>
				<span style="COLOR: #000000">" </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> para</span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> "</span>
				<span style="COLOR: #808080">%</span>
				<span style="COLOR: #000000">")<br /></span>
				<span style="COLOR: #008080">10</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">使用ilike方式进行模糊查询<br /></span>
				<span style="COLOR: #008080">11</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.ilike("equipID",new String("</span>
				<span style="COLOR: #808080">%</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11020</span>
				<span style="COLOR: #808080">%</span>
				<span style="COLOR: #000000">")));<br /></span>
				<span style="COLOR: #008080">12</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">ilike的i即ignore之意,所以这里查询出englishName值为"Optima XL 100K Ultracentrifuge"（忽略大小写）的记录<br /></span>
				<span style="COLOR: #008080">13</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.ilike("englishName",new String("Optima XL 100K Ultracentrifuge"),MatchMode.ANYWHERE));</span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">这里        <br /></span>
				<span style="COLOR: #008080">14</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">使用in方式有两种形式，即数组或者Collection的方式，可参考如下两个实例<br /></span>
				<span style="COLOR: #008080">15</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.</span>
				<span style="COLOR: #808080">in</span>
				<span style="COLOR: #000000">("equipID",new String</span>
				<span style="COLOR: #ff0000">[]</span>
				<span style="COLOR: #000000">{"</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11020449</span>
				<span style="COLOR: #000000">","</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11020450</span>
				<span style="COLOR: #000000">"}));</span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">数组参数<br /></span>
				<span style="COLOR: #008080">16</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        Collection col </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> new ArrayList();<br /></span>
				<span style="COLOR: #008080">17</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        col.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(new String("</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11020449</span>
				<span style="COLOR: #000000">"));<br /></span>
				<span style="COLOR: #008080">18</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        col.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(new String("</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11020450</span>
				<span style="COLOR: #000000">"));<br /></span>
				<span style="COLOR: #008080">19</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        col.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(new String("</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11020874</span>
				<span style="COLOR: #000000">"));<br /></span>
				<span style="COLOR: #008080">20</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.</span>
				<span style="COLOR: #808080">in</span>
				<span style="COLOR: #000000">("equipID",col));</span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">Collection参数        <br /></span>
				<span style="COLOR: #008080">21</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">使用isEmpty</span>
				<span style="COLOR: #808080">/</span>
				<span style="COLOR: #000000">isNotEmpty方式用来判断EquipmentPO对象中的Collection类型的属性是否为空的所有记录<br /></span>
				<span style="COLOR: #008080">22</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">EquipmentPO对象中定义属性private </span>
				<span style="COLOR: #0000ff">Set</span>
				<span style="COLOR: #000000"> equipFunctionDevelopPOs </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> new HashSet(); </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000"> 设备功能开发对象<br /></span>
				<span style="COLOR: #008080">23</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.isEmpty("equipFunctionDevelopPOs"));<br /></span>
				<span style="COLOR: #008080">24</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.isNotEmpty("equipFunctionDevelopPOs"));<br /></span>
				<span style="COLOR: #008080">25</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">使用isNull方式查询出所有schoolID属性没有值的记录。说明：Restrictions.isNull判断属性是否为空，为空返回true，反之返回false<br /></span>
				<span style="COLOR: #008080">26</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.</span>
				<span style="COLOR: #ff00ff">isNull</span>
				<span style="COLOR: #000000">("schoolID"));<br /></span>
				<span style="COLOR: #008080">27</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.isNotNull("schoolID"));        <br /></span>
				<span style="COLOR: #008080">28</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">与Restrictions.eq正好相反，表示不存在(</span>
				<span style="COLOR: #808080">not</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">in</span>
				<span style="COLOR: #000000">)<br /></span>
				<span style="COLOR: #008080">29</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.</span>
				<span style="COLOR: #808080">not</span>
				<span style="COLOR: #000000">(Restrictions.eq("equipID",new String("</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">11020449</span>
				<span style="COLOR: #000000">"))));<br /></span>
				<span style="COLOR: #008080">30</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">使用Restrictions.sizeEq方式用来查询EquipmentPO对象中的Collection类型的属性equipFunctionDevelopPOs的size为1的所有记录<br /></span>
				<span style="COLOR: #008080">31</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.sizeEq("equipFunctionDevelopPOs",</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
				<span style="COLOR: #000000">));        <br /></span>
				<span style="COLOR: #008080">32</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">使用sql限定的查询。注意{alias}.chnname这里是指实际表中的字段名而非属性名（不区分大小写）<br /></span>
				<span style="COLOR: #008080">33</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.sqlRestriction("{alias}.chnname </span>
				<span style="COLOR: #808080">like</span>
				<span style="COLOR: #000000"> (?)","</span>
				<span style="COLOR: #808080">%</span>
				<span style="COLOR: #000000">电</span>
				<span style="COLOR: #808080">%</span>
				<span style="COLOR: #000000">",Hibernate.STRING));<br /></span>
				<span style="COLOR: #008080">34</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.sqlRestriction("{alias}.ENGNAME </span>
				<span style="COLOR: #808080">like</span>
				<span style="COLOR: #000000"> (?)","</span>
				<span style="COLOR: #808080">%</span>
				<span style="COLOR: #000000">ptima</span>
				<span style="COLOR: #808080">%</span>
				<span style="COLOR: #000000">",Hibernate.STRING));<br /></span>
				<span style="COLOR: #008080">35</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #808080">//</span>
				<span style="COLOR: #000000">如果有多个查询条件，比如between子句的查询则如下：        <br /></span>
				<span style="COLOR: #008080">36</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        BigDecimal</span>
				<span style="COLOR: #ff0000">[]</span>
				<span style="COLOR: #000000"> unitPrice </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> {new BigDecimal(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">402514</span>
				<span style="COLOR: #000000">),new BigDecimal(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">614891</span>
				<span style="COLOR: #000000">)};<br /></span>
				<span style="COLOR: #008080">37</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        Type</span>
				<span style="COLOR: #ff0000">[]</span>
				<span style="COLOR: #000000"> types </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> {Hibernate.BIG_DECIMAL,Hibernate.BIG_DECIMAL};<br /></span>
				<span style="COLOR: #008080">38</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        criteria.</span>
				<span style="COLOR: #0000ff">add</span>
				<span style="COLOR: #000000">(Restrictions.sqlRestriction("{alias}.unit_price </span>
				<span style="COLOR: #808080">between</span>
				<span style="COLOR: #000000"> (?) </span>
				<span style="COLOR: #808080">and</span>
				<span style="COLOR: #000000"> (?)",unitPrice,types));<br /></span>
				<span style="COLOR: #008080">39</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        List list </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> criteria.list();<br /></span>
				<span style="COLOR: #008080">40</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        System.out.println("size</span>
				<span style="COLOR: #808080">===&gt;</span>
				<span style="COLOR: #000000">" </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> list.size());<br /></span>
				<span style="COLOR: #008080">41</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> list;</span>
		</div>
<img src ="http://www.blogjava.net/yesjoy/aggbug/65782.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yesjoy/" target="_blank">★yesjoy★</a> 2006-08-25 14:24 <a href="http://www.blogjava.net/yesjoy/articles/65782.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>总结以下hibernate查询语言的用法</title><link>http://www.blogjava.net/yesjoy/articles/37847.html</link><dc:creator>★yesjoy★</dc:creator><author>★yesjoy★</author><pubDate>Tue, 28 Mar 2006 09:58:00 GMT</pubDate><guid>http://www.blogjava.net/yesjoy/articles/37847.html</guid><wfw:comment>http://www.blogjava.net/yesjoy/comments/37847.html</wfw:comment><comments>http://www.blogjava.net/yesjoy/articles/37847.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yesjoy/comments/commentRss/37847.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yesjoy/services/trackbacks/37847.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"</span>
				<span style="COLOR: #0000ff">?&gt;</span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<span style="COLOR: #0000ff">&lt;!</span>
				<span style="COLOR: #ff00ff">DOCTYPE hibernate-mapping PUBLIC<br />    "-//Hibernate/Hibernate Mapping DTD//EN"<br />    "http://hibernate.sourceforge.net/hibernate-mapping-2.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-mapping </span>
				<span style="COLOR: #ff0000">package</span>
				<span style="COLOR: #0000ff">="gov.cfte.object.hibernate.model"</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">="ObjectLog"</span>
				<span style="COLOR: #ff0000"> table</span>
				<span style="COLOR: #0000ff">="OBJECT_LOG"</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">column</span>
				<span style="COLOR: #0000ff">="ID"</span>
				<span style="COLOR: #ff0000"> name</span>
				<span style="COLOR: #0000ff">="Id"</span>
				<span style="COLOR: #ff0000"> type</span>
				<span style="COLOR: #0000ff">="string"</span>
				<span style="COLOR: #ff0000"> length</span>
				<span style="COLOR: #0000ff">="32"</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">="net.sf.hibernate.id.UUIDHexGenerator"</span>
				<span style="COLOR: #ff0000"> </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<br />            </span>
				<span style="COLOR: #ff0000">column</span>
				<span style="COLOR: #0000ff">="TYPE_ID"</span>
				<span style="COLOR: #ff0000">
						<br />            length</span>
				<span style="COLOR: #0000ff">="32"</span>
				<span style="COLOR: #ff0000">
						<br />            name</span>
				<span style="COLOR: #0000ff">="TypeId"</span>
				<span style="COLOR: #ff0000">
						<br />            not-null</span>
				<span style="COLOR: #0000ff">="false"</span>
				<span style="COLOR: #ff0000">
						<br />            type</span>
				<span style="COLOR: #0000ff">="string"</span>
				<span style="COLOR: #ff0000">
						<br />         </span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />        </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property<br />            </span>
				<span style="COLOR: #ff0000">column</span>
				<span style="COLOR: #0000ff">="OPTIME"</span>
				<span style="COLOR: #ff0000">
						<br />            length</span>
				<span style="COLOR: #0000ff">="26"</span>
				<span style="COLOR: #ff0000">
						<br />            name</span>
				<span style="COLOR: #0000ff">="Optime"</span>
				<span style="COLOR: #ff0000">
						<br />            not-null</span>
				<span style="COLOR: #0000ff">="false"</span>
				<span style="COLOR: #ff0000">
						<br />            type</span>
				<span style="COLOR: #0000ff">="timestamp"</span>
				<span style="COLOR: #ff0000">
						<br />         </span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />        </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property<br />            </span>
				<span style="COLOR: #ff0000">column</span>
				<span style="COLOR: #0000ff">="USER_ID"</span>
				<span style="COLOR: #ff0000">
						<br />            length</span>
				<span style="COLOR: #0000ff">="32"</span>
				<span style="COLOR: #ff0000">
						<br />            name</span>
				<span style="COLOR: #0000ff">="UserId"</span>
				<span style="COLOR: #ff0000">
						<br />            not-null</span>
				<span style="COLOR: #0000ff">="false"</span>
				<span style="COLOR: #ff0000">
						<br />            type</span>
				<span style="COLOR: #0000ff">="string"</span>
				<span style="COLOR: #ff0000">
						<br />         </span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />        </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property<br />            </span>
				<span style="COLOR: #ff0000">column</span>
				<span style="COLOR: #0000ff">="OPINFO"</span>
				<span style="COLOR: #ff0000">
						<br />            length</span>
				<span style="COLOR: #0000ff">="32"</span>
				<span style="COLOR: #ff0000">
						<br />            name</span>
				<span style="COLOR: #0000ff">="Opinfo"</span>
				<span style="COLOR: #ff0000">
						<br />            not-null</span>
				<span style="COLOR: #0000ff">="false"</span>
				<span style="COLOR: #ff0000">
						<br />            type</span>
				<span style="COLOR: #0000ff">="string"</span>
				<span style="COLOR: #ff0000">
						<br />         </span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />        </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property<br />            </span>
				<span style="COLOR: #ff0000">column</span>
				<span style="COLOR: #0000ff">="RECORDINFO"</span>
				<span style="COLOR: #ff0000">
						<br />            length</span>
				<span style="COLOR: #0000ff">="1024"</span>
				<span style="COLOR: #ff0000">
						<br />            name</span>
				<span style="COLOR: #0000ff">="Recordinfo"</span>
				<span style="COLOR: #ff0000">
						<br />            not-null</span>
				<span style="COLOR: #0000ff">="false"</span>
				<span style="COLOR: #ff0000">
						<br />            type</span>
				<span style="COLOR: #0000ff">="string"</span>
				<span style="COLOR: #ff0000">
						<br />         </span>
				<span style="COLOR: #0000ff">/&gt;</span>
				<span style="COLOR: #000000">
						<br />        </span>
				<span style="COLOR: #0000ff">&lt;</span>
				<span style="COLOR: #800000">property<br />            </span>
				<span style="COLOR: #ff0000">column</span>
				<span style="COLOR: #0000ff">="IP"</span>
				<span style="COLOR: #ff0000">
						<br />            length</span>
				<span style="COLOR: #0000ff">="32"</span>
				<span style="COLOR: #ff0000">
						<br />            name</span>
				<span style="COLOR: #0000ff">="Ip"</span>
				<span style="COLOR: #ff0000">
						<br />            not-null</span>
				<span style="COLOR: #0000ff">="false"</span>
				<span style="COLOR: #ff0000">
						<br />            type</span>
				<span style="COLOR: #0000ff">="string"</span>
				<span style="COLOR: #ff0000">
						<br />         </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>
		<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">return</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">this</span>
				<span style="COLOR: #000000">.getHibernateTemplate().find(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">from ObjectLog</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br /></span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">this</span>
				<span style="COLOR: #000000">.getHibernateTemplate().find(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">from ObjectLog c where c.Opinfo = ?</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">, queryCondition);</span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">根据操作方式来精确查询(已测试通过)</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">this</span>
				<span style="COLOR: #000000">.getHibernateTemplate().find(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">from ObjectLog c where c.Opinfo like ?</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">%</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">queryCondition</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">%</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);</span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">根据操作方式来模糊查询(已测试通过)</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">this</span>
				<span style="COLOR: #000000">.getHibernateTemplate().find(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">from ObjectLog c where c.TypeId=? and c.Opinfo like ?</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> Object[]{typeId,</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">%</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">queryCondition</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">%</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">});</span>
		</div>
<img src ="http://www.blogjava.net/yesjoy/aggbug/37847.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yesjoy/" target="_blank">★yesjoy★</a> 2006-03-28 17:58 <a href="http://www.blogjava.net/yesjoy/articles/37847.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于hibernate的配置文件hibernate.cfg.xml的详细解释</title><link>http://www.blogjava.net/yesjoy/articles/31750.html</link><dc:creator>★yesjoy★</dc:creator><author>★yesjoy★</author><pubDate>Tue, 21 Feb 2006 02:33:00 GMT</pubDate><guid>http://www.blogjava.net/yesjoy/articles/31750.html</guid><wfw:comment>http://www.blogjava.net/yesjoy/comments/31750.html</wfw:comment><comments>http://www.blogjava.net/yesjoy/articles/31750.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yesjoy/comments/commentRss/31750.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yesjoy/services/trackbacks/31750.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&nbsp;version="1.0"</SPAN><SPAN style="COLOR: #0000ff">?&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">&lt;!</SPAN><SPAN style="COLOR: #ff00ff">DOCTYPE&nbsp;hibernate-configuration&nbsp;PUBLIC&nbsp;"-//Hibernate/Hibernate&nbsp;Configuration&nbsp;DTD//EN"<BR>"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"</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>&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="dialect"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">org.hibernate.dialect.MySQLDialect</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">property</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">一个Hibernate&nbsp;Dialect类名允许Hibernate针对特定的关系数据库生成优化的SQL<BR></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="connection.driver_class"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">com.mysql.jdbc.Driver</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">property</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">jdbc驱动类<BR></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="connection.url"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">jdbc:mysql://localhost/student</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">property</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">jdbc&nbsp;URL<BR></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="connection.username"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">root</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&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="connection.password"</SPAN><SPAN style="COLOR: #0000ff">&gt;&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&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="hibernate.connection.pool.size"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">10</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&nbsp;</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">true</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">property</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">输出所有SQL语句到控制台<BR></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="jdbc.fetch_size"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">50</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">property</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">非零值，指定JDBC抓取数量的大小<BR></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="jdbc.batch_size"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">20</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">property</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">非零值，允许Hibernate使用JDBC2的批量更新<BR></SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">property&nbsp;name="jdbc.use_scrollable_resultset"&gt;false&lt;/property</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000">允许Hibernate使用JDBC2的可滚动结果集.&nbsp;只有在使用用户提供的JDBC连接时，这个选项才是必要的,&nbsp;否则Hibernate会使用连接的元数据<BR><BR><BR></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">mapping&nbsp;</SPAN><SPAN style="COLOR: #ff0000">resource</SPAN><SPAN style="COLOR: #0000ff">="eqzhou/test/Student.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">mapping&nbsp;</SPAN><SPAN style="COLOR: #ff0000">resource</SPAN><SPAN style="COLOR: #0000ff">="eqzhou/test/Huzhao.hbm.xml"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000">映射定义文件<BR></SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">mapping&nbsp;resource="eqzhou/test/User.hbm.xml"/</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000">映射定义文件<BR></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">mapping&nbsp;</SPAN><SPAN style="COLOR: #ff0000">resource</SPAN><SPAN style="COLOR: #0000ff">="eqzhou/test/Dep.hbm.xml"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000">映射定义文件<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</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/yesjoy/aggbug/31750.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yesjoy/" target="_blank">★yesjoy★</a> 2006-02-21 10:33 <a href="http://www.blogjava.net/yesjoy/articles/31750.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>J2EE项目中的数据持久层设计 </title><link>http://www.blogjava.net/yesjoy/articles/30018.html</link><dc:creator>★yesjoy★</dc:creator><author>★yesjoy★</author><pubDate>Thu, 09 Feb 2006 05:15:00 GMT</pubDate><guid>http://www.blogjava.net/yesjoy/articles/30018.html</guid><wfw:comment>http://www.blogjava.net/yesjoy/comments/30018.html</wfw:comment><comments>http://www.blogjava.net/yesjoy/articles/30018.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yesjoy/comments/commentRss/30018.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yesjoy/services/trackbacks/30018.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN-TOP: 20pt; MARGIN-BOTTOM: 15pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 150%; TEXT-ALIGN: center" align=center><SPAN lang=EN-US style="FONT-SIZE: 22pt; FONT-FAMILY: 黑体; mso-prop-change: 韦港/勘测处/水规总院 20050309T1016; mso-bidi-font-size: 18.0pt; mso-hansi-font-family: 宋体; mso-bidi-font-weight: bold">J2EE<SPAN style="mso-prop-change: 韦港/勘测处/水规总院 20050309T1016">项目中的数据持久层设计<?XML:NAMESPACE PREFIX = O /><O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 20pt 0cm; TEXT-INDENT: 21pt; TEXT-ALIGN: center" align=center><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 华文新魏; mso-bidi-font-size: 10.5pt; mso-hansi-font-family: 宋体">刘艳霞 </SPAN><SPAN style="FONT-FAMILY: 楷体_GB2312; mso-bidi-font-size: 10.5pt; mso-hansi-font-family: 宋体">（</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt"><O:P></SPAN><SPAN style="FONT-FAMILY: 楷体_GB2312; mso-bidi-font-size: 10.5pt; mso-hansi-font-family: 宋体">唐山工业学校 唐山<SPAN lang=EN-US> 063000）</SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">数据持久层的设计目标是为整个项目提供一个高层、统一、安全和并发的数据持久机制。完成对各种数据进行持久化的编程工作，并为系统业务逻辑层提供服务。数据持久层提供了数据访问方法，能够使其它程序员避免手工编写程序访问数据持久层(Persistene layer)，使其专注于业务逻辑的开发，并且能够在不同项目中重用映射框架，大大简化了数据增、删、改、查等功能的开发过程，同时又不丧失多层结构的天然优势，继承延续J2EE特有的可伸缩性和可扩展性。<O:P> </O:P></SPAN></P>
<P style="MARGIN-TOP: 5px; MARGIN-BOTTOM: 3px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%" align=justify><FONT lang=ZH-CN face=黑体 size=4>1 数据持久层及ORM映射框架</FONT><SPAN style="FONT-FAMILY: 宋体"><O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">笔者从事的项目中的数据持久层，是基于J2EE体系结构，并采用了Hibernate作为持久映射框架。<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">Hibernate是一种新的ORM映射工具，是JDBC的轻量级的对象封装。Hibernate可以用在JDBC可以使用的任何场合，例如Java应用程序的数据库访问代码，DAO接口的实现类，甚至可以是BMP里面的访问数据库的代码。Hibernate不仅提供了从Java类到数据表之间的映射，也提供了数据查询和恢复机制。相对于使用JDBC和SQL来手工操作数据库，使用Hibernate，可以大大减少操作数据库的工作量。 <O:P></O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">Hibernate是一个和JDBC密切关联的、独立的对象持久层框架，可以搭配各种App Server、Web Server、EJB Container共同使用，Hibernate的兼容性仅同JDBC驱动、底层数据库产品间有一定的关系，但是和使用它的Java程序、App Server没有任何关系，也不存在兼容性问题。而且事实表明Hibernate可以和多种Web服务器或者应用服务器良好集成，如今已经支持几乎所有的流行的数据库服务器（达16种）。<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">在较为常用的数据持久方案中，Hibernate无疑是最优秀的，下面是对各种持久方案的比较。<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">¨<SPAN style="FONT-FAMILY: 宋体"> 流行的数据持久层架构：<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">Business Layer &lt;-&gt; Session Bean &lt;-&gt; Entity Bean &lt;-&gt; DB<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">¨<SPAN style="FONT-FAMILY: 宋体"> 为了解决性能障碍的替代架构：<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">Business Layer &lt;-&gt; DAO &lt;-&gt; JDBC &lt;-&gt; DB<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">¨<SPAN style="FONT-FAMILY: 宋体"> 使用Hibernate来提高上面架构的开发效率的架构：<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">Business Layer &lt;-&gt; DAO &lt;-&gt; Hibernate &lt;-&gt; DB <O:P></O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">我们就上面3个架构来作如下分析。<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体"><SPAN style="FONT-FAMILY: 宋体">(1)内存消耗：采用JDBC的架构无疑是最省内存的，Hibernate的架构次之，EB的架构最差。</SPAN><O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体"><SPAN style="FONT-FAMILY: 宋体">(2)运行效率：如果JDBC的代码写的非常优化，那么JDBC架构运行效率最高，但是实际项目中，这一点几乎做不到，这需要程序员非常精通JDBC，运用Batch语句，调整PreapredStatement的Batch Size和Fetch Size等参数，以及在必要的情况下采用结果集cache等等。而一般情况下程序员是做不到这一点的。因此Hibernate架构表现出最快的运行效率。EB的架构效率会差的很远。</SPAN><O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">(3)开发效率：在有Eclipse、JBuilder等开发工具的支持下，对于简单的项目，EB架构开发效率最高，JDBC次之，Hibernate最差。但是在大的项目，特别是持久层关系映射很复杂的情况下，Hibernate效率高的惊人，JDBC次之，而EB架构很可能会失败。</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><O:P> </O:P></SPAN></P>
<P style="MARGIN-TOP: 5px; MARGIN-BOTTOM: 3px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%" align=justify><FONT lang=ZH-CN face=黑体 size=4>2 数据持久层设计</FONT><SPAN lang=EN-US style="FONT-SIZE: 12pt; FONT-FAMILY: 黑体"><O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">复杂性是应用开发过程中最令人头疼的一个问题。每当在一个应用中增加一个功能时，它的复杂性通常呈几何级的增长。这种复杂性往往导致程序的开发无法再继续下去。这也是现在为什么许多应用只有<SPAN lang=EN-US>Beta版本而没有正式版的原因。<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><O:P></O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">专家将应用开发过程产生的复杂性分为两类，即非本质的（<SPAN lang=EN-US>accidental）和本质的（essential）。本质的复杂性是对于解决目标问题所必然产生的复杂性，非本质的复杂性是由于选择了不适当的开发工具和设计工具而产生的复杂性。对于一个功能确定的程序来讲，本质的复杂性是确定的，而非本质的复杂性则是没有限制的。因此，一个应用的开发要想较顺利地取得成功，就需要尽可能地减少非本质的复杂性。<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">设计模式使人们可以更加简单方便地复用成功的设计和体系结构。将已证实的技术表述成设计模式，也会使新系统开发者更加容易理解其设计思路。<SPAN lang=EN-US><O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">衡量一个系统优秀与否的关键因素，除了能够满足用户需求外还有如下方面：首先是灵活性。灵活性意指这种结构或模式不依赖于任何实际应用，应该与操作系统、应用程序无关。提供独立的结构，可以提供最大的重用。其次是可扩展性。随着业务的扩展，新的业务不断增加，业务逻辑自然增加，系统必然会进行修改或添加相应功能模块。再次是可配置性。最后是安全性。<SPAN lang=EN-US><O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">数据持久层的设计采纳了多种设计模式，最大限度的降低了系统内部各模块、子系统间的耦合性，使得系统相对易于扩展，并且能够在进行改变时，保证持久层的业务逻辑层相对稳定，基本不需要因持久层的调整改变而进行逻辑层的变动。<SPAN lang=EN-US><O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">笔者在项目中采用了如下设计模式。<SPAN lang=EN-US><O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN lang=EN-US style="FONT-FAMILY: 楷体_GB2312; mso-hansi-font-family: 宋体">2.1 整体架构——MVC模式（模型-视图-控制器）<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN lang=EN-US style="FONT-FAMILY: Symbol; mso-hansi-font-family: 宋体; mso-char-type: symbol; mso-symbol-font-family: Symbol; mso-ascii-font-family: 宋体">¨</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"> 模型（Model）：模型包含完成任务所需要的所有的行为和数据。在数据持久层中，模型即为值对象以及数据访问对象。<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN lang=EN-US style="FONT-FAMILY: Symbol; mso-hansi-font-family: 宋体; mso-char-type: symbol; mso-symbol-font-family: Symbol; mso-ascii-font-family: 宋体">¨</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"> 视图（View）：数据持久层中，视图就是持久层同其它层进行数据交换的值对象（Transfer Object）和视图助手对象。<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN lang=EN-US style="FONT-FAMILY: Symbol; mso-hansi-font-family: 宋体; mso-char-type: symbol; mso-symbol-font-family: Symbol; mso-ascii-font-family: 宋体">¨</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"> 控制器（Controller）：持久层所需的控制相对简单，因此集成到了控制代理中。<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">持久层整体采用<SPAN lang=EN-US>MVC模式，使得整个数据持久层的实现部分与项目的业务逻辑部分隔离开来，能够实现对接口作大的修改而不需要对相应的模型进行修改。另外，持久层某子系统发生变化时，不会影响到其它子系统。有利于提高系统的稳定性、可维护性。</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体"><O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN lang=EN-US style="FONT-FAMILY: 楷体_GB2312; mso-hansi-font-family: 宋体">2.2 值对象模式（Value Object Pattern）</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">值对象用来封装业务对象。相应的方法调用是设置（<SPAN lang=EN-US>getter）和检索（setter）值对象。它是任意的可串行化的Java对象，当客户端Bean请求业务数据时，该Bean可以构造值对象，用属性值来填充，并按照值把它传递给客户端。<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">在笔者开发项目的数据持久层体系结构中，值对象主要应用在子系统间传递、交换数据（<SPAN lang=EN-US>Transfer Object）和映射数据表两个方面（Persistent Object）。<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">在各子系统间进行数据传递和数据交换时，使用值对象模式能够最大化地降低系统间数据传递的开销。在这种策略下传递的是对象而不再是一个个的有意义的数据，使得系统在进行扩充、修改时，各子系统间数据传递部分不会受到影响，因为各子系统仅需要关心是否有值对象被传递，而并不去关心传递的到底是什么数据。<SPAN lang=EN-US><O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">在映射数据库表时，值对象类及其子类所构成的树形结构被用来映射一个数据库表，该继承树通过<SPAN lang=EN-US>XML配置文件对应数据库中的单个表，这使得最底层的关系型的数据库表结构能够面向对象模型所隐藏，另外，由于面向对象设计方法中类的可继承性，采用继承树对应一个表的策略使得该映射策略极易扩展，并且能够将一个复杂的数据表转化成若干简单的值对象来表示，提高了系统的可维护性和可修改性。<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN lang=EN-US style="FONT-FAMILY: 楷体_GB2312; mso-hansi-font-family: 宋体">2.3 数据访问对象（DAO）</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">根据数据源不同，数据访问也不同。根据存储的类型<SPAN lang=EN-US>(关系数据库、面向对象数据库等)和供应商不同，持久性存储(比如数据库)的访问差别也很大。当业务组件或表示组件需要访问某数据源时，它们可以使用合适的API来获得连接性，以及操作该数据源。但是在这些组件中包含连接性和数据访问代码会引入这些组件及数据源实现之间的紧密耦合。组件中这类代码依赖性使应用程序从某种数据源迁移到其它种类的数据源将变得非常麻烦和困难，当数据源变化时，组件也需要改变，以便于能够处理新类型的数据源。<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">笔者开发项目的数据持久层使用数据访问对象<SPAN lang=EN-US>(DAO)来抽象和封装所有对数据源的访问。DAO管理着与数据源的连接以便于检索和存储数据，DAO实现了用来操作数据源的访问机制，内部封装了对Hibenernate数据操纵、事务处理、会话管理等API的封装。外界依赖于DAO的业务组件为其客户端使用DAO提供了更简单的接口，DAO完全向客户端隐藏了数据源实现细节。由于当低层数据源实现变化时，DAO向客户端提供的接口不会变化，采用该设计模式允许DAO调整到不同的存储模式，而不会影响其客户端或业务组件，即使将来不再采用Hibernate作为关系映射框架，上层客户端也不会受到任何影响。另外，DAO还充当组件和数据源之间的适配器的角色。<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">数据持久层通过调整抽象工厂<SPAN lang=EN-US>(Abstract Factory)模式和工厂方法(Factory Method) 模式(这二个创建型模式的实现详情参见GoF的&lt;设计模式&gt;)，)，使DAO模式达到了很高的灵活度。<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">当底层存储随着实现的变化而变化时，该策略可以通过使用抽象工厂模式实现。抽象工厂可以基于工厂方法实现而创建，并可使用工厂方法实现。该策略提供一个<SPAN lang=EN-US>DAO的抽象工厂对象，其中该对象可以构造多种类型的具体的DAO工厂，每个工厂支持一种不同类型的持久性存储实现。一旦你获取某特定实现的具体DAO工厂，可以使用它来生成该实现中所支持和实现的DAO。<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN lang=EN-US style="FONT-FAMILY: 楷体_GB2312; mso-hansi-font-family: 宋体">2.4 连接池、应用级缓存及享元模式（提升系统性能）</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN lang=EN-US style="FONT-FAMILY: Symbol; mso-hansi-font-family: 宋体; mso-char-type: symbol; mso-symbol-font-family: Symbol; mso-ascii-font-family: 宋体">¨</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"> 缓存（Cache）<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">对于数据库来说，厂商的做法往往是在内存中开辟相应的区域来存储可能被多次存取的 数据和可能被多次执行的语句，以使这些数据在下次被访问时不必再次提交对<SPAN lang=EN-US>DBMS的请求和那些语句在下次执行时不必再次编译。<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">同样，数据持久层采用缓存技术来保存已经从数据库中检索出来的部分常用数据。客户端访问持久层时，持久层将首先访问缓存，如果能够命中则直接从缓存中提取数据，否则再向数据库发送提取数据的指令。这种设计能够大幅度地提高数据访问速度。<SPAN lang=EN-US><O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN lang=EN-US style="FONT-FAMILY: Symbol; mso-hansi-font-family: 宋体; mso-char-type: symbol; mso-symbol-font-family: Symbol; mso-ascii-font-family: 宋体">¨</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"> 连接池（Connection Pool）<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">池是一个很普遍的概念，和缓冲存储有机制相近的地方，都是缩减了访问的环节，但它更注重于资源的共享。<SPAN lang=EN-US><O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">对于访问数据库来说，建立连接的代价比较昂贵，因此，数据持久层建立了“连接池”以提高访问的性能。数据持久层把连接当作对象，整个系统启动后，连接池首先建立若干连接，访问本来需要与数据库连接的区域，都改为和池相连，池临时分配连接供访问使用，结果返回后，访问将连接交还。这种设计消除了<SPAN lang=EN-US>JDBC与数据源建立连接的延时，同时在应用级提供了对数据源的并发访问。</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体"><O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN lang=EN-US style="FONT-FAMILY: Symbol; mso-hansi-font-family: 宋体; mso-char-type: symbol; mso-symbol-font-family: Symbol; mso-ascii-font-family: 宋体">¨</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"> 享元模式（Flyweight）<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">面向对象语言的原则就是一切都是对象，但是如果真正使用起来，有时对象数可能显得很庞大，比如，数据库中的记录，如果以每条记录作为一个对象，提取几千条记录，对象数就是几千，这无疑相当耗费内存。数据持久层依据享元模式设计了若干元类，封装可以被共享的类。这种设计策略显著降低了系统的内存消耗。<SPAN lang=EN-US><O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN lang=EN-US style="FONT-FAMILY: 楷体_GB2312; mso-hansi-font-family: 宋体">2.5 各种对象的创建模式</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-ascii-font-family: 楷体_GB2312; mso-fareast-font-family: 楷体_GB2312">—</SPAN><SPAN style="FONT-FAMILY: 楷体_GB2312; mso-hansi-font-family: 宋体">工厂方法（<SPAN lang=EN-US>Factory Method）</SPAN></SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><O:P> </O:P></SPAN></P>
<P class=MsoNormal style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt; mso-line-height-rule: exactly" align=justify><SPAN style="FONT-FAMILY: 宋体">工厂方法模式将创建实例的工作与使用实例的工作分开，也就是说，让创建实例所需要的大量初始化工作从简单的构造函数中分离出去。只需要调用一个统一的方法，即可根据需要创建出各种对象的实例，对象的创建方法不再用编码到程序模块中，而是统一编写在工厂类中。这样在系统进行扩充修改时，系统的变化仅存在于工厂类内部，而绝对不会对其他对象造成影响。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体"><O:P> </O:P></SPAN></P>
<P style="MARGIN-TOP: 1px; MARGIN-BOTTOM: 1px; TEXT-INDENT: 24pt; LINE-HEIGHT: 180%" align=justify><SPAN style="FONT-FAMILY: 宋体">（收稿日期：<SPAN lang=EN-US>2004-12-31<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Email：<A href="mailto:chl@tsxd.sina.net"><SPAN style="COLOR: windowtext; FONT-FAMILY: 'Times New Roman'; TEXT-DECORATION: none; text-underline: none">chl@tsxd.sina.net</SPAN></A></SPAN></SPAN><FONT lang=ZH-CN face=黑体 size=4>）</FONT><SPAN class=keg1 lang=EN-US style="FONT-FAMILY: 宋体; mso-ansi-font-size: 10.5pt"></O:P> &nbsp; </SPAN>
<CENTER></CENTER><img src ="http://www.blogjava.net/yesjoy/aggbug/30018.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yesjoy/" target="_blank">★yesjoy★</a> 2006-02-09 13:15 <a href="http://www.blogjava.net/yesjoy/articles/30018.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>