﻿<?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-辰o(^o^)o的专栏[除非注释原创，其它文章基本来源于网络]-文章分类-Hibernate</title><link>http://www.blogjava.net/jackybu/category/1616.html</link><description>&lt;a href="http://www.fastonlineusers.com"&gt;&lt;b&gt;&lt;font color=red&gt;共有&lt;script src=http://fastonlineusers.com/online.php?d=jackybu.blogjava.net&gt;&lt;/script&gt;人在同时阅读此Blog&lt;/font&gt;&lt;/b&gt;&lt;/a&gt;</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 03:39:20 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 03:39:20 GMT</pubDate><ttl>60</ttl><item><title>[转]hibernate:one-to-one的学习小经验</title><link>http://www.blogjava.net/jackybu/articles/10501.html</link><dc:creator>辰</dc:creator><author>辰</author><pubDate>Fri, 19 Aug 2005 05:45:00 GMT</pubDate><guid>http://www.blogjava.net/jackybu/articles/10501.html</guid><wfw:comment>http://www.blogjava.net/jackybu/comments/10501.html</wfw:comment><comments>http://www.blogjava.net/jackybu/articles/10501.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jackybu/comments/commentRss/10501.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jackybu/services/trackbacks/10501.html</trackback:ping><description><![CDATA[<STRONG>&nbsp;one-to-one</STRONG>在hibernate中可以用来作为两张表之间的主键关联，这也是hibernate中主键关联的一种用法，这样在一张表中的ID，在生成另外一张表的同时回自动插入到相应的ID字段中去，相应的XML文件设置比较简单，举例如下：<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;!-- 建立一对一的到Address的映射，<FONT color=#ff0000>这个是写在User的XML配置文件中的</FONT> --&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--&nbsp;<FONT color=#ff0000>相应的User bean（PO）中也要添加属性 <STRONG>com.xx.Address&nbsp;</STRONG>&nbsp;<STRONG>address</STRONG></FONT>--&gt;<BR>&nbsp;&nbsp;&nbsp; <STRONG><FONT color=#008000>&lt;one-to-one name="address" cascade="all" class="com.xx.Address"/&gt;<BR></FONT></STRONG>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&lt;!--&nbsp;cascade的属性设置不再重复了，可以查看hibernate文档 --&gt;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;!-- 建立一对一的到User的映射，<FONT color=#ff0000>这个是写在Address的XML配置文件中的</FONT> --&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--&nbsp;<FONT color=#ff0000>相应的Address bean（PO）中也要添加属性 </FONT><FONT color=#ff0000><STRONG>com.xx.</STRONG>User user</FONT>--&gt; --&gt;<BR>&nbsp;&nbsp;&nbsp; <STRONG><FONT color=#008000>&lt;one-to-one name="user" class="com.xx.User" constrained="true"/&gt;<BR></FONT></STRONG><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;为了在Address中使用User中的主键ID值，我们需要设置Address中的主键生成规则，如下所示，采用foreign关键字<BR><BR>&nbsp;&nbsp;&nbsp;<STRONG><FONT color=#008000>&lt;id column="ID" name="id" type="long" unsaved-value="0"&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;generator class="foreign"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param name="property"&gt;user&lt;/param&gt;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/generator&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/id&gt;<BR></FONT></STRONG><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#000000><STRONG><EM>这里需要注意的是property的属性值必须与上面到User的映射所填写的name属性值一致</EM></STRONG></FONT>，这样就完成了one-to-one的映射关系。<BR><BR>上面的过程都很简单，下面我来说说这里需要注意的地方：<BR><BR>&nbsp;&nbsp;1.&nbsp;&nbsp;&nbsp;<STRONG>在设置属性ID的时候必须注意字段的长度，如笔者这样使用oracle的sequence来生成ID，其长度有14位之长，则应选择hibernate类型long，对应的实体中应选择Long，这样不会出现溢出的情况。</STRONG><BR><BR><BR>&nbsp;&nbsp;2.&nbsp;&nbsp;&nbsp;在测试的时候必须要注意这两张表之间因为已经存在了一对一的关系，所以我们不能只写<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#008000>user.setAddress(address);<BR></FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;而忽略了<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#008000>address.setUser(user);<BR></FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这样在做插入的时候会报出attempted to assign id from null one-to-one property: address的错误，这一点初学者会经常犯，笔者也是其中之一。<BR><BR><BR>&nbsp;3.&nbsp;&nbsp;&nbsp;如果不写cascade="all"或者写成cascade="none"的话，即使你写了<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#008000>user.setAddress(address);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;address.setUser(user);<BR></FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;也不会发生任何事情，只有user会被存储。<BR><BR>以上是一些笔者经历的小经验，如果有不对的地方欢迎指正。<img src ="http://www.blogjava.net/jackybu/aggbug/10501.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jackybu/" target="_blank">辰</a> 2005-08-19 13:45 <a href="http://www.blogjava.net/jackybu/articles/10501.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]One-to-one关系映射</title><link>http://www.blogjava.net/jackybu/articles/10497.html</link><dc:creator>辰</dc:creator><author>辰</author><pubDate>Fri, 19 Aug 2005 05:30:00 GMT</pubDate><guid>http://www.blogjava.net/jackybu/articles/10497.html</guid><wfw:comment>http://www.blogjava.net/jackybu/comments/10497.html</wfw:comment><comments>http://www.blogjava.net/jackybu/articles/10497.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jackybu/comments/commentRss/10497.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jackybu/services/trackbacks/10497.html</trackback:ping><description><![CDATA[<FONT face=宋体 size=5>对于</FONT><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic">hibernate</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic">one-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">关系来说，大家常常把它忽略，认为它很简单，其实这里面有些细节需要注意，在</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic">hibernate3</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中有两种实现one-to-one的方法：第一种就是用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic">many-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来代替一对多，其实</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic">one-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">也就是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic">many-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的一种极限方式，若把</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic">many-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-style: italic; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设置</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">unique="true"</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，则这时候的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">many-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">实质上就是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">one-to-one;</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这里为什么能够用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">many-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来代替</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">one-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">呢？最根本的是两个对象必须有一个字段相关联，那么你也发现</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">one-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中没有</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">column</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">属性，也就是不能够把</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">one-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的这种关系生成一个字段</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">/</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">属性，而</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">many-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">可以，它有</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">column</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">属性。所以，说了这么多，你应该明白为什么可以用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">many-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来代替</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">one-to-one.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于这种方法要注意几点：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例如，下面的介绍中</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">use</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的两个字段同时影射</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">adress</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，只能够实现单向</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">one-to-one,</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">也就是从</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">use</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">到</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">adress</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，不能够实现从</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">adress</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">到</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">use.</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">然但下面也介绍了可以使用复杂的方法来解决，但是得不偿失。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">第二种方法：<SPAN style="mso-bidi-font-style: italic">基于主键关联的</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt; mso-bidi-font-style: italic">one-to-one,</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-style: italic; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这种方法比较直接。也就是让</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt; mso-bidi-font-style: italic">adress</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-style: italic; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的主键值和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt; mso-bidi-font-style: italic">use</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-style: italic; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的一样就可以了，通过</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt; mso-bidi-font-style: italic"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;id name="id" column="ADDRESS_ID"&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;generator class="foreign"&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;param name="property"&gt;user&lt;/param&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;/generator&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来把</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">adress</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的主键值和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">use</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的主键值相等。其余的设置都是小事。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于初学者来说应该注意：单在</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">use</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">adress</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中设置</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">one-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是不行的，需要设置上面的代码，需要实现<SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">one-to-one两端的对象主键值相同，这是最主要的</SPAN>。这也是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">one-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">与</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">many-to-one</SPAN><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的不同之处。应当注意。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&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;</SPAN>fasttalk<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&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;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-font-kerning: 0pt"><A href="http://www.blogjava.net/asktalk"><FONT color=#4371a6>www.blogjava.net/asktalk</FONT></A><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><I><SPAN lang=EN-US style="FONT-SIZE: 7.5pt; mso-font-kerning: 0pt"><o:p></o:p></SPAN></I></B>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 14pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><I><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">Using a foreign key association<o:p></o:p></SPAN></I></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">The easiest way to represent the association from User to its billingAddress is to use a &lt;many-to-one&gt; mapping with a unique constraint on the foreign key. This may surprise you, since <I>many </I>doesn’t seem to be a good description of either end of a one-to-one association! However, from Hibernate’s point of view, there isn’t much difference between the two kinds of foreign key associations. So, we add a foreign key column named BILLING_ADDRESS_ID to the USER table and map it as follows:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;many-to-one name="billingAddress"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">class="Address"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">column="BILLING_ADDRESS_ID"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">cascade="save-update"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">Note that we’ve chosen save-update as the cascade style. This means the Address will become persistent when we create an association from a persistent User. Probably,<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>cascade="all" makes sense for this association, since deletion of the User should result in deletion of the Address. (Remember that Address now has its own entity lifecycle.)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">Our database schema still allows duplicate values in the BILLING_ADDRESS_ID column of the USER table, so two users could have a reference to the same address. To make this association truly one-to-one, we add unique="true" to the &lt;many-toone&gt; element, constraining the relational model so that there can be only one user per address:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;many-to-one name="billingAddress"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">class="Address"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">column="BILLING_ADDRESS_ID"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">cascade="all"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">unique="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">This change adds a unique constraint to the BILLING_ADDRESS_ID column in the DDL generated by Hibernate—resulting in the table structure illustrated by figure 6.7.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">But what if we want this association to be navigable from Address to User in Java?<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>From chapter 3, you know how to turn it into a bidirectional one-to-many collection—but we’ve decided that each Address has just one User, so this can’t be the right solution. We don’t want a collection of users in the Address class. Instead, we add a property named user (of type User) to the Address class, and map it like so in the mapping of Address:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;one-to-one name="user"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">class="User"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">property-ref="billingAddress"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">This mapping tells Hibernate that the user association in Address is the reverse direction of the billingAddress association in User.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">In code, we create the association between the two objects as follows:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">Address address = new Address();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">address.setStreet("<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:Street w:st="on"><st1:address w:st="on">646 Toorak Rd</st1:address></st1:Street>");<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">address.setCity("Toorak");<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">address.setZipcode("3000");<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">Transaction tx = session.beginTransaction();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">User user = (User) session.get(User.class, userId);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">address.setUser(user);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">user.setBillingAddress(address);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">tx.commit();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 14pt"><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype><v:shape id=_x0000_i1025 style="WIDTH: 374.25pt; HEIGHT: 193.5pt" type="#_x0000_t75"><v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image001.emz" o:title=""><IMG height=258 alt=image002.gif src="http://www.blogjava.net/images/blogjava_net/asktalk/images/image002.gif" width=499 border=0></v:imagedata></v:shape><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">To finish the mapping, we have to map the homeAddress property of User. This is easy enough: we add another &lt;many-to-one&gt; element to the User metadata, mapping a new foreign key column, HOME_ADDRESS_ID:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;many-to-one name="homeAddress"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">class="Address"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">column="HOME_ADDRESS_ID"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">cascade="save-update"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">unique="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">The USER table now defines two foreign keys referencing the primary key of the ADDRESS table: HOME_ADDRESS_ID and BILLING_ADDRESS_ID.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">Unfortunately, we can’t make both the billingAddress and homeAddress associations bidirectional, since we don’t know if a particular address is a billing address or a home address. (We can’t decide which property name—billingAddress or homeAddress—to use for the property-ref attribute in the mapping of the user property.) We <I>could </I>try making Address an abstract class with subclasses HomeAddress and BillingAddress and mapping the associations to the subclasses. This approach would work, but it’s complex and probably not sensible in this case.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Our advice is to avoid defining more than one one-to-one association between any two classes. If you must, leave the associations unidirectional. If you don’t have more than one—if there really is exactly one instance of Address per User—there is an alternative approach to the one we’ve just shown. Instead of defining a foreign key column in the USER table, you can use a <I>primary key association</I>.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><I><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">Using a primary key association<o:p></o:p></SPAN></I></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">Two tables related by a primary key association share the same primary key values.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The primary key of one table is also a foreign key of the other. The main difficulty with this approach is ensuring that associated instances are assigned the same primary key value when the objects are saved. Before we try to solve this problem, let’s see how we would map the primary key association.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">For a primary key association, <I>both </I>ends of the association are mapped using the &lt;one-to-one&gt; declaration. This also means that we can no longer map both the billing and home address, only one property. Each row in the USER table has a corresponding row in the ADDRESS table. Two addresses would require an additional table, and this mapping style therefore wouldn’t be adequate. Let’s call this single address property address and map it with the User:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;one-to-one name="address"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">class="Address"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">cascade="save-update"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 14pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">Next, here’s the user of Address:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;one-to-one name="user"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">class="User"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">constrained="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">The most interesting thing here is the use of constrained="true". It tells Hibernate that there is a foreign key constraint on the primary key of ADDRESS that refers to the primary key of USER.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">Now we must ensure that newly saved instances of Address are assigned the same identifier value as their User. We use a special Hibernate identifier-generation strategy called foreign:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;class name="Address" table="ADDRESS"&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;id name="id" column="ADDRESS_ID"&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;generator class="foreign"&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;param name="property"&gt;user&lt;/param&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;/generator&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;/id&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">...<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;one-to-one name="user"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">class="User"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">constrained="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">&lt;/class&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">The &lt;param&gt; named property of the foreign generator allows us to name a one-toone association of the Address class—in this case, the user association. The foreign generator inspects the associated object (the User) and uses its identifier as the identifier of the new Address. Look at the table structure in figure 6.8.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The code to create the object association is unchanged for a primary key association; <SPAN style="mso-spacerun: yes">&nbsp;</SPAN>it’s the same code we used earlier for the many-to-one mapping style.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 14pt"><v:shape id=_x0000_i1026 style="WIDTH: 341.25pt; HEIGHT: 176.25pt" type="#_x0000_t75"><v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image003.emz" o:title=""><IMG height=235 alt=image004.gif src="http://www.blogjava.net/images/blogjava_net/asktalk/images/image004.gif" width=455 border=0></v:imagedata></v:shape><o:p></o:p></SPAN></P><img src ="http://www.blogjava.net/jackybu/aggbug/10497.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jackybu/" target="_blank">辰</a> 2005-08-19 13:30 <a href="http://www.blogjava.net/jackybu/articles/10497.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]one-to-one的效率问题，用one-to-many来替代？</title><link>http://www.blogjava.net/jackybu/articles/10495.html</link><dc:creator>辰</dc:creator><author>辰</author><pubDate>Fri, 19 Aug 2005 05:26:00 GMT</pubDate><guid>http://www.blogjava.net/jackybu/articles/10495.html</guid><wfw:comment>http://www.blogjava.net/jackybu/comments/10495.html</wfw:comment><comments>http://www.blogjava.net/jackybu/articles/10495.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jackybu/comments/commentRss/10495.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jackybu/services/trackbacks/10495.html</trackback:ping><description><![CDATA[由于最近在把以前的一个设计移到hibernate上来，所以需要用到one-to-one，因为在以前的设计中需要用到在一个主表中对于多个子表的主键关联，所以一开始就想到了one-to-one的应用，觉得这样解决不但不会引起以前数据设计的改变，也能够很好的利用hibernate所带来的OR优势，可是当实际使用的时候发现，在插入数据的时候可以有选择的在任意子表中进行插入，所有的结果都在原来的预期之中，但是在查询的时候，比如说只查询主表中的内容<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#008000>From tableMain</FONT><BR><BR>仅仅执行看起来十分简单的一条语句，你所期望的是他紧紧查询T_MAIN这张主表，可是结果确实hibernate通过多个外连接将所有的子表一口气的全部查询出来<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#008000>select * from t_main main outer join t_sub1 sub1 on main.id = sub1.id outer join t_sub2 sub2 on main.id = sub2.id...</FONT><BR><BR>&nbsp;如此的效率绝对让你头痛不已，不仅如此，如果你通过首先获得子表t_sub1的某个主键ID，然后通过这个主键查询出子表对象，在关联至住表，同样的情况又会发生，又会生成类似的SQL语句，这样一来看来对于这个设计应用one-to-one本身就是一种错误，是这样吗？<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;或许有人认为我们在每个one-to-one中加入lazy="true"这个属性会杜绝上述情况的发生，经过笔者的证实即便你加入了lazy="true"，也不会带来任何的改变；又或者在hibernate.config中加入fetch depth属性以及在每个关联中设置outer-join="false"，这些都不会引起本质上的变化，加入outer-join="false"其实结果只是将原有的outer join语句改变成多条sql语句而已，并没发生什么本质变化，反而效率更低了。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;该怎么办呢？我们先仔细研究一下one-to-one的概念，one to one代表一对一，在一般的模型中很少会遇到one-to-one这种概念，因为他十分强调一对一的概念，就好比一个人他只有一个身体和一个头而已，头和身体是十分好的例子，因为有身体必定只有一个头，而且说到了身体必定要说头，就好像看了某个女孩的身材必定想知道她的长相如何（-_-），所以在这时我们使用one-to-one，因为这种一对一的关系是很强的，而且从对象中取得body必定会取得他所关联的head，这样的情况下使用outer-join是十分方便和有效率的，因为它使用了outer join查询从而避免了两条到数据库的查询语句，而且在这种情况下也只需要在body_hbm.xml中设置一个one-to-one即可，所以在这种确实是一对一<STRONG>而且</STRONG>在主表中一对一的关联个数（即主表中one-to-one标签）十分少的情况下，使用one-to-one是一种很不错的解决办法。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果一个主表会对多个子表都进行one-to-one关联呢，就像我们一开始遇到的这种情况，比如你不仅仅只想了解那个你中意的女孩的身材和脸蛋，而且还想知道他的学历，身世等等一切，在这种情况下，如果我们都是用多个one-to-one在主表中的话，那情况正如我们一开始看见的，是十分可怕的，该怎么做呢？不妨考虑一下使用one-to-many，什么，many?一开始听到many这个词的时候，我也觉得挺惊讶的这明明是多个一对一的关联为什么要用到many呢？其实many并没有一定要说是大于一的，你就只在它的many中存在一个关联它有能乃你何呢？如果用到many的话，我们就需要改动数据表的设计了，在每个有关连的子表中加入一列main_id代表主表中该记录的主键子段值，只需要这样子改动就可以了，这样所带来的效果绝对是值得你这样做的，然后我们就按照以往的one-to-many来设计就好了<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在body.hbm.xml加入（一到head的关联举例，其他的关联按照这样的格式添加即可）<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;set name="head" inverse="true" lazy="true" cascade="all-delete-orphan"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;key column="ID0000"/&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;one-to-many class="com.xx.Head"/&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/set&gt;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在head.hbm.xml加入<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;many-to-one name="body" column="ID0000" class="com.xx.Body" not-null="true"/&gt;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;行了，经过上面的改动我们就摆脱了查询时多个outer-join的困扰，只在需要的时候才对子表进行查询，因为设置了lazy="true"，所以一切的一切都在我们的预料之中，我们如果希望获得body的话hibernate绝对不会把它的head 也查询出来，节省了查询是所需要的负担，除非到了我们十分需要head的情况才会进行关联查询，获得所需要的head结果。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;所以由此看来<FONT color=#0000ff><STRONG>在one-to-one这种一对一的关系不是很强的情况下，或者是在一张表中存在多个one-to-one的情况下，使用one-to-many来代替one-to-one不失为一种不错的做法</STRONG></FONT>，当然更重要的良好的数据库设计，hibernate毕竟只是末，<STRONG>千万不要本末倒置</STRONG>。<BR><img src ="http://www.blogjava.net/jackybu/aggbug/10495.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jackybu/" target="_blank">辰</a> 2005-08-19 13:26 <a href="http://www.blogjava.net/jackybu/articles/10495.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate的检索方式</title><link>http://www.blogjava.net/jackybu/articles/9687.html</link><dc:creator>辰</dc:creator><author>辰</author><pubDate>Tue, 09 Aug 2005 13:21:00 GMT</pubDate><guid>http://www.blogjava.net/jackybu/articles/9687.html</guid><wfw:comment>http://www.blogjava.net/jackybu/comments/9687.html</wfw:comment><comments>http://www.blogjava.net/jackybu/articles/9687.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jackybu/comments/commentRss/9687.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jackybu/services/trackbacks/9687.html</trackback:ping><description><![CDATA[<!--StartFragment --><FONT style="BACKGROUND-COLOR: #ff0000">&nbsp;Q:</FONT><SPAN class=postbody>Hibernate的检索方式有很多 <BR>对象图的导航检索，load/get检索，HQL(find,Query)检索，QBC(Criteria，QBE)检索以及本地数据库SQL检索. <BR>那么，在实际应用中到底选择哪一种检索好？它们之间的检索有哪些联系和区别呢？</SPAN>&nbsp;<BR><BR><!--StartFragment --><FONT style="BACKGROUND-COLOR: #ff0000"> A:(by Robbin)</FONT><SPAN class=postbody>HQL功能最强大，适合各种情况，但是动态条件查询构造起来很不方便 <BR>Criteria最适合动态条件查询，不太适合统计查询，QBE还不够强大，只适合相当简单的查询 <BR>NativeSQL可以实现特定数据库的SQL，但是可移植性就牺牲了 <BR><BR><BR>Hibernate2的Criteria功能不够完善，所以Hibernate2上面可用的只有HQL和NativeSQL，Hibernate3的Criteria已经非常强大了。 <BR><BR>我的选择原则是： <BR><BR>针对web应用来说，大部分常规查询都是动态条件查询，所以首选使用Criteria，并且Hibernate3提供了DetachedCriteria，可以在web层构造好DetachedCriteria再进入session执行查询，非常方便实用的特性。 <BR><BR>但是涉及到统计查询和非常复杂的关联查询，Criteria就无能为力了，这种情况下我选择使用HQL。 <BR><BR>最后如果必须使用某些数据库的特性，例如Oracle的"...connect with ... by"这样的SQL，则选择使用NativeSQL。</SPAN> <img src ="http://www.blogjava.net/jackybu/aggbug/9687.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jackybu/" target="_blank">辰</a> 2005-08-09 21:21 <a href="http://www.blogjava.net/jackybu/articles/9687.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>