﻿<?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-jinfeng_wang-随笔分类-hibernate</title><link>http://www.blogjava.net/jinfeng_wang/category/481.html</link><description>G-G-S,D-D-U!</description><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 10:06:06 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 10:06:06 GMT</pubDate><ttl>60</ttl><item><title>Mapping collections of value types </title><link>http://www.blogjava.net/jinfeng_wang/archive/2005/04/11/3132.html</link><dc:creator>jinfeng_wang</dc:creator><author>jinfeng_wang</author><pubDate>Mon, 11 Apr 2005 06:12:00 GMT</pubDate><guid>http://www.blogjava.net/jinfeng_wang/archive/2005/04/11/3132.html</guid><wfw:comment>http://www.blogjava.net/jinfeng_wang/comments/3132.html</wfw:comment><comments>http://www.blogjava.net/jinfeng_wang/archive/2005/04/11/3132.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jinfeng_wang/comments/commentRss/3132.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jinfeng_wang/services/trackbacks/3132.html</trackback:ping><description><![CDATA[<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: 13pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt">Mapping collections of value types (from 《hibernate in action》chapter 6)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><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: 12pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt; mso-bidi-font-size: 13.0pt">&nbsp;<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: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 13.0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">这里所举的例子，都是基于这样的一个情景，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt; mso-bidi-font-size: 13.0pt">Item</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 13.0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">中包含有</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt; mso-bidi-font-size: 13.0pt">Images</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 13.0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;<SPAN style="COLOR: red">set</SPAN> name="images" lazy="true" table="ITEM_IMAGE"&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;key column="ITEM_ID"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;element type="string" column="FILENAME" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;/set&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Courier; 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-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这里的</SPAN><SPAN lang=EN-US style="mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt">&lt;key&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">定义了外键</SPAN><SPAN lang=EN-US style="mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt">ITEM_ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US style="mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt">&lt;elememt&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">则声明了集合的内部数据元素。</SPAN><SPAN lang=EN-US style="mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt">&lt;set&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">定义的集合中不可以有重复的数据。</SPAN><SPAN lang=EN-US style="mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><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="mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt">&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;<SPAN style="COLOR: red">idbag</SPAN> name="images" lazy="true" table="ITEM_IMAGE"&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;collection-id type="long" column="ITEM_IMAGE_ID"&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 44.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 5.62; mso-char-indent-size: 7.95pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;generator class="sequence"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;/collection-id&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;key column="ITEM_ID"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;element type="string" column="FILENAME" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;/idbag&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Courier; 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: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">ITEM_IMAGE_ID</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">为主键，而</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">ITEM_ID</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">则为外键，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;element&gt;</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">元素不变，这就允许集合中的数据重复。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Courier; 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>&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;<SPAN style="COLOR: red">list</SPAN> name="images" lazy="true" table="ITEM_IMAGE"&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;key column="ITEM_ID"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;index column="POSITION"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;element type="string" column="FILENAME" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;/list&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-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这里为</SPAN><SPAN lang=EN-US>ITEM_IMAGE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">表添加了</SPAN><SPAN lang=EN-US>index</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，整个表的主键是</SPAN><SPAN lang=EN-US>ITEM_ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>POSITION</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">构成的复合主键，它允许集合中的数据根据</SPAN><SPAN lang=EN-US>POSITION</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进行排序。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;<SPAN style="COLOR: red">map</SPAN> name="images"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">lazy="true"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">table="ITEM_IMAGE"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: Courier; mso-font-kerning: 0pt">sort="natural"</SPAN><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;key column="ITEM_ID"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;index column="IMAGE_NAME" type="string"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;element type="string" column="FILENAME" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;/map&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Courier; 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-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这里为</SPAN><SPAN lang=EN-US>ITEM_IMAGE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">表添加了</SPAN><SPAN lang=EN-US>index</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，整个表的主键是</SPAN><SPAN lang=EN-US>ITEM_ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>POSITION</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">构成的复合主键，它允许集合中的数据根据</SPAN><SPAN lang=EN-US>sort</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">指定的内容进行排序，这里允许用户对</SPAN><SPAN lang=EN-US>sort</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进行定制。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;map name="images"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">lazy="true"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">table="ITEM_IMAGE"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: Courier; mso-font-kerning: 0pt">order-by="IMAGE_NAME asc"</SPAN><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;key column="ITEM_ID"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;index column="IMAGE_NAME" type="string"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;element type="string" column="FILENAME" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;/map&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-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这里采用的是</SPAN><SPAN lang=EN-US>orbder-by</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，注意</SPAN><SPAN lang=EN-US>sort</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>order-by</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的区别。</SPAN><SPAN lang=EN-US>Sort</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是在内存中使用</SPAN><SPAN lang=EN-US>comparator</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进行排序，而</SPAN><SPAN lang=EN-US>order-by</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">则是在数据库中使用</SPAN><SPAN lang=EN-US>SQL</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语句进行排序。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;<SPAN style="COLOR: red">set</SPAN> name="images"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">lazy="true"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">table="ITEM_IMAGE"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">order-by="IMAGE_NAME asc"&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;key column="ITEM_ID"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;<SPAN style="COLOR: red">composite-element</SPAN> class="<SPAN style="COLOR: red">Image</SPAN>"&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 40pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 5.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;property name="name" column="IMAGE_NAME" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 40pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 5.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;property name="filename" column="FILENAME" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 40pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 5.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;property name="sizeX" column="SIZEX" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 40pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 5.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;property name="sizeY" column="SIZEY" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;/composite-element&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;/set&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Courier; 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-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这里定义了一个新的</SPAN><SPAN lang=EN-US>Image</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类，它不是实体类</SPAN><SPAN lang=EN-US>(entity class)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，而是一个简单的值类型</SPAN><SPAN lang=EN-US>(value type)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，仅仅是父实体的一个</SPAN><SPAN lang=EN-US>component</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，其生命周期完全由包含其的父实体</SPAN><SPAN lang=EN-US> (parent entity) ITEM</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控制。这里</SPAN><SPAN lang=EN-US>Image</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类中包含有数据</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">ITEM_ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN style="mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"> </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">IMAGE_NAME</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">、</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">FILENAME</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">、</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">SIZEX</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">、和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">SIZEY</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; 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: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Courier; 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>&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;set name="images"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">lazy="true"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">table="ITEM_IMAGE"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">order-by="IMAGE_NAME asc"&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;key column="ITEM_ID"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;composite-element class="Image"&gt;<SPAN style="COLOR: red"><o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 40pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 5.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;parent name="item"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 40pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 5.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;property name="name" column="IMAGE_NAME" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 40pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 5.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;property name="filename" column="FILENAME" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 40pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 5.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;property name="sizeX" column="SIZEX" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 40pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 5.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;property name="sizeY" column="SIZEY" not-null="true"/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 3.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;/composite-element&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 8pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 1.0; mso-char-indent-size: 8.0pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;/set&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Courier; 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-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">前面定义的</SPAN><SPAN lang=EN-US>IMAGE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类中，并不包含有对父实体</SPAN><SPAN lang=EN-US>ITEM</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的引用。也就是说，只可以从</SPAN><SPAN lang=EN-US>ITEM</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中导航到</SPAN><SPAN lang=EN-US>IMAGE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中，但是在</SPAN><SPAN lang=EN-US>IMAGE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">无法得到</SPAN><SPAN lang=EN-US>ITEM</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。这可以通过</SPAN><SPAN lang=EN-US>&lt;parent&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">实现。这样</SPAN><SPAN lang=EN-US>IMAGE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类中就含有一个数据成员</SPAN><SPAN lang=EN-US>item</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，指向其父实体。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">但是，它是无法做到真正的双向导航的。例如：通过</SPAN><SPAN lang=EN-US>SQL</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">查询直接得到</SPAN><SPAN lang=EN-US>IMAGE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">后，但是如同普通的值类型一样，</SPAN><SPAN lang=EN-US>IMAGE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对象的</SPAN><SPAN lang=EN-US>item</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是</SPAN><SPAN lang=EN-US>null</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，你无法从</SPAN><SPAN lang=EN-US>component</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如此直接得到其父对象，这必须由通过父－子关系的</SPAN><SPAN lang=EN-US>entity</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">完成。</SPAN></P><img src ="http://www.blogjava.net/jinfeng_wang/aggbug/3132.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jinfeng_wang/" target="_blank">jinfeng_wang</a> 2005-04-11 14:12 <a href="http://www.blogjava.net/jinfeng_wang/archive/2005/04/11/3132.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate Transaction Theory released.</title><link>http://www.blogjava.net/jinfeng_wang/archive/2005/04/11/3115.html</link><dc:creator>jinfeng_wang</dc:creator><author>jinfeng_wang</author><pubDate>Mon, 11 Apr 2005 03:59:00 GMT</pubDate><guid>http://www.blogjava.net/jinfeng_wang/archive/2005/04/11/3115.html</guid><wfw:comment>http://www.blogjava.net/jinfeng_wang/comments/3115.html</wfw:comment><comments>http://www.blogjava.net/jinfeng_wang/archive/2005/04/11/3115.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jinfeng_wang/comments/commentRss/3115.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jinfeng_wang/services/trackbacks/3115.html</trackback:ping><description><![CDATA[<P><A href="http://www.blogjava.net/Files/jinfeng_wang/Hibernate%20Transactions%20Theory.zip">Hibernate Transaction Theory</A>&nbsp;released.<BR><BR>翻译节选于《Hibernate in Action》Chapter 5，本文未经原作者同意，其著作权为原作者所有，请勿任意转载。如有翻译问题，请与<A href="mailto:jinfeng_wang@msn.com">jinfeng_wang</A>联系。<BR><BR>下载地址： <A href="http://www.blogjava.net/Files/jinfeng_wang/Hibernate%20Transactions%20Theory.zip">http://www.blogjava.net/Files/jinfeng_wang/Hibernate%20Transactions%20Theory.zip</A></P><img src ="http://www.blogjava.net/jinfeng_wang/aggbug/3115.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jinfeng_wang/" target="_blank">jinfeng_wang</a> 2005-04-11 11:59 <a href="http://www.blogjava.net/jinfeng_wang/archive/2005/04/11/3115.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate transaction theory(2)</title><link>http://www.blogjava.net/jinfeng_wang/archive/2005/04/07/2945.html</link><dc:creator>jinfeng_wang</dc:creator><author>jinfeng_wang</author><pubDate>Thu, 07 Apr 2005 08:10:00 GMT</pubDate><guid>http://www.blogjava.net/jinfeng_wang/archive/2005/04/07/2945.html</guid><wfw:comment>http://www.blogjava.net/jinfeng_wang/comments/2945.html</wfw:comment><comments>http://www.blogjava.net/jinfeng_wang/archive/2005/04/07/2945.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/jinfeng_wang/comments/commentRss/2945.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jinfeng_wang/services/trackbacks/2945.html</trackback:ping><description><![CDATA[<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: 13pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt">5.2 Working with application transactions</SPAN></I></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">In our CaveatEmptor application, both the user who posted a comment and any system administrator can open an Edit Comment screen to delete or edit the text of a comment. Suppose two different administrators open the edit screen to view the same comment simultaneously. Both edit the comment text and submit their changes. At this point, we have three ways to handle the concurrent attempts to<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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">write to the database:<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: 6pt; FONT-FAMILY: ZapfDingbats; mso-font-kerning: 0pt">■ </SPAN><I><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">Last commit wins—</SPAN></I><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">Both updates succeed, and the second update overwrites the changes of the first. No error message is shown. <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: 6pt; FONT-FAMILY: ZapfDingbats; mso-font-kerning: 0pt">■ </SPAN><I><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">First commit wins—</SPAN></I><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">The first modification is persisted, and the user submitting the second change receives an error message. The user must restart the business process by retrieving the updated comment. This option is often called <I>optimistic locking</I>.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; TEXT-ALIGN: left; mso-list: l0 level1 lfo3; tab-stops: list 18.0pt; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 6pt; FONT-FAMILY: ZapfDingbats; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">■<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><I><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">Merge conflicting updates—</SPAN></I><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">The first modification is persisted, and the second modification may be applied selectively by 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 style="FONT-SIZE: 10pt; 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: 10pt; mso-font-kerning: 0pt">CavertEmptor</SPAN><SPAN style="FONT-SIZE: 10pt; 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: 10pt; mso-font-kerning: 0pt">cooment</SPAN><SPAN style="FONT-SIZE: 10pt; 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: 10pt; 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: 10pt; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; 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: 10pt; 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: 10pt; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; 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: 10pt; 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: 10pt; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; 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: 10pt; 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: 10pt; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">The first option, last commit wins, is problematic; the second user overwrites the changes of the first user without seeing the changes made by the first user or even knowing that they existed. In our example, this probably wouldn’t matter, but it would be unacceptable for some other kinds of data. The second and third options are usually acceptable for most kinds of data. From our point of view, the third option is just a variation of the second—instead of showing an error message, we show the message and then allow the user to manually merge changes. There is no single best solution. You must investigate your own business requirements to decide among these three options.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">上面的第一个方案是有问题的：第二个用户在不知道改变、甚至不知道第一个用户已经操作的情况下，直接重写了数据库。在我们的例子中，也许是没关系的，但是对于某些应用是不可接受的。第二个和第三个方案对于绝大部分数据是可以结束的。按照我们的观点，第三个是第二个的变种，它不是显式错误信息，而是给用户进行选择，允许用户手工合并对数据的修改。这里并没有一个最完美的解决方案。你必须根据你自己项目的需要自行选择方案。</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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">&nbsp;<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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">The first option happens by default if you don’t do anything special in your application</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">On the other hand, Hibernate can help you implement the second and third strategies, using <I>managed versioning </I>for <I>optimistic locking</I>.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如果在你的项目中不采取任何措施，那么会默认使用第一个方案。但是，</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">hibernate</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">可以通过使用对“乐观锁”使用“管理版本”，帮助你实现第二个或第三个方案。</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">&nbsp;<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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">Managed versioning relies on either a version number that is incremented or a timestamp that is updated to the current time, every time an object is modified. For Hibernate managed versioning, we must add a new property to our </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">Comment </SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">class and map it as a version number using the </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;version&gt; </SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">tag. First, let’s look at the changes to the </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">Comment </SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">class:<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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">public class Comment {<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: 8pt; FONT-FAMILY: Courier; 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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">private int version;<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: 8pt; FONT-FAMILY: Courier; 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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">void setVersion(int version) {<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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">this.version = version;<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: 8pt; FONT-FAMILY: Courier; 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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">int getVersion() {<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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">return version;<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: 8pt; FONT-FAMILY: Courier; 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: 8pt; FONT-FAMILY: Courier; 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-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">“管理版本”使用版本号或者时间戳对数据进行标记，每次数据在更新的时候，它都会被更新。对于</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">hibernate</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，我们必须给</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">Comment</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类，添加新的属性，然后使用</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">&lt;version&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">标签对其进行映射。首先让我们来看一下</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">Comment</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类的改变：</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; 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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">public class Comment {<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="mso-bidi-font-size: 10.5pt; 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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">private int version;<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="mso-bidi-font-size: 10.5pt; 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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">void setVersion(int version) {<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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">this.version = version;<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="mso-bidi-font-size: 10.5pt; 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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">int getVersion() {<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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">return version;<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="mso-bidi-font-size: 10.5pt; 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="mso-bidi-font-size: 10.5pt; 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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&nbsp;<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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">You can also use a public scope for the setter and getter methods. The </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;version&gt; </SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">property mapping must come immediately after the identifier property mapping in the mapping file for the </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">Comment </SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">class:<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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;class name="Comment" table="COMMENTS"&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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;version name="version" column="VERSION"/&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: 8pt; FONT-FAMILY: Courier; 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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;/&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: 8pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">你可以使用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">public</SPAN><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">setter</SPAN><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">或</SPAN><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">getter</SPAN><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">方法，这里的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;version&gt;</SPAN><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">映射的定义必须紧跟在映射文件中</SPAN><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">comment</SPAN><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">类的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">id</SPAN><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">后面。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 8pt; FONT-FAMILY: Courier; 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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;class name="Comment" table="COMMENTS"&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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;version name="version" column="VERSION"/&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: 8pt; FONT-FAMILY: Courier; 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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&lt;/&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: 10pt; mso-font-kerning: 0pt">&nbsp;<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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">You don’t need to set the value of the version or timestamp property yourself; Hibernate will initialize the value when you first save a </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">Comment</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">, and increment or reset it whenever the object is modified. Whenever Hibernate updates a comment, it uses the version column in the </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; mso-font-kerning: 0pt">SQL</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">WHERE </SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">clause:<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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">update COMMENTS set COMMENT_TEXT='New comment text', VERSION=3<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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">where COMMENT_ID=123 and VERSION=2<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-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">你无需自己手工去修改这里的版本号或者时间戳，</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">hibernate</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">将会在你保存</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">Commment</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的时候对其初始化，然后在对象每次被更新的时候即使更新其版本号和时间戳。无论</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">hibernate</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">何时更新</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">comment,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">它都会在</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">SQL</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语句的</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">where</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">后面加上</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">version</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">列：</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; 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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">update COMMENTS set COMMENT_TEXT='New comment text', VERSION=3<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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">where COMMENT_ID=123 and VERSION=2<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: 8pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">&nbsp;<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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">If another application transaction would have updated the same item since it was read by the current application transaction, the </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">VERSION </SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">column would not contain the value 2, and the row would not be updated. Hibernate would check the row count returned by the </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; mso-font-kerning: 0pt">JDBC </SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">driver—which in this case would be the number of rows updated, zero—and throw a </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">StaleObjectStateException</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">. Using this exception, we might show the user of the second application transaction an error message (“You have been working with stale data because another user modified it!”) and let the <I>first commit win</I>. Alternatively, we could catch the exception and show the second user a new screen, allowing the user to manually <I>merge changes </I>between the two versions.</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; 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-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如果某个事务已经更新了某个数据，而另外一个事务也准备更新此数据，但是当时它取得数据的版本号是</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">2</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，而此时由于该数据已经被更新过，新版本号是</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">3</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，因此将无法更新数据库中的任何的数据。这时候</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">hibernate</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">将会检查</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">jdbc</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的返回行数－如果未有任何数据行被更新，则返回</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">0</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; 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: 9.5pt; FONT-FAMILY: Courier; mso-font-kerning: 0pt">StaleObjectStateException</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier">异常。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9.5pt; FONT-FAMILY: Courier; 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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">&nbsp;<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="mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">Using this exception, we might show the user of the second application transaction an error message (“You have been working with stale data because another user modified it!”) and let the <I>first commit win</I>. Alternatively, we could catch the exception and show the second user a new screen, allowing the user to manually <I>merge changes </I>between the two versions.</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">使用此异常，就可以给第二个应用事务的用户显式错误信息（你正使用的数据已经被其他人更新了），让第一个</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">提交者获胜</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Courier; mso-hansi-font-family: Courier; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">。当然，你也可以捕捉异常，然后给第二个用户一个新的平面，允许它手工合并这两个版本的数据。</SPAN><img src ="http://www.blogjava.net/jinfeng_wang/aggbug/2945.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jinfeng_wang/" target="_blank">jinfeng_wang</a> 2005-04-07 16:10 <a href="http://www.blogjava.net/jinfeng_wang/archive/2005/04/07/2945.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate Transactions theory (1)</title><link>http://www.blogjava.net/jinfeng_wang/archive/2005/04/07/2916.html</link><dc:creator>jinfeng_wang</dc:creator><author>jinfeng_wang</author><pubDate>Thu, 07 Apr 2005 06:03:00 GMT</pubDate><guid>http://www.blogjava.net/jinfeng_wang/archive/2005/04/07/2916.html</guid><wfw:comment>http://www.blogjava.net/jinfeng_wang/comments/2916.html</wfw:comment><comments>http://www.blogjava.net/jinfeng_wang/archive/2005/04/07/2916.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jinfeng_wang/comments/commentRss/2916.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jinfeng_wang/services/trackbacks/2916.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;5.1.4 理解事务隔离级别&nbsp;问题The ANSI SQL standard defines the standard transaction isolation levels in terms of which of these phenomena are permissible:ANSI SQL分别针对以下可能出现的问题，定义了不同的标准事务隔离级别。...&nbsp;&nbsp;<a href='http://www.blogjava.net/jinfeng_wang/archive/2005/04/07/2916.html'>阅读全文</a><img src ="http://www.blogjava.net/jinfeng_wang/aggbug/2916.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jinfeng_wang/" target="_blank">jinfeng_wang</a> 2005-04-07 14:03 <a href="http://www.blogjava.net/jinfeng_wang/archive/2005/04/07/2916.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate's session和《企业应用架构模式》第11章</title><link>http://www.blogjava.net/jinfeng_wang/archive/2005/04/05/2869.html</link><dc:creator>jinfeng_wang</dc:creator><author>jinfeng_wang</author><pubDate>Tue, 05 Apr 2005 03:13:00 GMT</pubDate><guid>http://www.blogjava.net/jinfeng_wang/archive/2005/04/05/2869.html</guid><wfw:comment>http://www.blogjava.net/jinfeng_wang/comments/2869.html</wfw:comment><comments>http://www.blogjava.net/jinfeng_wang/archive/2005/04/05/2869.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jinfeng_wang/comments/commentRss/2869.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jinfeng_wang/services/trackbacks/2869.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><B><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN">hibernate实现了《企业应用架构模式》第11章的许多模式，结合起来则更能理解hibernate的session的作用。只是双方的一些术语未必能直接对应上。<BR>＝＝＝＝＝＝＝＝＝＝＝<BR>Session接口<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN">Session（会话）接口是Hibernate应用使用的主要接口。会话接口的实例是轻量级的并且创建与销毁的代价也不昂贵。这很重要因为你的应用可能始终在创建与销毁会话，可能每一次请求都会如此。Hibernate会话并不是线程安全的因此应该被设计为每次只能在一个线程中使用。<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN">&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN">Hibernate会话是一个介于连接和事务之间的概念。<FONT color=#ff1493>你可以简单地认为会话是对于一个单独的工作单元已装载对象的缓存或集合。Hibernate可以检测到这个工作单元中对象的改变。</FONT>我们有时也将会话称为持续性管理器，因为它也是与持续性有关的操作例如存储和取出对象的接口。注意，Hibernate会话与Web层的HttpSession没有任何关系。当我们在本书中使用会话时，我们指的是Hibernate会话。为了区别，有时我们将HttpSession对象称为用户会话。<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN">&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><B><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">SessionFactory</SPAN></B><B><SPAN style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">接口</SPAN></B><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN">应用从</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-size: 9.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">SessionFactory</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN; mso-bidi-font-size: 9.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">（会话工厂）里获得会话实例。与会话接口相比，这个对象不够令人兴奋。</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN">&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN">会话工厂当然不是轻量级的！它打算在多个应用线程间进行共享。典型地，<FONT color=#ff1493>整个应用只有唯一的一个会话工厂——例如在应用初始化时被创建。然而，如果你的应用使用<SPAN lang=EN-US>Hibernate访问多个数据库，你需要对每一个数据库使用一个会话工厂。<o:p></o:p></SPAN></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN">&nbsp;<o:p></o:p></SPAN></P><SPAN style="FONT-SIZE: 10.5pt; FONT-FAMILY: 宋体; mso-fareast-language: ZH-CN; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-bidi-language: AR-SA">会话工厂缓存了生成的<SPAN lang=EN-US>SQL语句和Hibernate在运行时使用的映射元数据。它也保存了在一个工作单元中读入的数据并且可能在以后的工作单元中被重用（只有类和集合映射指定了这种二级缓存是想要的时才会如此）。<BR><BR><BR></SPAN></SPAN><img src ="http://www.blogjava.net/jinfeng_wang/aggbug/2869.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jinfeng_wang/" target="_blank">jinfeng_wang</a> 2005-04-05 11:13 <a href="http://www.blogjava.net/jinfeng_wang/archive/2005/04/05/2869.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate's casade table.</title><link>http://www.blogjava.net/jinfeng_wang/archive/2005/04/03/2798.html</link><dc:creator>jinfeng_wang</dc:creator><author>jinfeng_wang</author><pubDate>Sun, 03 Apr 2005 13:43:00 GMT</pubDate><guid>http://www.blogjava.net/jinfeng_wang/archive/2005/04/03/2798.html</guid><wfw:comment>http://www.blogjava.net/jinfeng_wang/comments/2798.html</wfw:comment><comments>http://www.blogjava.net/jinfeng_wang/archive/2005/04/03/2798.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jinfeng_wang/comments/commentRss/2798.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jinfeng_wang/services/trackbacks/2798.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="COLOR: blue; mso-ansi-language: EN">&nbsp;</P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">&lt;?</SPAN><SPAN style="COLOR: #ff00ff">xml&nbsp;version="1.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: #ff00ff">DOCTYPE&nbsp;hibernate-mapping&nbsp;PUBLIC&nbsp;"-//Hibernate/Hibernate&nbsp;Mapping&nbsp;DTD&nbsp;2.0//EN"<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">hibernate-mapping</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">class&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="com.oreilly.hh.Album"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;table</SPAN><SPAN style="COLOR: #0000ff">="ALBUM"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">meta&nbsp;</SPAN><SPAN style="COLOR: #ff0000">attribute</SPAN><SPAN style="COLOR: #0000ff">="class-description"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Represents&nbsp;an&nbsp;album&nbsp;in&nbsp;the&nbsp;music&nbsp;database,&nbsp;an&nbsp;organized&nbsp;list&nbsp;of&nbsp;tracks.<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@author&nbsp;Jim&nbsp;Elliott&nbsp;(with&nbsp;help&nbsp;from&nbsp;Hibernate)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">meta</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">id&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="id"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;type</SPAN><SPAN style="COLOR: #0000ff">="int"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;column</SPAN><SPAN style="COLOR: #0000ff">="ALBUM_ID"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">meta&nbsp;</SPAN><SPAN style="COLOR: #ff0000">attribute</SPAN><SPAN style="COLOR: #0000ff">="scope-set"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">protected</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">meta</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">generator&nbsp;</SPAN><SPAN style="COLOR: #ff0000">class</SPAN><SPAN style="COLOR: #0000ff">="native"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">id</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="title"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;type</SPAN><SPAN style="COLOR: #0000ff">="string"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">meta&nbsp;</SPAN><SPAN style="COLOR: #ff0000">attribute</SPAN><SPAN style="COLOR: #0000ff">="use-in-tostring"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">true</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">meta</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">column&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="TITLE"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;not-null</SPAN><SPAN style="COLOR: #0000ff">="true"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;index</SPAN><SPAN style="COLOR: #0000ff">="ALBUM_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>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">property</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="numDiscs"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;type</SPAN><SPAN style="COLOR: #0000ff">="integer"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;not-null</SPAN><SPAN style="COLOR: #0000ff">="true"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">set&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="artists"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;table</SPAN><SPAN style="COLOR: #0000ff">="ALBUM_ARTISTS"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">key&nbsp;</SPAN><SPAN style="COLOR: #ff0000">column</SPAN><SPAN style="COLOR: #0000ff">="ALBUM_ID"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">many-to-many&nbsp;</SPAN><SPAN style="COLOR: #ff0000">class</SPAN><SPAN style="COLOR: #0000ff">="com.oreilly.hh.Artist"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;column</SPAN><SPAN style="COLOR: #0000ff">="ARTIST_ID"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">set</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">set&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="comments"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;table</SPAN><SPAN style="COLOR: #0000ff">="ALBUM_COMMENTS"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">key&nbsp;</SPAN><SPAN style="COLOR: #ff0000">column</SPAN><SPAN style="COLOR: #0000ff">="ALBUM_ID"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">element&nbsp;</SPAN><SPAN style="COLOR: #ff0000">column</SPAN><SPAN style="COLOR: #0000ff">="COMMENT"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;type</SPAN><SPAN style="COLOR: #0000ff">="string"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">set</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">list&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="tracks"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;table</SPAN><SPAN style="COLOR: #0000ff">="ALBUM_TRACKS"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;cascade</SPAN><SPAN style="COLOR: #0000ff">="all"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">meta&nbsp;</SPAN><SPAN style="COLOR: #ff0000">attribute</SPAN><SPAN style="COLOR: #0000ff">="use-in-tostring"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">true</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">meta</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">key&nbsp;</SPAN><SPAN style="COLOR: #ff0000">column</SPAN><SPAN style="COLOR: #0000ff">="ALBUM_ID"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">index&nbsp;</SPAN><SPAN style="COLOR: #ff0000">column</SPAN><SPAN style="COLOR: #0000ff">="POS"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">composite-element&nbsp;</SPAN><SPAN style="COLOR: #ff0000">class</SPAN><SPAN style="COLOR: #0000ff">="com.oreilly.hh.AlbumTrack"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">many-to-one&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="track"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;class</SPAN><SPAN style="COLOR: #0000ff">="com.oreilly.hh.Track"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;cascade</SPAN><SPAN style="COLOR: #0000ff">="all"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">meta&nbsp;</SPAN><SPAN style="COLOR: #ff0000">attribute</SPAN><SPAN style="COLOR: #0000ff">="use-in-tostring"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">true</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">meta</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">column&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="TRACK_ID"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">many-to-one</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="disc"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;type</SPAN><SPAN style="COLOR: #0000ff">="integer"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;not-null</SPAN><SPAN style="COLOR: #0000ff">="true"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="positionOnDisc"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;type</SPAN><SPAN style="COLOR: #0000ff">="integer"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;not-null</SPAN><SPAN style="COLOR: #0000ff">="true"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">composite-element</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">list</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="added"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;type</SPAN><SPAN style="COLOR: #0000ff">="date"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">meta&nbsp;</SPAN><SPAN style="COLOR: #ff0000">attribute</SPAN><SPAN style="COLOR: #0000ff">="field-description"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">When&nbsp;the&nbsp;album&nbsp;was&nbsp;created</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">meta</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">property</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">class</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">hibernate-mapping</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></DIV></DIV><BR>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><BR>&nbsp;</P>
<P class=doctext style="MARGIN: auto 0cm"><SPAN lang=EN-US>The </SPAN><FONT size=3><FONT face="Times New Roman"><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">cascade</SPAN></TT><SPAN lang=EN-US> attribute tells Hibernate that you want operations performed on a 'parent' object to be transitively applied to its 'child' or 'dependent' objects. It's applicable to all forms of collections and associations. There are several possible values to choose among. The most common are </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">none</SPAN></TT><SPAN lang=EN-US> (the default), </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">save-update, delete</SPAN></TT><SPAN lang=EN-US>, and </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">all</SPAN></TT><SPAN lang=EN-US> (which combines </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">save-update</SPAN></TT><SPAN lang=EN-US> and </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">delete</SPAN></TT><SPAN lang=EN-US>). You can also change the default from </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">none</SPAN></TT><SPAN lang=EN-US> to </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">save-update</SPAN></TT><SPAN lang=EN-US> throughout your entire mapping document by supplying a </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">default-cascade</SPAN></TT><SPAN lang=EN-US> attribute in the </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">hibernate-mapping</SPAN></TT><SPAN lang=EN-US> tag itself.<BR></SPAN></FONT></FONT></P>
<P class=doctext style="MARGIN: auto 0cm"><SPAN lang=EN-US>级联（cascade）属性告诉hibernate：所有对父对象的操作都将付诸于子对象和依赖对象上。级联可以应用于各种形式的集合和关联中。这里级联的具体取值可以有多个，最常用的有：none(默认值)，save-update,delete和all（其中包含了save-update和delete）。你可以在hibernate-mappiong tag中通过设置default-casade属性，修改这里的casade的默认值，例如修改其默认值为save-update。<BR><BR>In our example, we want the tracks owned by an album to be automatically managed by the album, so that when we delete the album, its tracks are deleted. Note that we need to apply the </SPAN><FONT size=3><FONT face="Times New Roman"><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">cascade</SPAN></TT><SPAN lang=EN-US> attribute both to the </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">tracks</SPAN></TT><SPAN lang=EN-US> collection and its constituent </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">track</SPAN></TT><SPAN lang=EN-US> element to achieve this. Also, by using a </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">cascade</SPAN></TT><SPAN lang=EN-US> value of </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">all</SPAN></TT><SPAN lang=EN-US>, we eliminate the need to explicitly save any </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">Track</SPAN></TT><SPAN lang=EN-US> objects we create for the album—the </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'">addAlbumTrack()</SPAN></TT><SPAN lang=EN-US> method of <A href="http://www.blogjava.net/jinfeng_wang/admin/hibernate-CHP-5-SECT-3.html#hibernate-CHP-5-EXAMPLE-7">Example 5-7</A> no longer needs the line:<BR><BR>&nbsp;&nbsp; session.save(track);<BR><BR>在我们的例子中，我们希望track完全由album自动掌管，当我们删除album时，它的track也会被删除。注意，这里我们需要在两处设置casade属性实现此任务：tracks集合和它的组成元素track。同样，由于我们讲casade设置为all，那么不再需要显式的保存为album创建的track对象，5-7例子中的addAlbumTrack方法不再需要调用session.save(track)方法。<BR><BR></SPAN></FONT></FONT></P>
<P class=doctext style="MARGIN: auto 0cm"><SPAN lang=EN-US>By telling Hibernate that it's fully responsible for the relationship between an album and its track, we enable it to persist tracks when they're added to the album as well as delete them when the album itself is deleted.<BR><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P><FONT size=3><SPAN lang=EN-US style="FONT-SIZE: 10.5pt; FONT-FAMILY: 'Times New Roman'; mso-ansi-language: EN-US; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体; mso-font-kerning: 1.0pt; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><BR>通过高知hibernate，由其完全负责album和track直接的关系，可以保证track在加入到album时自动保存，在album删除时自动删除。<BR><BR>Delegating this sort of bookkeeping to the mapping layer can be very convenient, freeing you to focus on more abstract and important tasks, so it is worth using when appropriate. It's reminiscent of the liberation provided by Java's pervasive garbage collection, but it can't be as comprehensive because there is no definitive way to know when you're finished with persistent data by performing reachability analysis; you need to indicate it by calling </SPAN><FONT face="Times New Roman"><TT><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-font-kerning: 1.0pt; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">delete()</SPAN></TT><SPAN lang=EN-US style="FONT-SIZE: 10.5pt; FONT-FAMILY: 'Times New Roman'; mso-ansi-language: EN-US; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体; mso-font-kerning: 1.0pt; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"> and establishing lifecycle connections. The trade-off between flexibility and simple automation is yours to make, based on the nature of your data and the needs of your project.<BR>将这样的任务完全交代给持久层使得我们的任务更加的简便，可以让我们将注意力投注于更抽象更重要的业务，因此恰当的使用casade是值得的，这就像java的垃圾收集一样，使我们的工作得到了一定的解放。但是，由于目前并没有任何的可达性分析方法，可以确切的知道“你是否已经不再需要此持久层数据”，因此你必须调用delete()方法，才能建立起此生命周期链接。你必须依据你的数据的特点和项目的需要，在灵活性和自动化的简单性进行权衡。<BR><BR><FONT size=3>For example, if you use <TT>Collections</TT> methods to remove a <TT>Track</TT> from an <TT>Album's tracks</TT> property, this breaks the link between the <TT>Album</TT> and <TT>Track</TT> but does <SPAN class=docEmphasis>not</SPAN> actually delete the <TT>Track</TT> record. Even if you later delete the entire <TT>Album</TT>, this <TT>Track</TT> will remain, because it wasn't linked to the <TT>Album</TT> at the time that it was deleted. <BR>例如：如果你使用Album的tracks collection的remove方法，删除一个track，这将会移除album和track直接的链接，但是它并没真正的删除track记录。即使你后来整个的删除album，这个track也将会继续保留着，因为此时该track已经和album失去了链接。<BR>//（ 译者注）这里的含义是：级联是有范围的，表ALBUM和表TRACK之间通过表Album_Tracks建立了联系。在albu.hbm.xml中，进行了级联配置，使得Album和ALBUM_TRACKS之间级联。当删除某个album时，那么相应的ALBUM_TRACKS中的记录也将删除。如果对album中的tracks执行remove方法时，那么ALBUM_TRACKS中的相应记录也将删除。但是这里的级联却无法延伸到表TRACK中，TRACK表中的记录将安然无恙，依然存在。<BR>//原文中的tracks出现在两处，一个是Album对象的属性，其对应着ALBUM_TRACKS的记录。一个则是单独的TRACK表，所以表述十分容易引起混淆。</FONT></SPAN></FONT></FONT><img src ="http://www.blogjava.net/jinfeng_wang/aggbug/2798.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jinfeng_wang/" target="_blank">jinfeng_wang</a> 2005-04-03 21:43 <a href="http://www.blogjava.net/jinfeng_wang/archive/2005/04/03/2798.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate's transient object and persistent object</title><link>http://www.blogjava.net/jinfeng_wang/archive/2005/04/01/2706.html</link><dc:creator>jinfeng_wang</dc:creator><author>jinfeng_wang</author><pubDate>Fri, 01 Apr 2005 06:21:00 GMT</pubDate><guid>http://www.blogjava.net/jinfeng_wang/archive/2005/04/01/2706.html</guid><wfw:comment>http://www.blogjava.net/jinfeng_wang/comments/2706.html</wfw:comment><comments>http://www.blogjava.net/jinfeng_wang/archive/2005/04/01/2706.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jinfeng_wang/comments/commentRss/2706.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jinfeng_wang/services/trackbacks/2706.html</trackback:ping><description><![CDATA[<P class=docText></P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">package&nbsp;com.oreilly.hh;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>import&nbsp;net.sf.hibernate.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>import&nbsp;net.sf.hibernate.cfg.Configuration;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>import&nbsp;java.sql.Time;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>import&nbsp;java.util.Date;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_142_208_Open_Image onclick="this.style.display='none'; Codehighlighter1_142_208_Open_Text.style.display='none'; Codehighlighter1_142_208_Closed_Image.style.display='inline'; Codehighlighter1_142_208_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_142_208_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_142_208_Closed_Text.style.display='none'; Codehighlighter1_142_208_Open_Image.style.display='inline'; Codehighlighter1_142_208_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_142_208_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_142_208_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;Create&nbsp;sample&nbsp;data,&nbsp;letting&nbsp;Hibernate&nbsp;persist&nbsp;it&nbsp;for&nbsp;us.<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_234_2291_Open_Image onclick="this.style.display='none'; Codehighlighter1_234_2291_Open_Text.style.display='none'; Codehighlighter1_234_2291_Closed_Image.style.display='inline'; Codehighlighter1_234_2291_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_234_2291_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_234_2291_Closed_Text.style.display='none'; Codehighlighter1_234_2291_Open_Image.style.display='inline'; Codehighlighter1_234_2291_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;CreateTest&nbsp;</SPAN><SPAN id=Codehighlighter1_234_2291_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_234_2291_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG id=Codehighlighter1_297_2289_Open_Image onclick="this.style.display='none'; Codehighlighter1_297_2289_Open_Text.style.display='none'; Codehighlighter1_297_2289_Closed_Image.style.display='inline'; Codehighlighter1_297_2289_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_297_2289_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_297_2289_Closed_Text.style.display='none'; Codehighlighter1_297_2289_Open_Image.style.display='inline'; Codehighlighter1_297_2289_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main(String&nbsp;args[])&nbsp;throws&nbsp;Exception&nbsp;</SPAN><SPAN id=Codehighlighter1_297_2289_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_297_2289_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Create&nbsp;a&nbsp;configuration&nbsp;based&nbsp;on&nbsp;the&nbsp;properties&nbsp;file&nbsp;we've&nbsp;put<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;in&nbsp;the&nbsp;standard&nbsp;place.</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Configuration&nbsp;config&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Configuration();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Tell&nbsp;it&nbsp;about&nbsp;the&nbsp;classes&nbsp;we&nbsp;want&nbsp;mapped,&nbsp;taking&nbsp;advantage&nbsp;of<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;the&nbsp;way&nbsp;we've&nbsp;named&nbsp;their&nbsp;mapping&nbsp;documents.</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config.addClass(Track.</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Get&nbsp;the&nbsp;session&nbsp;factory&nbsp;we&nbsp;can&nbsp;use&nbsp;for&nbsp;persistence</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SessionFactory&nbsp;sessionFactory&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;config.buildSessionFactory();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Ask&nbsp;for&nbsp;a&nbsp;session&nbsp;using&nbsp;the&nbsp;JDBC&nbsp;information&nbsp;we've&nbsp;configured</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Session&nbsp;session&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;sessionFactory.openSession();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Transaction&nbsp;tx&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">;<BR><IMG id=Codehighlighter1_932_1919_Open_Image onclick="this.style.display='none'; Codehighlighter1_932_1919_Open_Text.style.display='none'; Codehighlighter1_932_1919_Closed_Image.style.display='inline'; Codehighlighter1_932_1919_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_932_1919_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_932_1919_Closed_Text.style.display='none'; Codehighlighter1_932_1919_Open_Image.style.display='inline'; Codehighlighter1_932_1919_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_932_1919_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_932_1919_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Create&nbsp;some&nbsp;data&nbsp;and&nbsp;persist&nbsp;it</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;session.beginTransaction();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Track&nbsp;track&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Track(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Russian&nbsp;Trance</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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 style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">vol2/album610/track02.mp3</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Time.valueOf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">00:03:30</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">),&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Date(),<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;(</SPAN><SPAN style="COLOR: #0000ff">short</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.save(track);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;track&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Track(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Video&nbsp;Killed&nbsp;the&nbsp;Radio&nbsp;Star</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">vol2/album611/track12.mp3</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Time.valueOf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">00:03:49</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">),&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Date(),<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;(</SPAN><SPAN style="COLOR: #0000ff">short</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.save(track);&nbsp;<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;track&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Track(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Gravity's&nbsp;Angel</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">vol2/album175/track03.mp3</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Time.valueOf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">00:06:06</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">),&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Date(),<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;(</SPAN><SPAN style="COLOR: #0000ff">short</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.save(track);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;We're&nbsp;done;&nbsp;make&nbsp;our&nbsp;changes&nbsp;permanent</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx.commit();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG id=Codehighlighter1_1941_2116_Open_Image onclick="this.style.display='none'; Codehighlighter1_1941_2116_Open_Text.style.display='none'; Codehighlighter1_1941_2116_Closed_Image.style.display='inline'; Codehighlighter1_1941_2116_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1941_2116_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1941_2116_Closed_Text.style.display='none'; Codehighlighter1_1941_2116_Open_Image.style.display='inline'; Codehighlighter1_1941_2116_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">&nbsp;(Exception&nbsp;e)&nbsp;</SPAN><SPAN id=Codehighlighter1_1941_2116_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_1941_2116_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG id=Codehighlighter1_1971_2085_Open_Image onclick="this.style.display='none'; Codehighlighter1_1971_2085_Open_Text.style.display='none'; Codehighlighter1_1971_2085_Closed_Image.style.display='inline'; Codehighlighter1_1971_2085_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1971_2085_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1971_2085_Closed_Text.style.display='none'; Codehighlighter1_1971_2085_Open_Image.style.display='inline'; Codehighlighter1_1971_2085_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(tx&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_1971_2085_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_1971_2085_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Something&nbsp;went&nbsp;wrong;&nbsp;discard&nbsp;all&nbsp;partial&nbsp;changes</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx.rollback();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">throw</SPAN><SPAN style="COLOR: #000000">&nbsp;e;<BR><IMG id=Codehighlighter1_2126_2214_Open_Image onclick="this.style.display='none'; Codehighlighter1_2126_2214_Open_Text.style.display='none'; Codehighlighter1_2126_2214_Closed_Image.style.display='inline'; Codehighlighter1_2126_2214_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_2126_2214_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2126_2214_Closed_Text.style.display='none'; Codehighlighter1_2126_2214_Open_Image.style.display='inline'; Codehighlighter1_2126_2214_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">finally</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_2126_2214_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_2126_2214_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;No&nbsp;matter&nbsp;what,&nbsp;close&nbsp;the&nbsp;session</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.close();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Clean&nbsp;up&nbsp;after&nbsp;ourselves</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sessionFactory.close();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV>
<P class=docText><BR><BR>When it comes time to actually perform persistence, we ask the <TT>SessionFactory</TT> to open a <TT>Session</TT> for us (<A class=docLink href="mk:@MSITStore:E:\document\Java\hibernate\book\hibernate.a.developers.notebook.chm::/0596006969/hibernate-CHP-3-SECT-1.html#hibernate-CHP-3-EXAMPLE-1">line 27</A>), which establishes a JDBC connection to the database, and provides us with a context in which we can create, obtain, manipulate, and delete persistent objects. As long as the session is open, a connection to the database is maintained, and changes to the persistent objects associated with the session are tracked so they can be applied to the database when the session is closed. Conceptually you can think of a session as a 'large scale transaction' between the persistent objects and the database, which may encompass several database-level transactions. Like a database transaction, though, you should not think about keeping your Hibernate session open over long periods of application existence (such as while you're waiting for user input). A single session is used for a specific and bounded operation in the application, something like populating the user interface or making a change that has been committed by the user. The next operation will use a new session. Also note that <TT>Session</TT> objects are not thread safe, so they cannot be shared between threads. Each thread needs to obtain its own session from the factory.<BR></P>
<P class=docText>现在开始讲述“持久化操作”。在第27行，使用SessionFactory打开了Session，建立了数据库JDBC的连接，（该连接已经进行了初始化），我们可以借用它进行一系列的操作，例如：创建、获取、操纵、删除持久化对象。只要session处于open状态，那么对数据库的连接就一直建立着，所有对已经和session绑定的持久化对象的操作都会被记录下来，并且在session关闭的时候，所有的这些操作就会被更新到数据库中。从概念上理解，你可以将session看作一个在持久化对象和数据库之间的“大规模的事务”，它可以跨越几个数据库事务。正如数据库事务那样，你不该在程序中将hibernate session长时间的打开着（例如当程序在等待用户输入的时候），程序中的每个单独的操作都该采用单独的session，例如在“弹出用户界面，或者将用户的修改提交到数据库”中，就该使用两个session。同样需要注意的是，session是线程不安全的，因此无法在线程之间共享session，每个线程都应该使用SessionFactory建立自己的sess。<BR><BR>We need to look more closely at the lifecycle of mapped objects in Hibernate, and how this relates to sessions, because the terminology is rather specific and the concepts are quite important. A mapped object such as an instance of our <TT>Track</TT> class moves back and forth between two states with respect to Hibernate: <FONT color=#008000><SPAN class=docEmphasis>transient</SPAN> </FONT>and <SPAN class=docEmphasis><FONT color=#008000>persistent</FONT></SPAN>. An object that is transient is not associated with any session. When you first create a <TT>Track</TT> instance using <TT>new()</TT>, it is transient; unless you tell Hibernate to persist it, the object will vanish when it is garbage collected or your application terminates.<BR></P>
<P class=docText>下面我们对hibernate中被映射对象的声明周期进行说明，因为被映射对象相关的术语是相当特别的、它的概念也是很重要的。在hibernate中，被映射对象（例如程序中的Track对象）会在两个状态中不断的来回切换：临时（transient）状态和持久（persistent）状态。未曾与session绑定的对象就处于临时状态，例如程序中刚开始new出来的Track对象，它就处于临时状态。除非你通知hibernate对它进行持久化，否则整个对象就会在垃圾收集器回收或者程序结束的时候消逝。<BR><BR>Passing a transient mapped object to a <TT>Session</TT>'s <TT>save()</TT> method causes it to become persistent. It will survive garbage collection and termination of the Java VM, staying available until it is explicitly deleted. (There is a related distinction between <SPAN class=docEmphasis><FONT color=#006400>entities</FONT></SPAN> and <SPAN class=docEmphasis>values</SPAN> discussed at the beginning of <A class=docLink href="mk:@MSITStore:E:\document\Java\hibernate\book\hibernate.a.developers.notebook.chm::/0596006969/hibernate-APP-A.html#hibernate-APP-A">Appendix A</A>. Mapped objects that have been persisted are called entities, even if they do not currently exist as an instance in any virtual machine.) If you've got a persistent object and you call <TT>Session</TT>'s <TT>delete()</TT> method on it, the object transitions back to a transient state. The object still exists as an instance in your application, but it is no longer going to stay around unless you change your mind and save it again; it's ceased being an entity. <BR><BR>但是，将一个临时对象使用session的save方法保存之后，它就处于持久状态，即使在垃圾回事或者Java VM结束之后，它都一直存在，知道该对象被明确删除为止（在附录A开头有相关“entities和values区别”的讨论，即使其在虚拟机中并不真实存在着一个对象与之相对应）。当你对持久化对象调用session.delete()方法时，该对象又变为临时状态。虽然该对象仍然在程序中做为一个实例存在着，但是除非你改变主意再次将其持久化，那么它将很快的消逝，它的“实体(entity)”也就愕然而之。</P>
<P class=docText>On the other hand, and this point is worth extra emphasis, if you haven't deleted an object (so it's still persistent), when you change its properties there is no need to save it again for those changes to be reflected in the database. Hibernate automatically tracks changes to any persistent objects and flushes those changes to the database at appropriate times. When you close the session, any pending changes are flushed.<BR><BR>另一方面，需要强调的是：如若你不删除某持久化对象，那么当你改变其属性时，并不需要显示的对其的改变进行保存。hibernate将会自动的跟踪到你对持久化对象的改变，然后在适当的时候将这些改变填入到数据库中。<BR><BR>An important but subtle point concerns the status of persistent objects you worked with in a session that has been closed, such as after you run a query to find all entities matching some criteria (you'll see how to do this in the upcoming section, '<A class=docLink href="mk:@MSITStore:E:\document\Java\hibernate\book\hibernate.a.developers.notebook.chm::/0596006969/hibernate-CHP-3-SECT-2.html#hibernate-CHP-3-SECT-2">Finding Persistent Objects</A>'). As noted above, you don't want to keep this session around longer than necessary to perform the database operation, so you close it once your queries are finished. What's the deal with the mapped objects you've loaded at this point? Well, they were persistent while the session was around, but once they are no longer associated with an active session (in this case because the session has been closed) they are not persistent any longer. Now, this doesn't mean that they no longer exist in the database; indeed, if you run the query again (assuming nobody has changed the data in the meantime), you'll get back the same set of objects; they're still entities. It simply means that there is not currently an active correspondence being maintained between the state of the objects in your virtual machine and the database. It is perfectly reasonable to carry on working with the objects. If you later need to make changes to the objects and you want the changes to 'stick,' you will open a new session and use it to save the changed objects. Because each entity has a unique ID, Hibernate has no problem figuring out how to link the transient objects back to the appropriate persistent state in the new session.<BR><BR>下面讨论“在session关闭后，和session绑定的持久化对象的状态”的问题。例如当你执行了一个查询，得到了你所需要的实体对象之后，正如前面所述的那样，你不应该将session保持过久，使之超出数据库操作的范围，这时候你将会关闭session。那么此时那些已经载入的映射对象处于什么状态呢？答案是这样的，在session的打开的过程中，它是处于持久化状态，但是它一旦不在和active session（因为此时session已经关闭）绑定，那么它就处于持久化状态。但是，这并不意味着他们在数据库中就不再存在，事实上，如果你再次执行查询（假设此间无人修改数据），那么你将可以得到同样的集合数据。也就是说，虚拟机中的对象所处的状态和数据库中的实体数据的状态之间并没有必然的联系。这时候，你可以对那些对象进行自己的操作，如果你改变了一些对象的数据，并想将其存储到数据库中，那么你必须重新建立一个session，使用它保存那些经过改变的数据。因为每个实体都有自己的唯一ID，因此hibernate可以很容易的在新的session中计算出如何将临时对象重新转换为相应的持久对象。<BR><BR></P>
<P class=docText>Armed with these concepts and terms, the remainder of the example is easy enough to understand. <A class=docLink href="mk:@MSITStore:E:\document\Java\hibernate\book\hibernate.a.developers.notebook.chm::/0596006969/hibernate-CHP-3-SECT-1.html#hibernate-CHP-3-EXAMPLE-1">Line 31</A> sets up a database transaction using our open session. Within that, we create a few <TT>Track</TT> instances containing sample data and save them in the session (<A class=docLink href="mk:@MSITStore:E:\document\Java\hibernate\book\hibernate.a.developers.notebook.chm::/0596006969/hibernate-CHP-3-SECT-1.html#hibernate-CHP-3-EXAMPLE-1">lines 33-50</A>), turning them from transient instances into persistent entities. Finally, <A class=docLink href="mk:@MSITStore:E:\document\Java\hibernate\book\hibernate.a.developers.notebook.chm::/0596006969/hibernate-CHP-3-SECT-1.html#hibernate-CHP-3-EXAMPLE-1">line 53</A> commits our transaction, atomically (as a single, indivisible unit) making all the database changes permanent. The <TT>try/catch/finally</TT> block wrapped around all this shows an important and useful idiom for working with transactions. If anything goes wrong, <A class=docLink href="mk:@MSITStore:E:\document\Java\hibernate\book\hibernate.a.developers.notebook.chm::/0596006969/hibernate-CHP-3-SECT-1.html#hibernate-CHP-3-EXAMPLE-1">lines 56-60</A> will roll back the transaction and then bubble out the exception, leaving the database the way we found it. The session is closed in the <TT>finally</TT> portion at <A class=docLink href="mk:@MSITStore:E:\document\Java\hibernate\book\hibernate.a.developers.notebook.chm::/0596006969/hibernate-CHP-3-SECT-1.html#hibernate-CHP-3-EXAMPLE-1">line 63</A>, ensuring that this takes place whether we exit through the 'happy path' of a successful commit, or via an exception that caused rollback. Either way, it gets closed as it should.<BR></P>
<P class=docText>通过上面的概念的讲述和讨论，那么例子中的其他部分也就很好理解了。第31行，使用打开的session建立了数据库事务。在事务中，33－50行创建了一个Track对象，并在session中将其保存，将其从临时对象变为持久对象。最后，53行提交事务，原子性的执行数据库改变。这里的try/catch/finally封装了事务处理中常出现的idom。一旦出现任何问题，56－60行就会回滚事务，然后抛出异常，保证数据库维持原状。无论如何，都会执行finnally块中的63行，无论在成功执行或者出现错误回滚的情况下，都会关闭session。<BR><BR>At the end of our method we also close the session factory itself on <A class=docLink href="mk:@MSITStore:E:\document\Java\hibernate\book\hibernate.a.developers.notebook.chm::/0596006969/hibernate-CHP-3-SECT-1.html#hibernate-CHP-3-EXAMPLE-1">line 67</A>. This is something you'd do in the 'graceful shutdown' section of your application. In a web application environment, it would be in the appropriate lifecycle event handler. <SUP class=docFootnote><A class=docLink href="mk:@MSITStore:E:\document\Java\hibernate\book\hibernate.a.developers.notebook.chm::/0596006969/hibernate-CHP-3-SECT-1.html#hibernate-CHP-3-FN-1">[3.1]</A></SUP> In this simple example, when the <TT>main()</TT> method returns, the application is ending.<BR><BR>在方法的最后67行，将会关闭session factory。这是你的应用程序正常退出时应该执行的操作。在Web程序中，你则需要使用一定的生命周期事件处理器，完成此操作。在我们的例子中，在main退出的时候，也就是程序中止的时间。<BR>&nbsp;</P><img src ="http://www.blogjava.net/jinfeng_wang/aggbug/2706.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jinfeng_wang/" target="_blank">jinfeng_wang</a> 2005-04-01 14:21 <a href="http://www.blogjava.net/jinfeng_wang/archive/2005/04/01/2706.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hbm2java，schemaexport</title><link>http://www.blogjava.net/jinfeng_wang/archive/2005/04/01/2688.html</link><dc:creator>jinfeng_wang</dc:creator><author>jinfeng_wang</author><pubDate>Fri, 01 Apr 2005 02:22:00 GMT</pubDate><guid>http://www.blogjava.net/jinfeng_wang/archive/2005/04/01/2688.html</guid><wfw:comment>http://www.blogjava.net/jinfeng_wang/comments/2688.html</wfw:comment><comments>http://www.blogjava.net/jinfeng_wang/archive/2005/04/01/2688.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jinfeng_wang/comments/commentRss/2688.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jinfeng_wang/services/trackbacks/2688.html</trackback:ping><description><![CDATA[<P>&nbsp;</P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">&lt;?</SPAN><SPAN style="COLOR: #ff00ff">xml&nbsp;version="1.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">project&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="Harnessing&nbsp;Hibernate:&nbsp;The&nbsp;Developer's&nbsp;Notebook"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default</SPAN><SPAN style="COLOR: #0000ff">="db"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;basedir</SPAN><SPAN style="COLOR: #0000ff">="."</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">&nbsp;Set&nbsp;up&nbsp;properties&nbsp;containing&nbsp;important&nbsp;project&nbsp;directories&nbsp;</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="source.root"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;value</SPAN><SPAN style="COLOR: #0000ff">="src"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="class.root"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;value</SPAN><SPAN style="COLOR: #0000ff">="classes"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="lib.dir"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;value</SPAN><SPAN style="COLOR: #0000ff">="lib"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">property&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="data.dir"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;value</SPAN><SPAN style="COLOR: #0000ff">="data"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">&nbsp;Set&nbsp;up&nbsp;the&nbsp;class&nbsp;path&nbsp;for&nbsp;compilation&nbsp;and&nbsp;execution&nbsp;</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">path&nbsp;</SPAN><SPAN style="COLOR: #ff0000">id</SPAN><SPAN style="COLOR: #0000ff">="project.class.path"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">&nbsp;Include&nbsp;our&nbsp;own&nbsp;classes,&nbsp;of&nbsp;course&nbsp;</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">pathelement&nbsp;</SPAN><SPAN style="COLOR: #ff0000">location</SPAN><SPAN style="COLOR: #0000ff">="${class.root}"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">&nbsp;Include&nbsp;jars&nbsp;in&nbsp;the&nbsp;project&nbsp;library&nbsp;directory&nbsp;</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">fileset&nbsp;</SPAN><SPAN style="COLOR: #ff0000">dir</SPAN><SPAN style="COLOR: #0000ff">="${lib.dir}"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">include&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="*.jar"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">fileset</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">path</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">target&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="db"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;description</SPAN><SPAN style="COLOR: #0000ff">="Runs&nbsp;HSQLDB&nbsp;database&nbsp;management&nbsp;UI<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>against&nbsp;the&nbsp;database&nbsp;file--use&nbsp;when&nbsp;application&nbsp;is&nbsp;not&nbsp;running"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">java&nbsp;</SPAN><SPAN style="COLOR: #ff0000">classname</SPAN><SPAN style="COLOR: #0000ff">="org.hsqldb.util.DatabaseManager"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fork</SPAN><SPAN style="COLOR: #0000ff">="yes"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">classpath&nbsp;</SPAN><SPAN style="COLOR: #ff0000">refid</SPAN><SPAN style="COLOR: #0000ff">="project.class.path"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">arg&nbsp;</SPAN><SPAN style="COLOR: #ff0000">value</SPAN><SPAN style="COLOR: #0000ff">="-driver"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">arg&nbsp;</SPAN><SPAN style="COLOR: #ff0000">value</SPAN><SPAN style="COLOR: #0000ff">="org.hsqldb.jdbcDriver"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">arg&nbsp;</SPAN><SPAN style="COLOR: #ff0000">value</SPAN><SPAN style="COLOR: #0000ff">="-url"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">arg&nbsp;</SPAN><SPAN style="COLOR: #ff0000">value</SPAN><SPAN style="COLOR: #0000ff">="jdbc:hsqldb:${data.dir}/music"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">arg&nbsp;</SPAN><SPAN style="COLOR: #ff0000">value</SPAN><SPAN style="COLOR: #0000ff">="-user"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">arg&nbsp;</SPAN><SPAN style="COLOR: #ff0000">value</SPAN><SPAN style="COLOR: #0000ff">="sa"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">java</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">target</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">&nbsp;Teach&nbsp;Ant&nbsp;how&nbsp;to&nbsp;use&nbsp;Hibernate's&nbsp;code&nbsp;generation&nbsp;tool&nbsp;</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">taskdef&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="hbm2java"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;classname</SPAN><SPAN style="COLOR: #0000ff">="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;classpathref</SPAN><SPAN style="COLOR: #0000ff">="project.class.path"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">&nbsp;Generate&nbsp;the&nbsp;java&nbsp;code&nbsp;for&nbsp;all&nbsp;mapping&nbsp;files&nbsp;in&nbsp;our&nbsp;source&nbsp;tree&nbsp;</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">target&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="codegen"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;description</SPAN><SPAN style="COLOR: #0000ff">="Generate&nbsp;Java&nbsp;source&nbsp;from&nbsp;the&nbsp;O/R&nbsp;mapping&nbsp;files"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">hbm2java&nbsp;</SPAN><SPAN style="COLOR: #ff0000">output</SPAN><SPAN style="COLOR: #0000ff">="${source.root}"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">fileset&nbsp;</SPAN><SPAN style="COLOR: #ff0000">dir</SPAN><SPAN style="COLOR: #0000ff">="${source.root}"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">include&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="**/*.hbm.xml"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">fileset</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">hbm2java</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">target</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">&nbsp;Create&nbsp;our&nbsp;runtime&nbsp;subdirectories&nbsp;and&nbsp;copy&nbsp;resources&nbsp;into&nbsp;them&nbsp;</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">target&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="prepare"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;description</SPAN><SPAN style="COLOR: #0000ff">="Sets&nbsp;up&nbsp;build&nbsp;structures"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">mkdir&nbsp;</SPAN><SPAN style="COLOR: #ff0000">dir</SPAN><SPAN style="COLOR: #0000ff">="${class.root}"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">&nbsp;Copy&nbsp;our&nbsp;property&nbsp;files&nbsp;and&nbsp;O/R&nbsp;mappings&nbsp;for&nbsp;use&nbsp;at&nbsp;runtime&nbsp;</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">copy&nbsp;</SPAN><SPAN style="COLOR: #ff0000">todir</SPAN><SPAN style="COLOR: #0000ff">="${class.root}"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">fileset&nbsp;</SPAN><SPAN style="COLOR: #ff0000">dir</SPAN><SPAN style="COLOR: #0000ff">="${source.root}"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">include&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="**/*.properties"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">include&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="**/*.hbm.xml"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">fileset</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">copy</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">target</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">&nbsp;Compile&nbsp;the&nbsp;java&nbsp;source&nbsp;of&nbsp;the&nbsp;project&nbsp;</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">target&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="compile"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;depends</SPAN><SPAN style="COLOR: #0000ff">="prepare"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;description</SPAN><SPAN style="COLOR: #0000ff">="Compiles&nbsp;all&nbsp;Java&nbsp;classes"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">javac&nbsp;</SPAN><SPAN style="COLOR: #ff0000">srcdir</SPAN><SPAN style="COLOR: #0000ff">="${source.root}"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destdir</SPAN><SPAN style="COLOR: #0000ff">="${class.root}"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug</SPAN><SPAN style="COLOR: #0000ff">="on"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimize</SPAN><SPAN style="COLOR: #0000ff">="off"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deprecation</SPAN><SPAN style="COLOR: #0000ff">="on"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">classpath&nbsp;</SPAN><SPAN style="COLOR: #ff0000">refid</SPAN><SPAN style="COLOR: #0000ff">="project.class.path"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">javac</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">target</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">&nbsp;Generate&nbsp;the&nbsp;schemas&nbsp;for&nbsp;all&nbsp;mapping&nbsp;files&nbsp;in&nbsp;our&nbsp;class&nbsp;tree&nbsp;</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">target&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="schema"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;depends</SPAN><SPAN style="COLOR: #0000ff">="compile"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;description</SPAN><SPAN style="COLOR: #0000ff">="Generate&nbsp;DB&nbsp;schema&nbsp;from&nbsp;the&nbsp;O/R&nbsp;mapping&nbsp;files"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">&lt;!--</SPAN><SPAN style="COLOR: #008000">&nbsp;Teach&nbsp;Ant&nbsp;how&nbsp;to&nbsp;use&nbsp;Hibernate's&nbsp;schema&nbsp;generation&nbsp;tool&nbsp;</SPAN><SPAN style="COLOR: #008000">--&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">taskdef&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="schemaexport"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;classname</SPAN><SPAN style="COLOR: #0000ff">="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;classpathref</SPAN><SPAN style="COLOR: #0000ff">="project.class.path"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">schemaexport&nbsp;</SPAN><SPAN style="COLOR: #ff0000">properties</SPAN><SPAN style="COLOR: #0000ff">="${class.root}/hibernate.properties"</SPAN><SPAN style="COLOR: #ff0000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quiet</SPAN><SPAN style="COLOR: #0000ff">="no"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;text</SPAN><SPAN style="COLOR: #0000ff">="no"</SPAN><SPAN style="COLOR: #ff0000">&nbsp;drop</SPAN><SPAN style="COLOR: #0000ff">="no"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">fileset&nbsp;</SPAN><SPAN style="COLOR: #ff0000">dir</SPAN><SPAN style="COLOR: #0000ff">="${class.root}"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">include&nbsp;</SPAN><SPAN style="COLOR: #ff0000">name</SPAN><SPAN style="COLOR: #0000ff">="**/*.hbm.xml"</SPAN><SPAN style="COLOR: #0000ff">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">fileset</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">schemaexport</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">target</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">project</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></DIV></DIV>
<P>&nbsp;</P>
<P class=docText>You may be wondering why the taskdef for the schema update tool is inside our schema target, rather than at the top of the build file, next to the one for hbm2java. Well, I wanted it up there too, but I ran into a snag that's worth explaining. I got strange error messages the first time I tried to build the schema target, complaining there was no <SPAN class=docEmphasis>hibernate.properties</SPAN> on the class path and our compiled Track class couldn't be found. When I ran it again, it worked. Some detective work using<FONT style="BACKGROUND-COLOR: #008000"> <SPAN class=docEmphBold>ant -verbose</SPAN></FONT> revealed that if the <SPAN class=docEmphasis>classes</SPAN> directory didn't exist when the taskdef was encountered, Ant helpfully removed it from the class path. Since a taskdef can't have its own dependencies, the solution is to move it into the schema target, giving it the benefit of that target's dependencies, ensuring the <SPAN class=docEmphasis>classes</SPAN> directory exists by the time the taskdef is processed.<BR><BR>也许你会奇怪，为何找了schema更新工具的taskdef会在schema的target中进行定义，而不是在build文件的顶部，紧接着hbm2java进行定义。刚开始我也是那样想的，这里我需要解释一下这里的问题。当第一次构建schema时，我得到了许多莫名奇妙的错误，报错信息是：在classpath中找不到hibernate.properties和Track类。但是再次构建的时候，就可以了。当使用命令“ant -verbose”时，你就可以发现其中的细节之所在。如果在ant解析到taskdef时，build文件中所使用的class目录并不存在，那么ant就会自己将此目录从classpath中移除。但是taskdef又无法定义自己的dependencies，因此这里不得不将其移入到schema target中，这样也就可以使用到target的dependencies，保证在ant处理taskdef的时候，class目录必然已经存在。</P><img src ="http://www.blogjava.net/jinfeng_wang/aggbug/2688.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jinfeng_wang/" target="_blank">jinfeng_wang</a> 2005-04-01 10:22 <a href="http://www.blogjava.net/jinfeng_wang/archive/2005/04/01/2688.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Working with Hibernate in Eclipse - using Hibernate Synchronizer(zz)</title><link>http://www.blogjava.net/jinfeng_wang/archive/2005/03/15/2084.html</link><dc:creator>jinfeng_wang</dc:creator><author>jinfeng_wang</author><pubDate>Tue, 15 Mar 2005 00:45:00 GMT</pubDate><guid>http://www.blogjava.net/jinfeng_wang/archive/2005/03/15/2084.html</guid><wfw:comment>http://www.blogjava.net/jinfeng_wang/comments/2084.html</wfw:comment><comments>http://www.blogjava.net/jinfeng_wang/archive/2005/03/15/2084.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/jinfeng_wang/comments/commentRss/2084.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jinfeng_wang/services/trackbacks/2084.html</trackback:ping><description><![CDATA[<A href="http://www.onjava.com/pub/a/onjava/2005/01/05/hibernate.html">http://www.onjava.com/pub/a/onjava/2005/01/05/hibernate.html</A><BR><img src ="http://www.blogjava.net/jinfeng_wang/aggbug/2084.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jinfeng_wang/" target="_blank">jinfeng_wang</a> 2005-03-15 08:45 <a href="http://www.blogjava.net/jinfeng_wang/archive/2005/03/15/2084.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernatesynch的安装与使用</title><link>http://www.blogjava.net/jinfeng_wang/archive/2005/02/23/1449.html</link><dc:creator>jinfeng_wang</dc:creator><author>jinfeng_wang</author><pubDate>Wed, 23 Feb 2005 03:03:00 GMT</pubDate><guid>http://www.blogjava.net/jinfeng_wang/archive/2005/02/23/1449.html</guid><wfw:comment>http://www.blogjava.net/jinfeng_wang/comments/1449.html</wfw:comment><comments>http://www.blogjava.net/jinfeng_wang/archive/2005/02/23/1449.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.blogjava.net/jinfeng_wang/comments/commentRss/1449.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jinfeng_wang/services/trackbacks/1449.html</trackback:ping><description><![CDATA[<P>安装的在线地址：<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Eclipse 2.1: <A href="http://www.binamics.com/hibernatesync/eclipse2.1">http://www.binamics.com/hibernatesync/eclipse2.1</A>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Eclipse 3M*: <A href="http://www.binamics.com/hibernatesync">http://www.binamics.com/hibernatesync</A> <BR><BR>使用：<BR>1）创建project，自己加入hibernate的所有包，当然还有数据库驱动。<BR><BR>2) 新建hibernate 的配置文件：File &gt;&gt; New &gt;&gt; Other &gt;&gt; Hibernate &gt;&gt; Configuration File. <BR>&nbsp;&nbsp;&nbsp;&nbsp; 在这里填入配置文件的相关信息，包括：文件的存放位置，你所使用的数据库的具体配置（驱动，帐号，密码等）。这里的session factory name 不填。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp; 因为我直接是应用程序，所以没有使用datasource。<BR>&nbsp;&nbsp;&nbsp; 如果你使用的是datasource,那就需要选择application server.<BR><BR>&nbsp;&nbsp;&nbsp; 因为仅是简单应用程序，那么就需要将配置文件中的有关JTA的配置删除哦！<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="hibernate.transaction.factory_class"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;net.sf.hibernate.transaction.JTATransactionFactory<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/property&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property name="jta.UserTransaction"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;java:comp/UserTransaction<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/property&gt;<BR></P>
<P>3）新建hibernate的mapping文件：File &gt;&gt; New &gt;&gt; Other &gt;&gt; Hibernate &gt;&gt; Mapping File.&nbsp;<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; 这里需要选择mapping文件的存放位置<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 填入你的数据库帐号，然后refresh，选择你的数据库表。这里的table pattern,schema pattern 自己知道哦。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 填写你的包名，然后确认。<BR><BR>4）生成各项java文件。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 右键单击mapping文件，然后选择hibernate synchronize-&gt; synchronize files。 这就生成各项java文件了。 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果你看不到，那就refresh整个工程的文件夹。<BR><BR>5）编写测试程序，使用那生成的java文件进行数据库操作。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;测试程序中所使用的两个接口是：{tablename}.java&nbsp; ,&nbsp; {tablename}DAO.java<BR>&nbsp;&nbsp;&nbsp;例如：如果你的表名是Person，那么你将用Person.java, PersonDAO.java<BR>&nbsp;&nbsp; 这里的PersonDAO封装了hibernate的操作，更加方便了。<BR><BR>&nbsp;&nbsp; 自己观察一下吧，会有更多收获的。</P><img src ="http://www.blogjava.net/jinfeng_wang/aggbug/1449.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jinfeng_wang/" target="_blank">jinfeng_wang</a> 2005-02-23 11:03 <a href="http://www.blogjava.net/jinfeng_wang/archive/2005/02/23/1449.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>