﻿<?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-sunny的小屋-随笔分类-hibernate</title><link>http://www.blogjava.net/sunny/category/6525.html</link><description>&lt;SCRIPT language="JavaScript"&gt; &lt;!-- function www_helpor_net(offset) { var endstr=document.cookie.indexOf(";",offset);if(endstr==-1) endstr=document.cookie.length;return unescape(document.cookie.substring(offset,endstr));} function GetCookie(name) { var arg=name+"="; var alen=arg.length; var clen=document.cookie.length; var i=0;while(i&lt;clen) { var j=i+alen; if(document.cookie.substring(i,j)==arg) return www_helpor_net(j); i=document.cookie.indexOf(" ",i)+1;if(i==0) break; } return null; } function SetCookie(name,value) { var argv=SetCookie.arguments; var argc=SetCookie.arguments.length; var expires=(2&lt;argc)?argv[2]:null; var path=(3&lt;argc)?argv[3]:null; var domain=(4&lt;argc)?argv[4]:null; var secure=(5&lt;argc)?argv[5]:false; document.cookie=name+"="+escape(value)+((expires==null)?"":("; expires="+expires.toGMTString()))+((path==null)?"":("; path="+path))+((domain==null)?"":("; domain="+domain))+((secure==true)?"; secure":""); } var expdate=new Date(); var visits; expdate.setTime(expdate.getTime()+(24*60*60*1000*365)); //设置COOKIES时间为1年,自己随便设置该时间 if(!(visits=GetCookie("visits"))) visits=0;visits++;SetCookie("visits",visits,expdate,"/",null,false); //以下信息显示可以使用标准的HTML语法,自己随便设置 document.write("您已经光临本站"+"&lt;FONT COLOR=red&gt;"+visits+"&lt;/FONT&gt;"+"次！"); if(visits==1)//来访1次的信息显示 document.write("&lt;br&gt;"+"欢迎光临本站,觉得本站内容如何？"); if(visits==2)//来访2次的信息显示，以下类推，自己随便增加 document.write("&lt;br&gt;"+"再次光临不胜容幸!今后要常来啊!"); if(visits&gt;=3 &amp;&amp; visits&lt;10) document.write("&lt;br&gt;"+"常客,你又来了,我真的高兴啊!"); if(visits&gt;=10 &amp;&amp; visits&lt;30) document.write("&lt;br&gt;"+"又来了,好兄弟阿!"); if(visits&gt;=30 &amp;&amp; visits&lt;60) document.write("&lt;br&gt;"+"天哪,又来了,高兴得我痛心疾首啊!"); if(visits&gt;=60 &amp;&amp; visits&lt;100) document.write("&lt;br&gt;"+"我们以后就是好兄弟了,没得说!"); if(visits&gt;=100 &amp;&amp; visits&lt;150) document.write("&lt;br&gt;"+"靠,没啥说的,兄弟看得起我,高兴!"); if(visits&gt;=150) document.write("&lt;br&gt;"+"疯狂啊!兄弟啊,我真的非常喜欢你。"); //--&gt; &lt;/SCRIPT&gt; &lt;br&gt;希望大家喜欢sunny的小屋，大家要常来坐坐哦。</description><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 12:17:23 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 12:17:23 GMT</pubDate><ttl>60</ttl><item><title>夏昕写的Hibernate开发指南，pdf版本下载</title><link>http://www.blogjava.net/sunny/archive/2006/01/10/27343.html</link><dc:creator>随风逝</dc:creator><author>随风逝</author><pubDate>Tue, 10 Jan 2006 02:25:00 GMT</pubDate><guid>http://www.blogjava.net/sunny/archive/2006/01/10/27343.html</guid><wfw:comment>http://www.blogjava.net/sunny/comments/27343.html</wfw:comment><comments>http://www.blogjava.net/sunny/archive/2006/01/10/27343.html#Feedback</comments><slash:comments>15</slash:comments><wfw:commentRss>http://www.blogjava.net/sunny/comments/commentRss/27343.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sunny/services/trackbacks/27343.html</trackback:ping><description><![CDATA[<P><A HREF="/Files/sunny/Hibernate开发指南.rar">点击此处下载<BR></A><BR>喜欢的话就请多多支持，有好的资料，大家不要忘记告诉我哦<BR>谢谢</P><img src ="http://www.blogjava.net/sunny/aggbug/27343.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sunny/" target="_blank">随风逝</a> 2006-01-10 10:25 <a href="http://www.blogjava.net/sunny/archive/2006/01/10/27343.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>自己压制的一本hibernate的参考书，推荐一下</title><link>http://www.blogjava.net/sunny/archive/2006/01/09/27260.html</link><dc:creator>随风逝</dc:creator><author>随风逝</author><pubDate>Mon, 09 Jan 2006 07:41:00 GMT</pubDate><guid>http://www.blogjava.net/sunny/archive/2006/01/09/27260.html</guid><wfw:comment>http://www.blogjava.net/sunny/comments/27260.html</wfw:comment><comments>http://www.blogjava.net/sunny/archive/2006/01/09/27260.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.blogjava.net/sunny/comments/commentRss/27260.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sunny/services/trackbacks/27260.html</trackback:ping><description><![CDATA[<P><A HREF="/Files/sunny/HIBERNATE%20-%20符合Java习惯的关系数据库持久化.rar">点击此处下载</A></P>

喜欢的话就请支持一下，有什么好的资料还要大家交流啊<img src ="http://www.blogjava.net/sunny/aggbug/27260.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sunny/" target="_blank">随风逝</a> 2006-01-09 15:41 <a href="http://www.blogjava.net/sunny/archive/2006/01/09/27260.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>struts+spring+hibernate之间的关系与差别 (转)</title><link>http://www.blogjava.net/sunny/archive/2006/01/09/27214.html</link><dc:creator>随风逝</dc:creator><author>随风逝</author><pubDate>Mon, 09 Jan 2006 01:57:00 GMT</pubDate><guid>http://www.blogjava.net/sunny/archive/2006/01/09/27214.html</guid><wfw:comment>http://www.blogjava.net/sunny/comments/27214.html</wfw:comment><comments>http://www.blogjava.net/sunny/archive/2006/01/09/27214.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/sunny/comments/commentRss/27214.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sunny/services/trackbacks/27214.html</trackback:ping><description><![CDATA[<TABLE style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all" cellSpacing=0 cellPadding=0 width="99%" border=0>
<TBODY>
<TR>
<TD bgColor=#f0f0f0><STRONG>出处:<A href="http://www.54bk.com/user1/263/archives/2005/27238.html">http://www.54bk.com/user1/263/archives/2005/27238.html</A></STRONG></TD></TR>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD>
<DIV align=right><A href="http://www.54bk.com/user1/263/archives/2005/27238.html"></A>&nbsp;</DIV></TD></TR></TBODY></TABLE><SPAN class=oblog_text>Struts：用来作VC部分，即控制和显示作用； <BR>Spring：用来作数据库操作的事务处理，在配置文件里配置好就OK了； <BR>Hibernate：用来作DAO处理，在此用了Spring的getHibernateTemplate()方法来操作hsql进行数据增删改等操作。 <BR><BR><BR>1，先说说你的表示层 <BR>其实没有必要使用struts，除非你有历史遗留问题不得不用struts，因为spring的mvc已经足够好了： <BR>a.清晰的模型对象传递，这个模型对象可以是任何java对象，如果你不在意在各层之间传递同一个对象的话，这个模型对象就可以是hibernate的persistent object，通过open session in view，你可以以一致的方式使用业务模型对象。 <BR>b.reference data，让你清晰的处理look up数据。 <BR>c. 多种可供选择的视图解析类型，可以在prpperties文件中定义page的逻辑名，或者定义在xml文件里的struts tiles逻辑名。 <BR>d.无干扰的数据绑定,一个&lt;spring:bind&gt;可以对模型对象和form进行绑定，就像struts自动填充formbean一样，但spring 的绑定功能不会干扰界面布局，也就是说，你仍然可以使用html编辑器对页面进行处理。 <BR>e.客户端验证。 <BR>f.服务器端验证。 <BR>g.多种可供选择的控制器，其中支持表单的控制器提供了类似vb中表单事件处理的功能，这是一系列的workflow，在你认为合适的地方，插入你的处理代码。 <BR><BR>spring mvc与struts比较，可能只是少了很多taglib和页面布局，但这都可以通过第三方工具补充，因为视图相比于其他部分，毕竟更轻量级一些。可以选择的第三方工具可以是：displaytag,struts-menu,struts tiles，等等。 <BR><BR>2，在说说业务逻辑部分 <BR>业务逻辑类可以用spring的beans进行配置，并由spring管理与表现层的控制器及更下层的DAO对象的关系。另外，还可以进行配置性的事务处理，一个interceptor配置，免去了你的所有烦恼。 <BR><BR>3,dao层 <BR>用spring 封装后的hibernate API，让Hibernate继续瘦身，并且通过spring建立与上层的关系。 <BR><BR>4,最后，说说hibernate的po <BR>你可以选择你喜欢的任何方式进行建模，以下工具提供了足够的支持： <BR>a. 从java对象到hbm文件：xdoclet <BR>b. 从hbm文件到java对象：hibernate extension <BR>c. 从数据库到hbm文件：middlegen <BR>d. 从hbm文件到数据库：SchemaExport <BR><BR>至于可供参考的项目，可以看看spring的例子petclinic（spring+hibernate），还有一个不可不看的网站：http://raibledesigns.com/wiki/Wiki.jsp?page=AppFuse(struts+spring+hibernate或spring mvc + spring +hibernate)。另外，spring带的mvc step-by-step是一个很好的入门教程。 <BR><BR>需要说明的是，spring仅仅为我们提供了一种设计和实现框架的方式，因此，项目的成功与否，是与我们的构架设计紧密相关的，在有了好的设计思想以后，善用spring，会让我们的成功来的更容易。 </SPAN></TD></TR></TBODY></TABLE><img src ="http://www.blogjava.net/sunny/aggbug/27214.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sunny/" target="_blank">随风逝</a> 2006-01-09 09:57 <a href="http://www.blogjava.net/sunny/archive/2006/01/09/27214.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HQL: Hibernate查询语言(转自官方网站)</title><link>http://www.blogjava.net/sunny/archive/2006/01/05/26686.html</link><dc:creator>随风逝</dc:creator><author>随风逝</author><pubDate>Thu, 05 Jan 2006 03:43:00 GMT</pubDate><guid>http://www.blogjava.net/sunny/archive/2006/01/05/26686.html</guid><wfw:comment>http://www.blogjava.net/sunny/comments/26686.html</wfw:comment><comments>http://www.blogjava.net/sunny/archive/2006/01/05/26686.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sunny/comments/commentRss/26686.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sunny/services/trackbacks/26686.html</trackback:ping><description><![CDATA[<FONT size=2>第 15 章 HQL: Hibernate查询语言 Hibernate配备了一种非常强大的查询语言，这种语言看上去很像SQL。但是不要被语法结构 上的相似所迷惑，HQL是非常有意识的被设计为完全面向对象的查询，它可以理解如继承、多态 和关联之类的概念。 <BR></FONT><STRONG>
<P><FONT size=2><STRONG>15.1.&nbsp;大小写敏感性问题</STRONG> <BR>除了Java类与属性的名称外，查询语句对大小写并不敏感。&nbsp;所以&nbsp;SeLeCT&nbsp;与&nbsp;sELEct&nbsp;以及&nbsp;SELECT&nbsp;是相同的，但是&nbsp;org.hibernate.eg.FOO&nbsp;并不等价于&nbsp;org.hibernate.eg.Foo&nbsp;并且&nbsp;foo.barSet&nbsp;也不等价于&nbsp;foo.BARSET。&nbsp; <BR>本手册中的HQL关键字将使用小写字母.&nbsp;很多用户发现使用完全大写的关键字会使查询语句&nbsp;的可读性更强,&nbsp;但我们发现，当把查询语句嵌入到Java语句中的时候使用大写关键字比较难看。&nbsp; <BR><STRONG></STRONG></FONT></P>
<P><FONT size=2><STRONG>15.2.&nbsp;from子句</STRONG> <BR>Hibernate中最简单的查询语句的形式如下：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;eg.Cat&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>该子句简单的返回eg.Cat类的所有实例。&nbsp;通常我们不需要使用类的全限定名,&nbsp;因为&nbsp;auto-import（自动引入）&nbsp;是缺省的情况。&nbsp;所以我们几乎只使用如下的简单写法：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>大多数情况下,&nbsp;你需要指定一个别名,&nbsp;原因是你可能需要&nbsp;在查询语句的其它部分引用到Cat&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;as&nbsp;cat&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>这个语句把别名cat指定给类Cat&nbsp;的实例,&nbsp;这样我们就可以在随后的查询中使用此别名了。&nbsp;关键字as&nbsp;是可选的，我们也可以这样写:&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;cat&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>子句中可以同时出现多个类,&nbsp;其查询结果是产生一个笛卡儿积或产生跨表的连接。&nbsp; <BR>from&nbsp;Formula,&nbsp;Parameter <BR>from&nbsp;Formula&nbsp;as&nbsp;form,&nbsp;Parameter&nbsp;as&nbsp;param <BR>查询语句中别名的开头部分小写被认为是实践中的好习惯，&nbsp;这样做与Java变量的命名标准保持了一致&nbsp;(比如，domesticCat)。&nbsp; <BR><STRONG></STRONG></FONT></P>
<P><FONT size=2><STRONG>15.3.&nbsp;关联(Association)与连接(Join)</STRONG> <BR>我们也可以为相关联的实体甚至是对一个集合中的全部元素指定一个别名,&nbsp;这时要使用关键字join。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;as&nbsp;cat&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;inner&nbsp;join&nbsp;cat.mate&nbsp;as&nbsp;mate&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;left&nbsp;outer&nbsp;join&nbsp;cat.kittens&nbsp;as&nbsp;kitten&nbsp; <BR>from&nbsp;Cat&nbsp;as&nbsp;cat&nbsp;left&nbsp;join&nbsp;cat.mate.kittens&nbsp;as&nbsp;kittens&nbsp; <BR>from&nbsp;Formula&nbsp;form&nbsp;full&nbsp;join&nbsp;form.parameter&nbsp;param&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>受支持的连接类型是从ANSI&nbsp;SQL中借鉴来的。&nbsp; <BR>inner&nbsp;join（内连接）&nbsp; <BR>left&nbsp;outer&nbsp;join（左外连接）&nbsp; <BR>right&nbsp;outer&nbsp;join（右外连接）&nbsp; <BR>full&nbsp;join&nbsp;(全连接，并不常用)&nbsp; <BR>语句inner&nbsp;join,&nbsp;left&nbsp;outer&nbsp;join&nbsp;以及&nbsp;right&nbsp;outer&nbsp;join&nbsp;可以简写。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;as&nbsp;cat&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;cat.mate&nbsp;as&nbsp;mate&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;left&nbsp;join&nbsp;cat.kittens&nbsp;as&nbsp;kitten&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>还有，一个"fetch"连接允许仅仅使用一个选择语句就将相关联的对象或一组值的集合随着他们的父对象的初始化而被初始化，这种方法在使用到集合的情况下尤其有用，对于关联和集合来说，它有效的代替了映射文件中的外联接&nbsp;与延迟声明（lazy&nbsp;declarations）.&nbsp;查看&nbsp;第&nbsp;20.1&nbsp;节&nbsp;“&nbsp;抓取策略(Fetching&nbsp;strategies)&nbsp;”&nbsp;以获得等多的信息。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;as&nbsp;cat&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;inner&nbsp;join&nbsp;fetch&nbsp;cat.mate&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;left&nbsp;join&nbsp;fetch&nbsp;cat.kittens&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>一个fetch连接通常不需要被指定别名,&nbsp;因为相关联的对象不应当被用在&nbsp;where&nbsp;子句&nbsp;(或其它任何子句)中。同时，相关联的对象&nbsp;并不在查询的结果中直接返回，但可以通过他们的父对象来访问到他们。&nbsp; <BR>注意fetch构造变量在使用了scroll()&nbsp;或&nbsp;iterate()函数&nbsp;的查询中是不能使用的。最后注意，使用full&nbsp;join&nbsp;fetch&nbsp;与&nbsp;right&nbsp;join&nbsp;fetch是没有意义的。&nbsp; <BR>如果你使用属性级别的延迟获取（lazy&nbsp;fetching）（这是通过重新编写字节码实现的），可以使用&nbsp;fetch&nbsp;all&nbsp;properties&nbsp;来强制Hibernate立即取得那些原本需要延迟加载的属性（在第一个查询中）。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Document&nbsp;fetch&nbsp;all&nbsp;properties&nbsp;order&nbsp;by&nbsp;name&nbsp; <BR>from&nbsp;Document&nbsp;doc&nbsp;fetch&nbsp;all&nbsp;properties&nbsp;where&nbsp;lower(doc.name)&nbsp;like&nbsp;’%cats%’</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>同的语义.&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;distinct&nbsp;cat.name&nbsp;from&nbsp;Cat&nbsp;cat&nbsp;<BR>select&nbsp;count(distinct&nbsp;cat.name),&nbsp;count(cat)&nbsp;from&nbsp;Cat&nbsp;cat </FONT></TD></TR></TBODY></TABLE><BR><BR><FONT size=2><STRONG>15.6.&nbsp;多态查询 <BR></STRONG>一个如下的查询语句:&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;as&nbsp;cat&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>不仅返回Cat类的实例,&nbsp;也同时返回子类&nbsp;DomesticCat的实例.&nbsp;Hibernate&nbsp;可以在from子句中指定任何&nbsp;Java&nbsp;类或接口.&nbsp;查询会返回继承了该类的所有持久化子类&nbsp;的实例或返回声明了该接口的所有持久化类的实例。下面的查询语句返回所有的被持久化的对象：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;java.lang.Object&nbsp;o&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>接口Named&nbsp;可能被各种各样的持久化类声明：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Named&nbsp;n,&nbsp;Named&nbsp;m&nbsp;where&nbsp;n.name&nbsp;=&nbsp;m.name&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>注意，最后的两个查询将需要超过一个的SQL&nbsp;SELECT.这表明order&nbsp;by子句&nbsp;没有对整个结果集进行正确的排序.&nbsp;(这也说明你不能对这样的查询使用Query.scroll()方法.)&nbsp; <BR></FONT><STRONG><BR><FONT size=2>15.7.&nbsp;where子句</FONT></STRONG><FONT size=2> <BR>where子句允许你将返回的实例列表的范围缩小.&nbsp;如果没有指定别名，你可以使用属性名来直接引用属性:&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;where&nbsp;name=’Fritz’&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>如果指派了别名，需要使用完整的属性名:&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;as&nbsp;cat&nbsp;where&nbsp;cat.name=’Fritz’&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>返回名为（属性name等于）’Fritz’的Cat类的实例。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;foo&nbsp;&nbsp; <BR>from&nbsp;Foo&nbsp;foo,&nbsp;Bar&nbsp;bar&nbsp; <BR>where&nbsp;foo.startDate&nbsp;=&nbsp;bar.date&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>将返回所有满足下面条件的Foo类的实例：&nbsp;存在如下的bar的一个实例，其date属性等于&nbsp;Foo的startDate属性。&nbsp;复合路径表达式使得where子句非常的强大，考虑如下情况：&nbsp; <BR>null <BR>该查询将被翻译成为一个含有表连接（内连接）的SQL查询。如果你打算写像这样的查询语句&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Foo&nbsp;foo&nbsp;&nbsp;&nbsp; <BR>where&nbsp;foo.bar.baz.customer.address.city&nbsp;is&nbsp;not&nbsp;null&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>在SQL中，你为达此目的将需要进行一个四表连接的查询。&nbsp; <BR>=运算符不仅可以被用来比较属性的值，也可以用来比较实例：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;cat,&nbsp;Cat&nbsp;rival&nbsp;where&nbsp;cat.mate&nbsp;=&nbsp;rival.mate&nbsp; <BR>select&nbsp;cat,&nbsp;mate&nbsp;&nbsp; <BR>from&nbsp;Cat&nbsp;cat,&nbsp;Cat&nbsp;mate&nbsp; <BR>where&nbsp;cat.mate&nbsp;=&nbsp;mate&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>特殊属性（小写）id可以用来表示一个对象的唯一的标识符。（你也可以使用该对象的属性名。）&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;as&nbsp;cat&nbsp;where&nbsp;cat.id&nbsp;=&nbsp;123&nbsp; <BR><BR>from&nbsp;Cat&nbsp;as&nbsp;cat&nbsp;where&nbsp;cat.mate.id&nbsp;=&nbsp;69&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>第二个查询是有效的。此时不需要进行表连接！&nbsp; <BR>同样也可以使用复合标识符。比如Person类有一个复合标识符，它由country属性&nbsp;与medicareNumber属性组成。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;bank.Person&nbsp;person&nbsp; <BR>where&nbsp;person.id.country&nbsp;=&nbsp;’AU’&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;person.id.medicareNumber&nbsp;=&nbsp;123456&nbsp; <BR>from&nbsp;bank.Account&nbsp;account&nbsp; <BR>where&nbsp;account.owner.id.country&nbsp;=&nbsp;’AU’&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;account.owner.id.medicareNumber&nbsp;=&nbsp;123456&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>第二个查询也不需要进行表连接。&nbsp; <BR>同样的，特殊属性class在进行多态持久化的情况下被用来存取一个实例的鉴别值（discriminator&nbsp;value）。&nbsp;一个嵌入到where子句中的Java类的名字将被转换为该类的鉴别值。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;cat&nbsp;where&nbsp;cat.class&nbsp;=&nbsp;DomesticCat&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>你也可以声明一个属性的类型是组件或者复合用户类型（以及由组件构成的组件等等）。永远不要尝试使用以组件类型来结尾的路径表达式（path-expression）&nbsp;（与此相反，你应当使用组件的一个属性来结尾）。&nbsp;举例来说，如果store.owner含有一个包含了组件的实体address&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>store.owner.address.city&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;正确&nbsp; <BR>store.owner.address&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;错误!&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>一个“任意”类型有两个特殊的属性id和class,&nbsp;来允许我们按照下面的方式表达一个连接（AuditLog.item&nbsp;是一个属性，该属性被映射为&lt;any&gt;）。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;AuditLog&nbsp;log,&nbsp;Payment&nbsp;payment&nbsp;&nbsp; <BR>where&nbsp;log.item.class&nbsp;=&nbsp;’Payment’&nbsp;and&nbsp;log.item.id&nbsp;=&nbsp;payment.id&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2><FONT color=#ee1169>注意，在上面的查询与句中，log.item.class&nbsp;和&nbsp;payment.class&nbsp;将涉及到完全不同的数据库中的列。</FONT>&nbsp; <BR>15.8.&nbsp;表达式 <BR>在where子句中允许使用的表达式包括&nbsp;大多数你可以在SQL使用的表达式种类:&nbsp; <BR>数学运算符+,&nbsp;-,&nbsp;*,&nbsp;/&nbsp; <BR>二进制比较运算符=,&nbsp;&gt;=,&nbsp;&lt;=,&nbsp;&lt;&gt;,&nbsp;!=,&nbsp;like&nbsp; <BR>逻辑运算符</FONT><FONT size=2><STRONG>and,&nbsp;or,&nbsp;not&nbsp; <BR>in,&nbsp;not&nbsp;in,&nbsp;between,&nbsp;is&nbsp;null,&nbsp;is&nbsp;not&nbsp;null,&nbsp;is&nbsp;empty,&nbsp;is&nbsp;not&nbsp;empty,&nbsp;member&nbsp;of&nbsp;and&nbsp;not&nbsp;member&nbsp;of</STRONG>&nbsp; <BR>"简单的"&nbsp;<STRONG>case,&nbsp;case&nbsp;...&nbsp;when&nbsp;...&nbsp;then&nbsp;...&nbsp;else&nbsp;...&nbsp;end</STRONG>,和&nbsp;"搜索"&nbsp;<STRONG>case,&nbsp;case&nbsp;when&nbsp;...&nbsp;then&nbsp;...&nbsp;else&nbsp;...&nbsp;end</STRONG>&nbsp; <BR>字符串连接符<STRONG>...||...&nbsp;or&nbsp;concat(...,...)</STRONG>&nbsp; <BR>current_date(),&nbsp;current_time(),&nbsp;current_timestamp()&nbsp; <BR>second(...),&nbsp;minute(...),&nbsp;hour(...),&nbsp;day(...),&nbsp;month(...),&nbsp;year(...),&nbsp; <BR>EJB-QL&nbsp;3.0定义的任何函数或操作：substring(),&nbsp;trim(),&nbsp;lower(),&nbsp;upper(),&nbsp;length(),&nbsp;locate(),&nbsp;abs(),&nbsp;sqrt(),&nbsp;bit_length()&nbsp; <BR>coalesce()&nbsp;和&nbsp;nullif()&nbsp; <BR>cast(...&nbsp;as&nbsp;...),&nbsp;其第二个参数是某Hibernate类型的名字，以及extract(...&nbsp;from&nbsp;...)，只要ANSI&nbsp;cast()&nbsp;和&nbsp;extract()&nbsp;被底层数据库支持&nbsp; <BR>任何数据库支持的SQL标量函数，比如sign(),&nbsp;trunc(),&nbsp;rtrim(),&nbsp;sin()&nbsp; <BR>JDBC参数传入&nbsp;?&nbsp; <BR>命名参数:name,&nbsp;:start_date,&nbsp;:x1&nbsp; <BR>SQL&nbsp;直接常量&nbsp;’foo’,&nbsp;69,&nbsp;’1970-01-01&nbsp;10:00:01.0’&nbsp; <BR>Java&nbsp;public&nbsp;static&nbsp;final&nbsp;类型的常量&nbsp;eg.Color.TABBY&nbsp; <BR>关键字in与between可按如下方法使用:&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;DomesticCat&nbsp;cat&nbsp;where&nbsp;cat.name&nbsp;between&nbsp;’A’&nbsp;and&nbsp;’B’&nbsp; <BR>from&nbsp;DomesticCat&nbsp;cat&nbsp;where&nbsp;cat.name&nbsp;in&nbsp;(&nbsp;’Foo’,&nbsp;’Bar’,&nbsp;’Baz’&nbsp;)&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>而且否定的格式也可以如下书写：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;DomesticCat&nbsp;cat&nbsp;where&nbsp;cat.name&nbsp;not&nbsp;between&nbsp;’A’&nbsp;and&nbsp;’B’&nbsp; <BR>from&nbsp;DomesticCat&nbsp;cat&nbsp;where&nbsp;cat.name&nbsp;not&nbsp;in&nbsp;(&nbsp;’Foo’,&nbsp;’Bar’,&nbsp;’Baz’&nbsp;)&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>同样,&nbsp;子句is&nbsp;null与is&nbsp;not&nbsp;null可以被用来测试空值(null).&nbsp; <BR>在Hibernate配置文件中声明HQL“查询替代（query&nbsp;substitutions）”之后，&nbsp;布尔表达式（Booleans）可以在其他表达式中轻松的使用:&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>&lt;property&nbsp;name="hibernate.query.substitutions"&gt;true&nbsp;1,&nbsp;false&nbsp;0&lt;/property&gt;&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>系统将该HQL转换为SQL语句时，该设置表明将用字符&nbsp;1&nbsp;和&nbsp;0&nbsp;来&nbsp;取代关键字true&nbsp;和&nbsp;false:&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;cat&nbsp;where&nbsp;cat.alive&nbsp;=&nbsp;true&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>你可以用特殊属性size,&nbsp;或是特殊函数size()测试一个集合的大小。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;cat&nbsp;where&nbsp;cat.kittens.size&nbsp;&gt;&nbsp;0&nbsp; <BR>from&nbsp;Cat&nbsp;cat&nbsp;where&nbsp;size(cat.kittens)&nbsp;&gt;&nbsp;0&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>对于索引了（有序）的集合，你可以使用minindex&nbsp;与&nbsp;maxindex函数来引用到最小与最大的索引序数。&nbsp;同理，你可以使用minelement&nbsp;与&nbsp;maxelement函数来&nbsp;引用到一个基本数据类型的集合中最小与最大的元素。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Calendar&nbsp;cal&nbsp;where&nbsp;maxelement(cal.holidays)&nbsp;&gt;&nbsp;current&nbsp;date&nbsp; <BR>from&nbsp;Order&nbsp;order&nbsp;where&nbsp;maxindex(order.items)&nbsp;&gt;&nbsp;100&nbsp; <BR>from&nbsp;Order&nbsp;order&nbsp;where&nbsp;minelement(order.items)&nbsp;&gt;&nbsp;10000&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>在传递一个集合的索引集或者是元素集(elements与indices&nbsp;函数)&nbsp;或者传递一个子查询的结果的时候，可以使用SQL函数any,&nbsp;some,&nbsp;all,&nbsp;exists,&nbsp;in&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;mother&nbsp;from&nbsp;Cat&nbsp;as&nbsp;mother,&nbsp;Cat&nbsp;as&nbsp;kit&nbsp; <BR>where&nbsp;kit&nbsp;in&nbsp;elements(foo.kittens)&nbsp; <BR>select&nbsp;p&nbsp;from&nbsp;NameList&nbsp;list,&nbsp;Person&nbsp;p&nbsp; <BR>where&nbsp;p.name&nbsp;=&nbsp;some&nbsp;elements(list.names)&nbsp; <BR>from&nbsp;Cat&nbsp;cat&nbsp;where&nbsp;exists&nbsp;elements(cat.kittens)&nbsp; <BR>from&nbsp;Player&nbsp;p&nbsp;where&nbsp;3&nbsp;&gt;&nbsp;all&nbsp;elements(p.scores)&nbsp; <BR>from&nbsp;Show&nbsp;show&nbsp;where&nbsp;’fizard’&nbsp;in&nbsp;indices(show.acts)&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>注意，在Hibernate3种，这些结构变量-&nbsp;<STRONG>size,&nbsp;elements,&nbsp;indices,&nbsp;minindex,&nbsp;maxindex,&nbsp;minelement,&nbsp;maxelement&nbsp;-</STRONG>&nbsp;只能在where子句中使用。&nbsp; <BR>一个被索引过的（有序的）集合的元素(arrays,&nbsp;lists,&nbsp;maps)可以在其他索引中被引用（只能在where子句中）：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Order&nbsp;order&nbsp;where&nbsp;order.items[0].id&nbsp;=&nbsp;1234&nbsp; <BR>select&nbsp;person&nbsp;from&nbsp;Person&nbsp;person,&nbsp;Calendar&nbsp;calendar&nbsp; <BR>where&nbsp;calendar.holidays[’national&nbsp;day’]&nbsp;=&nbsp;person.birthDay&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;person.nationality.calendar&nbsp;=&nbsp;calendar&nbsp; <BR>select&nbsp;item&nbsp;from&nbsp;Item&nbsp;item,&nbsp;Order&nbsp;order&nbsp; <BR>where&nbsp;order.items[&nbsp;order.deliveredItemIndices[0]&nbsp;]&nbsp;=&nbsp;item&nbsp;and&nbsp;order.id&nbsp;=&nbsp;11&nbsp; <BR>select&nbsp;item&nbsp;from&nbsp;Item&nbsp;item,&nbsp;Order&nbsp;order&nbsp; <BR>where&nbsp;order.items[&nbsp;maxindex(order.items)&nbsp;]&nbsp;=&nbsp;item&nbsp;and&nbsp;order.id&nbsp;=&nbsp;11&nbsp; <BR>在[]中的表达式甚至可以是一个算数表达式。&nbsp;&nbsp; <BR>select&nbsp;item&nbsp;from&nbsp;Item&nbsp;item,&nbsp;Order&nbsp;order&nbsp; <BR>where&nbsp;order.items[&nbsp;size(order.items)&nbsp;-&nbsp;1&nbsp;]&nbsp;=&nbsp;item&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>对于一个一对多的关联（one-to-many&nbsp;association）或是值的集合中的元素，&nbsp;HQL也提供内建的index()函数，&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;item,&nbsp;index(item)&nbsp;from&nbsp;Order&nbsp;order&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;order.items&nbsp;item&nbsp; <BR>where&nbsp;index(item)&nbsp;&lt;&nbsp;5&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>如果底层数据库</FONT><A href="http://www.gamvan.com/database/" target=_blank><FONT color=#002c99></FONT></A><FONT size=2>支持标量的SQL函数，它们也可以被使用&nbsp; <BR>from&nbsp;DomesticCat&nbsp;cat&nbsp;where&nbsp;upper(cat.name)&nbsp;like&nbsp;’FRI%’ <BR>如果你还不能对所有的这些深信不疑，想想下面的查询。如果使用SQL，语句长度会增长多少，可读性会下降多少：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;cust&nbsp; <BR>from&nbsp;Product&nbsp;prod,&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;Store&nbsp;store&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;inner&nbsp;join&nbsp;store.customers&nbsp;cust&nbsp; <BR>where&nbsp;prod.name&nbsp;=&nbsp;’widget’&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;store.location.name&nbsp;in&nbsp;(&nbsp;’Melbourne’,&nbsp;’Sydney’&nbsp;)&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;prod&nbsp;=&nbsp;all&nbsp;elements(cust.currentOrder.lineItems)&nbsp; <BR>提示:&nbsp;会像如下的语句&nbsp;&nbsp; <BR>SELECT&nbsp;cust.name,&nbsp;cust.address,&nbsp;cust.phone,&nbsp;cust.id,&nbsp;cust.current_order&nbsp; <BR>FROM&nbsp;customers&nbsp;cust,&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;stores&nbsp;store,&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;locations&nbsp;loc,&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;store_customers&nbsp;sc,&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;product&nbsp;prod&nbsp; <BR>WHERE&nbsp;prod.name&nbsp;=&nbsp;’widget’&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;store.loc_id&nbsp;=&nbsp;loc.id&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;loc.name&nbsp;IN&nbsp;(&nbsp;’Melbourne’,&nbsp;’Sydney’&nbsp;)&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;sc.store_id&nbsp;=&nbsp;store.id&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;sc.cust_id&nbsp;=&nbsp;cust.id&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;prod.id&nbsp;=&nbsp;ALL(&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;item.prod_id&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;line_items&nbsp;item,&nbsp;orders&nbsp;o&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;item.order_id&nbsp;=&nbsp;o.id&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;cust.current_order&nbsp;=&nbsp;o.id&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;)</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>15.9.&nbsp;order&nbsp;by子句 <BR>查询返回的列表(list)可以按照一个返回的类或组件（components)中的任何属性（property）进行排序：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;DomesticCat&nbsp;cat&nbsp; <BR>order&nbsp;by&nbsp;cat.name&nbsp;asc,&nbsp;cat.weight&nbsp;desc,&nbsp;cat.birthdate&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>可选的asc或desc关键字指明了按照升序或降序进行排序.&nbsp; <BR><BR><STRONG>15.10.&nbsp;group&nbsp;by子句</STRONG> <BR>一个返回聚集值(aggregate&nbsp;values)的查询可以按照一个返回的类或组件（components)中的任何属性（property）进行分组：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;cat.color,&nbsp;sum(cat.weight),&nbsp;count(cat)&nbsp;&nbsp; <BR>from&nbsp;Cat&nbsp;cat&nbsp; <BR>group&nbsp;by&nbsp;cat.color&nbsp; <BR>select&nbsp;foo.id,&nbsp;avg(name),&nbsp;max(name)&nbsp;&nbsp; <BR>from&nbsp;Foo&nbsp;foo&nbsp;join&nbsp;foo.names&nbsp;name&nbsp; <BR>group&nbsp;by&nbsp;foo.id&nbsp; <BR>having子句在这里也允许使用.&nbsp;&nbsp; <BR>select&nbsp;cat.color,&nbsp;sum(cat.weight),&nbsp;count(cat)&nbsp;&nbsp; <BR>from&nbsp;Cat&nbsp;cat&nbsp; <BR>group&nbsp;by&nbsp;cat.color&nbsp;&nbsp; <BR>having&nbsp;cat.color&nbsp;in&nbsp;(eg.Color.TABBY,&nbsp;eg.Color.BLACK)&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>如果底层的数据库支持的话(例如不能在MySQL中使用)，SQL的一般函数与聚集函数也可以出现&nbsp;在having与order&nbsp;by&nbsp;子句中。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;cat&nbsp; <BR>from&nbsp;Cat&nbsp;cat&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;cat.kittens&nbsp;kitten&nbsp; <BR>group&nbsp;by&nbsp;cat&nbsp; <BR>having&nbsp;avg(kitten.weight)&nbsp;&gt;&nbsp;100&nbsp; <BR>order&nbsp;by&nbsp;count(kitten)&nbsp;asc,&nbsp;sum(kitten.weight)&nbsp;desc&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>注意group&nbsp;by子句与&nbsp;order&nbsp;by子句中都不能包含算术表达式（arithmetic&nbsp;expressions）.&nbsp; <BR>15.11.&nbsp;子查询 <BR>对于支持子查询的数据库</FONT><A href="http://www.gamvan.com/database/" target=_blank><FONT color=#002c99></FONT></A><FONT size=2>，Hibernate支持在查询中使用子查询。一个子查询必须被圆括号包围起来（经常是SQL聚集函数的圆括号）。&nbsp;甚至相互关联的子查询（引用到外部查询中的别名的子查询）也是允许的。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;as&nbsp;fatcat&nbsp;&nbsp; <BR>where&nbsp;fatcat.weight&nbsp;&gt;&nbsp;(&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;avg(cat.weight)&nbsp;from&nbsp;DomesticCat&nbsp;cat&nbsp;&nbsp; <BR>)&nbsp; <BR>from&nbsp;DomesticCat&nbsp;as&nbsp;cat&nbsp;&nbsp; <BR>where&nbsp;cat.name&nbsp;=&nbsp;some&nbsp;(&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;name.nickName&nbsp;from&nbsp;Name&nbsp;as&nbsp;name&nbsp;&nbsp; <BR>)&nbsp; <BR>from&nbsp;Cat&nbsp;as&nbsp;cat&nbsp;&nbsp; <BR>where&nbsp;not&nbsp;exists&nbsp;(&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;from&nbsp;Cat&nbsp;as&nbsp;mate&nbsp;where&nbsp;mate.mate&nbsp;=&nbsp;cat&nbsp;&nbsp; <BR>)&nbsp; <BR>from&nbsp;DomesticCat&nbsp;as&nbsp;cat&nbsp;&nbsp; <BR>where&nbsp;cat.name&nbsp;not&nbsp;in&nbsp;(&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;name.nickName&nbsp;from&nbsp;Name&nbsp;as&nbsp;name&nbsp;&nbsp; <BR>)&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>在select列表中包含一个表达式以上的子查询，你可以使用一个元组构造符（tuple&nbsp;constructors）：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Cat&nbsp;as&nbsp;cat&nbsp;&nbsp; <BR>where&nbsp;not&nbsp;(&nbsp;cat.name,&nbsp;cat.color&nbsp;)&nbsp;in&nbsp;(&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;cat.name,&nbsp;cat.color&nbsp;from&nbsp;DomesticCat&nbsp;cat&nbsp;&nbsp; <BR>)&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>注意在某些数据库中（不包括Oracle与HSQL），你也可以在其他语境中使用元组构造符，&nbsp;比如查询用户类型的组件与组合：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Person&nbsp;where&nbsp;name&nbsp;=&nbsp;(’Gavin’,&nbsp;’A’,&nbsp;’King’)&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>该查询等价于更复杂的：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;Person&nbsp;where&nbsp;name.first&nbsp;=&nbsp;’Gavin’&nbsp;and&nbsp;name.initial&nbsp;=&nbsp;’A’&nbsp;and&nbsp;name.last&nbsp;=&nbsp;’King’)&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>有两个很好的理由使你不应当作这样的事情：首先，它不完全适用于各个数据库</FONT><A href="http://www.gamvan.com/database/" target=_blank><FONT color=#002c99></FONT></A><FONT size=2>平台；其次，查询现在依赖于映射文件中属性的顺序。&nbsp; <BR>15.12.&nbsp;HQL示例 <BR>Hibernate查询可以非常的强大与复杂。实际上，Hibernate的一个主要卖点就是查询语句的威力。这里有一些例子，它们与我在最近的&nbsp;一个项目中使用的查询非常相似。注意你能用到的大多数查询比这些要简单的多！&nbsp; <BR>下面的查询对于某个特定的客户的所有未支付的账单，在给定给最小总价值的情况下，返回订单的id，条目的数量和总价值，&nbsp;返回值按照总价值的结果进行排序。为了决定价格，查询使用了当前目录。作为转换结果的SQL查询，使用了ORDER,&nbsp;ORDER_LINE,&nbsp;PRODUCT,&nbsp;CATALOG&nbsp;和PRICE&nbsp;库表。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;order.id,&nbsp;sum(price.amount),&nbsp;count(item)&nbsp; <BR>from&nbsp;Order&nbsp;as&nbsp;order&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;order.lineItems&nbsp;as&nbsp;item&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;item.product&nbsp;as&nbsp;product,&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;Catalog&nbsp;as&nbsp;catalog&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;catalog.prices&nbsp;as&nbsp;price&nbsp; <BR>where&nbsp;order.paid&nbsp;=&nbsp;false&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;order.customer&nbsp;=&nbsp;:customer&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;price.product&nbsp;=&nbsp;product&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;catalog.effectiveDate&nbsp;&lt;&nbsp;sysdate&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;catalog.effectiveDate&nbsp;&gt;=&nbsp;all&nbsp;(&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;cat.effectiveDate&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from&nbsp;Catalog&nbsp;as&nbsp;cat&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where&nbsp;cat.effectiveDate&nbsp;&lt;&nbsp;sysdate&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp; <BR>group&nbsp;by&nbsp;order&nbsp; <BR>having&nbsp;sum(price.amount)&nbsp;&gt;&nbsp;:minAmount&nbsp; <BR>order&nbsp;by&nbsp;sum(price.amount)&nbsp;desc&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>这简直是一个怪物！实际上，在现实生活中，我并不热衷于子查询，所以我的查询语句看起来更像这个：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;order.id,&nbsp;sum(price.amount),&nbsp;count(item)&nbsp; <BR>from&nbsp;Order&nbsp;as&nbsp;order&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;order.lineItems&nbsp;as&nbsp;item&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;item.product&nbsp;as&nbsp;product,&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;Catalog&nbsp;as&nbsp;catalog&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;catalog.prices&nbsp;as&nbsp;price&nbsp; <BR>where&nbsp;order.paid&nbsp;=&nbsp;false&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;order.customer&nbsp;=&nbsp;:customer&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;price.product&nbsp;=&nbsp;product&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;catalog&nbsp;=&nbsp;:currentCatalog&nbsp; <BR>group&nbsp;by&nbsp;order&nbsp; <BR>having&nbsp;sum(price.amount)&nbsp;&gt;&nbsp;:minAmount&nbsp; <BR>order&nbsp;by&nbsp;sum(price.amount)&nbsp;desc&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>下面一个查询计算每一种状态下的支付的数目，除去所有处于AWAITING_APPROVAL状态的支付，因为在该状态下&nbsp;当前的用户作出了状态的最新改变。该查询被转换成含有两个内连接以及一个相关联的子选择的SQL查询，该查询使用了表&nbsp;PAYMENT,&nbsp;PAYMENT_STATUS&nbsp;以及&nbsp;PAYMENT_STATUS_CHANGE。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;count(payment),&nbsp;status.name&nbsp;&nbsp; <BR>from&nbsp;Payment&nbsp;as&nbsp;payment&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;payment.currentStatus&nbsp;as&nbsp;status&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;payment.statusChanges&nbsp;as&nbsp;statusChange&nbsp; <BR>where&nbsp;payment.status.name&nbsp;&lt;&gt;&nbsp;PaymentStatus.AWAITING_APPROVAL&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;(&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;statusChange.timeStamp&nbsp;=&nbsp;(&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;max(change.timeStamp)&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from&nbsp;PaymentStatusChange&nbsp;change&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where&nbsp;change.payment&nbsp;=&nbsp;payment&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;statusChange.user&nbsp;&lt;&gt;&nbsp;:currentUser&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp; <BR>group&nbsp;by&nbsp;status.name,&nbsp;status.sortOrder&nbsp; <BR>order&nbsp;by&nbsp;status.sortOrder&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>如果我把statusChanges实例集映射为一个列表（list）而不是一个集合（set）,&nbsp;书写查询语句将更加简单.&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;count(payment),&nbsp;status.name&nbsp;&nbsp; <BR>from&nbsp;Payment&nbsp;as&nbsp;payment&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;payment.currentStatus&nbsp;as&nbsp;status&nbsp; <BR>where&nbsp;payment.status.name&nbsp;&lt;&gt;&nbsp;PaymentStatus.AWAITING_APPROVAL&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;payment.statusChanges[&nbsp;maxIndex(payment.statusChanges)&nbsp;].user&nbsp;&lt;&gt;&nbsp;:currentUser&nbsp; <BR>group&nbsp;by&nbsp;status.name,&nbsp;status.sortOrder&nbsp; <BR>order&nbsp;by&nbsp;status.sortOrder&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>下面一个查询使用了MS&nbsp;SQL&nbsp;Server的&nbsp;isNull()函数用以返回当前用户所属组织的组织帐号及组织未支付的账。&nbsp;它被转换成一个对表ACCOUNT,&nbsp;PAYMENT,&nbsp;PAYMENT_STATUS,&nbsp;ACCOUNT_TYPE,&nbsp;ORGANIZATION&nbsp;以及&nbsp;ORG_USER进行的三个内连接，&nbsp;一个外连接和一个子选择的SQL查询。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;account,&nbsp;payment&nbsp; <BR>from&nbsp;Account&nbsp;as&nbsp;account&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;left&nbsp;outer&nbsp;join&nbsp;account.payments&nbsp;as&nbsp;payment&nbsp; <BR>where&nbsp;:currentUser&nbsp;in&nbsp;elements(account.holder.users)&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;PaymentStatus.UNPAID&nbsp;=&nbsp;isNull(payment.currentStatus.name,&nbsp;PaymentStatus.UNPAID)&nbsp; <BR>order&nbsp;by&nbsp;account.type.sortOrder,&nbsp;account.accountNumber,&nbsp;payment.dueDate&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>对于一些数据库，我们需要弃用（相关的）子选择。&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;account,&nbsp;payment&nbsp; <BR>from&nbsp;Account&nbsp;as&nbsp;account&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;account.holder.users&nbsp;as&nbsp;user&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;left&nbsp;outer&nbsp;join&nbsp;account.payments&nbsp;as&nbsp;payment&nbsp; <BR>where&nbsp;:currentUser&nbsp;=&nbsp;user&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;PaymentStatus.UNPAID&nbsp;=&nbsp;isNull(payment.currentStatus.name,&nbsp;PaymentStatus.UNPAID)&nbsp; <BR>order&nbsp;by&nbsp;account.type.sortOrder,&nbsp;account.accountNumber,&nbsp;payment.dueDate&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2><STRONG>15.13.&nbsp;批量的UPDATE&nbsp;&amp;&nbsp;DELETE语句</STRONG> <BR>HQL现在支持UPDATE与DELETE语句.&nbsp;查阅&nbsp;第&nbsp;14.3&nbsp;节&nbsp;“大批量更新/删除（Bulk&nbsp;update/delete）”&nbsp;以获得更多信息。&nbsp; <BR>15.14.&nbsp;小技巧&nbsp;&amp;&nbsp;小窍门 <BR>你可以统计查询结果的数目而不必实际的返回他们：&nbsp; <BR>(&nbsp;(Integer)&nbsp;session.iterate("select&nbsp;count(*)&nbsp;from&nbsp;....").next()&nbsp;).intValue() <BR>若想根据一个集合的大小来进行排序，可以使用如下的语句：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;usr.id,&nbsp;usr.name&nbsp; <BR>from&nbsp;User&nbsp;as&nbsp;usr&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;left&nbsp;join&nbsp;usr.messages&nbsp;as&nbsp;msg&nbsp; <BR>group&nbsp;by&nbsp;usr.id,&nbsp;usr.name&nbsp; <BR>order&nbsp;by&nbsp;count(msg)&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>如果你的数据库</FONT><A href="http://www.gamvan.com/database/" target=_blank><FONT color=#002c99></FONT></A><FONT size=2>支持子选择，你可以在你的查询的where子句中为选择的大小（selection&nbsp;size）指定一个条件:&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>from&nbsp;User&nbsp;usr&nbsp;where&nbsp;size(usr.messages)&nbsp;&gt;=&nbsp;1&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>如果你的数据库</FONT><A href="http://www.gamvan.com/database/" target=_blank><FONT color=#002c99></FONT></A><FONT size=2>不支持子选择语句，使用下面的查询：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;usr.id,&nbsp;usr.name&nbsp; <BR>from&nbsp;User&nbsp;usr.name&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;join&nbsp;usr.messages&nbsp;msg&nbsp; <BR>group&nbsp;by&nbsp;usr.id,&nbsp;usr.name&nbsp; <BR>having&nbsp;count(msg)&nbsp;&gt;=&nbsp;1&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>因为内连接（inner&nbsp;join）的原因，这个解决方案不能返回含有零个信息的User&nbsp;类的实例,&nbsp;所以这种情况下使用下面的格式将是有帮助的:&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>select&nbsp;usr.id,&nbsp;usr.name&nbsp; <BR>from&nbsp;User&nbsp;as&nbsp;usr&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;left&nbsp;join&nbsp;usr.messages&nbsp;as&nbsp;msg&nbsp; <BR>group&nbsp;by&nbsp;usr.id,&nbsp;usr.name&nbsp; <BR>having&nbsp;count(msg)&nbsp;=&nbsp;0&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>JavaBean的属性可以被绑定到一个命名查询（named&nbsp;query）的参数上：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>Query&nbsp;q&nbsp;=&nbsp;s.createQuery("from&nbsp;foo&nbsp;Foo&nbsp;as&nbsp;foo&nbsp;where&nbsp;foo.name=:name&nbsp;and&nbsp;foo.size=:size");&nbsp; <BR>q.setProperties(fooBean);&nbsp;//&nbsp;fooBean包含方法getName()与getSize()&nbsp; <BR>List&nbsp;foos&nbsp;=&nbsp;q.list();&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>通过将接口Query与一个过滤器（filter）一起使用，集合（Collections）是可以分页的：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>Query&nbsp;q&nbsp;=&nbsp;s.createFilter(&nbsp;collection,&nbsp;""&nbsp;);&nbsp;//&nbsp;一个简单的过滤器&nbsp; <BR>q.setMaxResults(PAGE_SIZE);&nbsp; <BR>q.setFirstResult(PAGE_SIZE&nbsp;*&nbsp;pageNumber);&nbsp; <BR>List&nbsp;page&nbsp;=&nbsp;q.list();&nbsp;</FONT></TD></TR></TBODY></TABLE><BR><FONT size=2>通过使用查询过滤器（query&nbsp;filter）可以将集合（Collection）的原素分组或排序:&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>Collection&nbsp;orderedCollection&nbsp;=&nbsp;s.filter(&nbsp;collection,&nbsp;"order&nbsp;by&nbsp;this.amount"&nbsp;);&nbsp; <BR>Collection&nbsp;counts&nbsp;=&nbsp;s.filter(&nbsp;collection,&nbsp;"select&nbsp;this.type,&nbsp;count(this)&nbsp;group&nbsp;by&nbsp;this.type"&nbsp;);&nbsp;</FONT></TD></TR></TBODY></TABLE><FONT size=2>不用通过初始化，你就可以知道一个集合（Collection）的大小：&nbsp; <BR></FONT>
<TABLE style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px" borderColor=#e0e0e0 cellSpacing=1 cellPadding=4 width="95%" align=center border=1 fixed TABLE-LAYOUT:>
<TBODY>
<TR>
<TD style="HEIGHT: 25px; WORD-WRAP: break-word" vAlign=top bgColor=#f6f6f6><FONT size=2><FONT style="COLOR: #b0b0b0">代码内容</FONT><BR>(&nbsp;(Integer)&nbsp;session.iterate("select&nbsp;count(*)&nbsp;from&nbsp;....").next()&nbsp;).intValue();</FONT></TD></TR></TBODY></TABLE><BR></P></STRONG><img src ="http://www.blogjava.net/sunny/aggbug/26686.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sunny/" target="_blank">随风逝</a> 2006-01-05 11:43 <a href="http://www.blogjava.net/sunny/archive/2006/01/05/26686.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>log4j的使用（转自csdn）</title><link>http://www.blogjava.net/sunny/archive/2006/01/04/26570.html</link><dc:creator>随风逝</dc:creator><author>随风逝</author><pubDate>Wed, 04 Jan 2006 08:36:00 GMT</pubDate><guid>http://www.blogjava.net/sunny/archive/2006/01/04/26570.html</guid><wfw:comment>http://www.blogjava.net/sunny/comments/26570.html</wfw:comment><comments>http://www.blogjava.net/sunny/archive/2006/01/04/26570.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sunny/comments/commentRss/26570.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sunny/services/trackbacks/26570.html</trackback:ping><description><![CDATA[<DT>&nbsp;
<DD class=ArticleContent>
<DIV class=HideDiv style="OVERFLOW: hidden; WIDTH: 100%"><SPAN id=LblContent>
<P><U><FONT color=#0000ff></FONT></U><BR><BR><BR>简单的Log4j使用只需要导入下面的包就可以了<BR>// import log4j packages<BR>import org.apache.log4j.Logger;</P><BR>
<P>然后你需要在类的开头写上下面一句<BR>private final static Logger log =<BR>Logger.getLogger(ClassName.class);//注意这里写的是当前类的类名。<BR>这样你就有了一个日志对象叫做 log，这个log有以下集中用法<BR>log.info();//一般信息<BR>log.debug();//调试信息<BR>log.error();//错误信息<BR>log.warn();//警告信息<BR>log.fatal();//致命错误信息<BR>上面列出的就是所谓log4j的输出级别，log4j建议只使用4个级别，它们从上到下分别为<BR>ERROR、WARN、INFO、DEBUG，假设你定义的级别是info，那么error和warn的日志可以显示而比他低的debug信息就不显示了。<BR>知道了上面的内容还不够，要想很好的使用log4j你还需要配置log4j的环境。因为每个log都可以通过配置它的属性文件来达到控制日志输出的格式的目的，下面我给出一个在weblogic下使用log4j的配置过程，首先给出这个配置文件的完整信息。<BR><!-- log4j.properties--><BR>#log4j.rootLogger=INFO,A1,R //这一句指定了日志输出的级别为info,A1和R分别代表日志输出到什么地方。<BR>log4j.category.hybl_wshabcm=debug,A1,R //这一句指定了日志具体输出哪个包的信息，以及输出位置<BR>log4j.appender.A1=org.apache.log4j.ConsoleAppender //这里指定了日志输出的第一个位置A1是控制台ConsoleAppender<BR>/*<BR>*其中，Log4j提供的appender有以下几种：<BR>*org.apache.log4j.ConsoleAppender（控制台），<BR>*org.apache.log4j.FileAppender（文件），<BR>*org.apache.log4j.DailyRollingFileAppender（每天产生一个日志文件），<BR>*org.apache.log4j.RollingFileAppender（文件大小到达指定尺寸的时候产生一个新的文件），<BR>*org.apache.log4j.WriterAppender（将日志信息以流格式发送到任意指定的地方）<BR>*<BR>*/<BR>log4j.appender.A1.layout=org.apache.log4j.PatternLayout //指定A1的布局模式</P><BR>
<P>/*<BR>*其中，Log4j提供的layout有以下几种：<BR>×org.apache.log4j.HTMLLayout（以HTML表格形式布局），<BR>*org.apache.log4j.PatternLayout（可以灵活地指定布局模式），<BR>*org.apache.log4j.SimpleLayout（包含日志信息的级别和信息字符串），<BR>*org.apache.log4j.TTCCLayout（包含日志产生的时间、线程、类别等等信息）<BR>*/<BR>log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n //指定日志的输出格式</P><BR>
<P><BR>log4j.appender.R=org.apache.log4j.RollingFileAppender //指定以文件的方式输出日志<BR>log4j.appender.R.File=c:/sys.html //文件位置<BR>log4j.appender.R.MaxFileSize=500KB //文件最大尺寸<BR>log4j.appender.R.MaxBackupIndex=1 //备份数<BR>log4j.appender.R.layout=org.apache.log4j.HTMLLayout //文件的格式为Html格式<BR>#log4j.appender.R.layout=org.apache.log4j.PatternLayout <BR>log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n</P><BR>
<P><!-- log4j.properties--></P><BR>
<P><BR>有了上面的这个文件我们还要在weblogic启动的时候通过一个类加载这个文件，可以使用下面的方式<BR>1、加载配置文件的Servlet<BR><!--SetupServlet --><BR>package hybl_wshabcm.servelet;</P><BR>
<P>import java.io.File;<BR>import java.io.LineNumberReader;<BR>import java.io.FileReader;<BR>import java.io.FileNotFoundException;<BR>import java.io.IOException;<BR>import java.util.Vector;<BR>import java.sql.Driver;<BR>import java.sql.DriverManager;</P><BR>
<P>// import servlet packages<BR>import javax.servlet.http.HttpServlet;<BR>import javax.servlet.ServletConfig;<BR>import javax.servlet.ServletException;</P><BR>
<P>// import log4j packages<BR>import org.apache.log4j.Logger;<BR>import org.apache.log4j.PropertyConfigurator;<BR>public class SetupServlet extends HttpServlet{</P><BR>
<P>public void init(ServletConfig config) throws ServletException{</P><BR>
<P>super.init(config);</P><BR>
<P>// first thing to do, is to set up the Driver that we might be using<BR>// in case of JDBCAppender<BR>try{<BR>Driver d =<BR>(Driver)(Class.forName(<BR>"org.gjt.mm.mysql.Driver").newInstance());<BR>DriverManager.registerDriver(d);<BR>//加载JDBC驱动程序，当准备将日志记录到数据库的时候可以使用<BR>}catch(Exception e){ System.err.println(e); }</P><BR>
<P>// next load up the properties<BR>//启动时从web.xml中获得配置文件的信息<BR>String props = config.getInitParameter("props");</P><BR>
<P>if(props == null || props.length() == 0 ||<BR>!(new File(props)).isFile()){</P><BR>
<P>System.err.println(<BR>"ERROR: Cannot read the configuration file. " +<BR>"Please check the path of the config init param in web.xml");<BR>throw new ServletException();<BR>}<BR>}</P><BR>
<P>public void destroy(){<BR>super.destroy();<BR>}<BR>}<BR><!--SetupServlet --></P><BR>
<P>2、web.xml文件的部署<BR>上面的这个Servlet在服务器启动的时候将一些初始化信息加载，那么如何在weblogic启动的时候将这个类也<BR>加载呢？看一个例子<BR><SERVLET><BR><SERVLET-NAME>setup</SERVLET-NAME> <BR><SERVLET-CLASS>hybl_wshabcm.servelet.SetupServlet</SERVLET-CLASS>//启动时装载的类 <BR><INIT-PARAM><BR><PARAM-NAME>props</PARAM-NAME><BR><PARAM-VALUE>/WEB-INF/log4j.properties</PARAM-VALUE>//部署文件的位置<BR></INIT-PARAM><BR><LOAD-ON-STARTUP>2</LOAD-ON-STARTUP>//这句很重要<BR></SERVLET></P><BR>
<P>好了，现在你可以象开头那样使用log4j来记录日志了</P></SPAN></DIV></DD><img src ="http://www.blogjava.net/sunny/aggbug/26570.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sunny/" target="_blank">随风逝</a> 2006-01-04 16:36 <a href="http://www.blogjava.net/sunny/archive/2006/01/04/26570.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>第一个hibernate例子终于调试出来了</title><link>http://www.blogjava.net/sunny/archive/2005/12/31/26155.html</link><dc:creator>随风逝</dc:creator><author>随风逝</author><pubDate>Sat, 31 Dec 2005 02:02:00 GMT</pubDate><guid>http://www.blogjava.net/sunny/archive/2005/12/31/26155.html</guid><wfw:comment>http://www.blogjava.net/sunny/comments/26155.html</wfw:comment><comments>http://www.blogjava.net/sunny/archive/2005/12/31/26155.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.blogjava.net/sunny/comments/commentRss/26155.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sunny/services/trackbacks/26155.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;很开心，直到昨天晚上，我的第一个hibernate例子才成功，之前大概经历了1周以上的时间，没办法，我比较笨。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在这里，我要记录一下几个一直困扰我，直到昨天才解决的问题，也是很多初学者会遇到的问题，希望能给更多的初学者一些提示，少走弯路。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;我用了eclipse3.1.1和myeclipse4.01,不过我觉得初学乍练还是不要用这样的工具，最好是一步一步的，有利于你对hibernate的理解，对于我这样子想要急于看到成果的，用个方便的ide也是不错的选择，废话不说了，开始正题。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;对于myeclipse的配置和使用，请大家到网上查阅，这里不再赘述。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 首先，在eclipse里面创建一个web project，并且给这个项目add hibernate capabilities，一切设置保持默认就好，这样之后你的项目就已经具备了hibernate的功能，接下来应该进行hibernate.cfg.xml的配置了，这一步实际上是连接数据库。我把我的配置文件粘贴在下面： 
<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">
<P><SPAN style="COLOR: #000000">&lt;?xml version='1.0' encoding='UTF-8'?&gt;<BR>&lt;!DOCTYPE hibernate-configuration PUBLIC<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "-//Hibernate/Hibernate Configuration DTD 3.0//EN"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "<A href="http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd</A>"&gt;</SPAN></P>
<P><SPAN style="COLOR: #000000">&lt;!-- Generated by MyEclipse Hibernate Tools.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --&gt;<BR>&lt;hibernate-configuration&gt;</SPAN></P>
<P><SPAN style="COLOR: #000000">&lt;session-factory&gt;<BR>&nbsp;&lt;property name="myeclipse.connection.profile"&gt;thin&lt;/property&gt;<BR>&nbsp;&lt;!-- JDBC驱动程序 --&gt;<BR>&nbsp;&lt;property name="connection.driver_class"&gt;oracle.jdbc.driver.OracleDriver&lt;/property&gt;<BR>&nbsp;&lt;!-- JDBC URL --&gt;<BR>&nbsp;&lt;property name="connection.url"&gt;jdbc:oracle:thin:@127.0.0.1:1521:test&lt;/property&gt;<BR>&nbsp;&lt;!--&nbsp;数据库用户名 --&gt;<BR>&nbsp;&lt;property name="connection.username"&gt;test&lt;/property&gt;<BR>&nbsp;&lt;!--&nbsp;数据库密码 --&gt;<BR>&nbsp;&lt;property name="connection.password"&gt;test&lt;/property&gt;<BR>&lt;!--&nbsp;sql的方言，不同db的sql略有不同 --&gt;<BR>&nbsp;&lt;property name="dialect"&gt;org.hibernate.dialect.<FONT face="Courier New">OracleDialect</FONT>&lt;/property&gt;<BR>&lt;!--&nbsp;后台显示sql语句，主要是调试用--&gt;<BR>&nbsp;&lt;property name="show_sql"&gt;ture&lt;/property&gt;<BR></SPAN><SPAN style="COLOR: #000000"><BR>&lt;/session-factory&gt;</SPAN></P>
<P><SPAN style="COLOR: #000000">&lt;/hibernate-configuration&gt;</SPAN></P></DIV>现在你可以切换到eclipse的db视图了，在那里你可以creat hibernate mapping，这样，会自动的生成pojo，并且更新hibernate.cfg.xml。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这个过程十分简单，选择好pojo的包，然后鼠标轻轻一点就会完成。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;接下来是编写一个测试页了，这里我只做了一个select页面，里面的代码：<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 id=Codehighlighter1_2_109_Open_Image onclick="this.style.display='none'; Codehighlighter1_2_109_Open_Text.style.display='none'; Codehighlighter1_2_109_Closed_Image.style.display='inline'; Codehighlighter1_2_109_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_2_109_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2_109_Closed_Text.style.display='none'; Codehighlighter1_2_109_Open_Image.style.display='inline'; Codehighlighter1_2_109_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">&lt;%</SPAN><SPAN id=Codehighlighter1_2_109_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_2_109_Open_Text><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">@&nbsp;page&nbsp;language</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">java</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&nbsp;import</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">hiber.*,org.hibernate.*,org.hibernate.cfg.*,java.util.*</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&nbsp;pageEncoding</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">UTF-8</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN></SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #000000">&lt;!--<SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">hiber.*是这个项目pojo的包</SPAN>--&gt;<BR><IMG id=Codehighlighter1_187_333_Open_Image onclick="this.style.display='none'; Codehighlighter1_187_333_Open_Text.style.display='none'; Codehighlighter1_187_333_Closed_Image.style.display='inline'; Codehighlighter1_187_333_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_187_333_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_187_333_Closed_Text.style.display='none'; Codehighlighter1_187_333_Open_Image.style.display='inline'; Codehighlighter1_187_333_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">&lt;%</SPAN><SPAN id=Codehighlighter1_187_333_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_187_333_Open_Text><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">String</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&nbsp;path&nbsp;</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&nbsp;request.getContextPath();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">String</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&nbsp;basePath&nbsp;</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>request.getScheme()</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">://</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">request.getServerName()</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">:</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">request.getServerPort()</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">path</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">/</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">%&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: #ff00ff">DOCTYPE&nbsp;HTML&nbsp;PUBLIC&nbsp;"-//W3C//DTD&nbsp;HTML&nbsp;4.01&nbsp;Transitional//EN"</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">html</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">head</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">title</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">test&nbsp;hibernate</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">title</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">meta&nbsp;</SPAN><SPAN style="COLOR: #ff0000">http-equiv</SPAN><SPAN style="COLOR: #0000ff">="pragma"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;content</SPAN><SPAN style="COLOR: #0000ff">="no-cache"</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">meta&nbsp;</SPAN><SPAN style="COLOR: #ff0000">http-equiv</SPAN><SPAN style="COLOR: #0000ff">="cache-control"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;content</SPAN><SPAN style="COLOR: #0000ff">="no-cache"</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">meta&nbsp;</SPAN><SPAN style="COLOR: #ff0000">http-equiv</SPAN><SPAN style="COLOR: #0000ff">="expires"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;content</SPAN><SPAN style="COLOR: #0000ff">="0"</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">meta&nbsp;</SPAN><SPAN style="COLOR: #ff0000">http-equiv</SPAN><SPAN style="COLOR: #0000ff">="keywords"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;content</SPAN><SPAN style="COLOR: #0000ff">="keyword1,keyword2,keyword3"</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">meta&nbsp;</SPAN><SPAN style="COLOR: #ff0000">http-equiv</SPAN><SPAN style="COLOR: #0000ff">="description"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;content</SPAN><SPAN style="COLOR: #0000ff">="This&nbsp;is&nbsp;my&nbsp;page"</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"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&lt;link&nbsp;rel="stylesheet"&nbsp;type="text/css"&nbsp;href="styles.css"&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></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: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">head</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">body</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_792_1483_Open_Image onclick="this.style.display='none'; Codehighlighter1_792_1483_Open_Text.style.display='none'; Codehighlighter1_792_1483_Closed_Image.style.display='inline'; Codehighlighter1_792_1483_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_792_1483_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_792_1483_Closed_Text.style.display='none'; Codehighlighter1_792_1483_Open_Image.style.display='inline'; Codehighlighter1_792_1483_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">&lt;%</SPAN><SPAN id=Codehighlighter1_792_1483_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_792_1483_Open_Text><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Session&nbsp;ses&nbsp;</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&nbsp;</SPAN><SPAN style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">null</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try{<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;sf</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">new</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&nbsp;Configuration().configure().buildSessionFactory();<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;ses</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">sf.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;Query&nbsp;query</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">ses.createQuery(</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">select&nbsp;t&nbsp;from&nbsp;Yhzd&nbsp;t</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">);//这个的地方需要特别注意，这里的Yhzd是类名，不是表名，所以<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//如果换成写就会出错误，而且不能使用select * ，这里返回的<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;查询结果也是类的list<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;List&nbsp;list</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">query.list();<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;out.print(</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">list's&nbsp;size&nbsp;is</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">list.size()</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&nbsp;&lt;br&gt;</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">);<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;Iterator&nbsp;it</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">list.iterator();<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; BACKGROUND-COLOR: #f5f5f5">while</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">(it.hasNext()){</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"><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;//下面是我数据库中的表，可以根据你的数据库来改写<SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"><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;Yhzd&nbsp;yhzd&nbsp;<SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&nbsp;(Yhzd)it.next();//这个地方卡了我很久，就是这个转换，因为当初我用了<SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">select&nbsp;id from&nbsp;Yhzd&nbsp;t，而id本身不<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//是一个yhzd的类，所以无法转换。</SPAN></SPAN></SPAN></SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"><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;out.println(</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">name==</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">yhzd.getYhmc()</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">yhlx==</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">yhzd.getYhlx()</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&nbsp;&lt;br&gt;</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">);<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;}<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}catch(HibernateException&nbsp;e){<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;out.print(</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">error!!!!!!!!!!!!</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">);<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;e.printStackTrace();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}finally{<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; BACKGROUND-COLOR: #f5f5f5">if</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">(ses&nbsp;!</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&nbsp;</SPAN><SPAN style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">null</SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">)&nbsp;ses.close();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></SPAN><SPAN style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">%&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">body</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">html</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN></DIV>整个例子完成了，由于时间比较少，所以没有做add ，edit 和delete，如果哪位朋友想要的话可以留言联系，我会尽快完成的。<img src ="http://www.blogjava.net/sunny/aggbug/26155.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sunny/" target="_blank">随风逝</a> 2005-12-31 10:02 <a href="http://www.blogjava.net/sunny/archive/2005/12/31/26155.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>struts 整合 hibernate需要注意的问题（转） </title><link>http://www.blogjava.net/sunny/archive/2005/12/30/26090.html</link><dc:creator>随风逝</dc:creator><author>随风逝</author><pubDate>Fri, 30 Dec 2005 09:13:00 GMT</pubDate><guid>http://www.blogjava.net/sunny/archive/2005/12/30/26090.html</guid><wfw:comment>http://www.blogjava.net/sunny/comments/26090.html</wfw:comment><comments>http://www.blogjava.net/sunny/archive/2005/12/30/26090.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/sunny/comments/commentRss/26090.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sunny/services/trackbacks/26090.html</trackback:ping><description><![CDATA[<DIV class=postTitle><A class=postTitle2 id=viewpost1_TitleUrl HREF="/sunny/articles/25424.html"><FONT color=#000000>struts 整合 hibernate需要注意的问题（转）</FONT></A> －－出自www.hibernate.org</DIV>
<P>在 struts+ hibernate 这种结构中，是不应该把Hibernate产生的PO直接传递给JSP的，不管他是Iterator，还是List，这是一个设计错误。 </P>
<P>我来谈谈在J2EE架构中各层的数据表示方法： </P>
<P>Web层的数据表示是FormBean，数据来源于HTML Form POST <BR>业务层的数据表示是VO <BR>持久层的数据表示是PO，其数据来源于数据库，持久层的数据表示例如CMP </P>
<P>在一个规范的J2EE架构中，不同层的数据表示应该被限制在层内，而不应该扩散到其它层，这样可以降低层间的耦合性，提高J2EE架构整体的可维护性和可扩展性。比如说Web层的逻辑进行了修改，那么只需要修改FormBean的结构，而不需要触动业务层和持久层的代码修改。同样滴，当数据库表进行了小的调整，那么也只需要修改持久层数据表示，而不需要触动业务层代码和Web层代码。 </P>
<P>不过由于Hibernate的强大功能，例如动态生成PO，PO的状态管理可以脱离Session，使得在应用了Hibernate的J2EE框架中，PO完全可以充当VO，因此我们下面把PO和VO合并，统称为PO。 </P>
<P>先来谈谈ActionFormBean和持久层的PO之间的重大区别。 </P>
<P>在简单的应用中，ActionFormBean和PO几乎是没有区别，所以很多人干脆就是用ActionFormBean来充当PO，于是ActionFormBean从JSP页面到Servlet控制层再到业务层，然后穿过持久层，最后一直映射到数据库表。真是一竿子捅到了底！ </P>
<P>但是在复杂的应用中，ActionFormBean和PO是分离的，他们也不可能一样。ActionFormBean是和网页里面的Form表单一一对应的，Form里面有什么元素，Bean里面就有什么属性。而PO和数据库表对应，因此如果数据库表不修改，那么PO也不会修改，如果页面的流程和数据库表字段对应关系不一致，那么你又如何能够使用ActionFormBean来取代PO呢？ </P>
<P>比如说吧，用户注册页面要求注册用户的基本信息，因此HTML Form里面包含了基本信息属性，于是你需要一个ActionFormBean来一一对应(注意：是一一对应)，每个Bean属性对应一个文本框或者选择框什么的。 </P>
<P>而用户这个持久对象呢？他的属性和ActionFormBean有什么明显不同呢？他会有一些ActionFormBean所没有的集合属性，比如说用户的权限属性，用户的组属性，用户的帖子等等。另外还有可能的是在ActionFormBean里面有3个属性，分别是用户的First Name, Middle Name, Last Name，而在我的User这个持久对象中就是一个 Name 对象属性。 </P>
<P>假设我的注册页面原来只要你提供First Name，那么ActionFormBean就这一个属性，后来我要你提供全名，你要改ActionFormBean，加两个属性。但是这个时候PO是不应该修改滴，因为数据库没有改。 </P>
<P>那么在一个完整的J2EE系统中应该如何进行合理的设计呢？ </P>
<P>JSP(View) ---&gt; ActionFormBean(Module) ---&gt; Action(Control) </P>
<P>ActionFormBean是Web层的数据表示，它和HTML页面Form对应，只要Web页面的操作流程发生改变，它就要相应的进行修改，它不应该也不能被传递到业务层和持久层，否则一旦页面修改，会一直牵连到业务层和持久层的大面积的代码进行修改，对于软件的可维护性和可扩展性而言，是一个灾难，Actiont就是他的边界，到此为止！ </P>
<P>Action(Web Control) ---&gt; Business Bean ---&gt; DAO ---&gt; ORM ---&gt;DB </P>
<P>而PO则是业务层和持久层的数据表示，它在业务层和持久层之间进行流动，他不应该也不能被传递到Web层的View中去，而ActionServlet就是他的边界，到此为止！ </P>
<P>然后来看一看整个架构的流程： </P>
<P>当用户通过浏览器访问网页，提交了一个页面。于是Action拿到了这个FormBean，他会把FormBean属性读出来，然后构造一个PO对象，再调用业务层的Bean类，完成了注册操作，重定向到成功页面。而业务层Bean收到这个PO对象之后，调用DAO接口方法，进行持久对象的持久化操作。 </P>
<P>当用户查询某个会员的信息的时候，他用全名进行查询，于是Action得到一个UserNameFormBean包括了3个属性，分别是first name, middle name, last name，然后Action把UserNameFormBean的3个属性读出来，构造Name对象，再调用业务Bean，把Name对象传递给业务Bean，进行查询。 </P>
<P>业务Bean取得Name(注意: Name对象只是User的一个属性)对象之后调用DAO接口，返回一个User的PO对象，注意这个User不同于在Web层使用的UserFormBean，他有很多集合属性滴。然后业务Bean把User对象返回给Action。 </P>
<P>Action拿到User之后，把User的基本属性取出(集合属性如果不需要就免了)，构造UserFormBean，然后把UserFormBean request.setAttribute(...)，然后重定向到查询结果页面。 </P>
<P>查询页面拿到request对象里面的ActionFormBean，自动调用tag显示之。 </P>
<P>总结： </P>
<P>FormBean是Web层的数据表示，他不能被传递到业务层；PO是持久层的数据表示，在特定情况下，例如Hibernate中，他可以取代VO出现在业务层，但是不管PO还是VO都必须限制在业务层内使用，最多到达Web层的Control，绝不能被扩散到View去。 </P>
<P>FormBean和PO之间的数据转化是在Action中进行滴。 </P>
<P>BTW: </P>
<P>JDO1.x还不能像Hibernate功能这样强大，PO不能脱离持久层，所以必须在业务层使用VO，因此必须在业务层进行大量的VO和PO的转化操作，相对于Hibernate来说，编程比较烦琐。 </P>
<P>当然咯，理论是一回事，实际操作也不一定非要这样干，你可以自行取舍，在实际项目中灵活一点，增加一点bad smell，提高开发效率。只不过在大型项目中最好还是严丝合缝，不然的话，改版的时候会痛苦的很滴。</P><img src ="http://www.blogjava.net/sunny/aggbug/26090.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sunny/" target="_blank">随风逝</a> 2005-12-30 17:13 <a href="http://www.blogjava.net/sunny/archive/2005/12/30/26090.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>