﻿<?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-JavaMuse-随笔分类-Hibernate学习笔记</title><link>http://www.blogjava.net/zhuguangxiang/category/19889.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 03:13:49 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 03:13:49 GMT</pubDate><ttl>60</ttl><item><title>对象的存储和关系型数据库</title><link>http://www.blogjava.net/zhuguangxiang/archive/2007/02/10/99133.html</link><dc:creator>满山红叶</dc:creator><author>满山红叶</author><pubDate>Sat, 10 Feb 2007 06:44:00 GMT</pubDate><guid>http://www.blogjava.net/zhuguangxiang/archive/2007/02/10/99133.html</guid><wfw:comment>http://www.blogjava.net/zhuguangxiang/comments/99133.html</wfw:comment><comments>http://www.blogjava.net/zhuguangxiang/archive/2007/02/10/99133.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhuguangxiang/comments/commentRss/99133.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhuguangxiang/services/trackbacks/99133.html</trackback:ping><description><![CDATA[
		<p>ORM 的原则是将一个类映射到一张表上，然而对象和关系型数据库之间有一点的差异。<br />对象和对象之间的关系有一对一，一对多，多对多。这三个关系的关系的存储是有差异的。<br />1、一对一：我们可以用两张表分别表示两个类。他们之间的关系我们可以用主关键字或者外关键字来表示。然而这里有一个粒度的问题(The problem of granularity )。关于粒度问题见第4点。<br />2、一对多：我们也可以用两张表来表示两个类。他们的关系我们可以用外键来关联。<br />3、多对多：我们可以用两张表来表示这两个类。用第三张表来表示他们之间的关系。<br />4、关于问题的粒度：<br />      如：一个用户和一个地址之间的关系。当然我们可以用一对一的方法来解决。<br />然而这样是否合理？也许最好的方法是在数据库中增加一个新的数据类型Address(country state city zipcode etc.)。这样我们就很容易把这个粒度问题解决了。不幸的是，数据库是不允许自定义类型的（也许现在的数据库可以，鄙人不太清楚，呵呵）。Hibernate 对这个问题有了很好的解决。见以后的文章。<br />5、对象之间可以有继承的关系，这是数据库望洋兴叹的。Hibernate 对这个问题有了很好的解决。见以后的文章。<br />6、对象的身份识别问题。<br />    大家都知道，java中的对象识别是用equals()和haseCode()来实现。<br />   举例：</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #0000ff">package</span>
				<span style="COLOR: #000000"> jmuse.eg;<br /><img id="Codehighlighter1_18_62_Open_Image" onclick="this.style.display='none'; Codehighlighter1_18_62_Open_Text.style.display='none'; Codehighlighter1_18_62_Closed_Image.style.display='inline'; Codehighlighter1_18_62_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_18_62_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_18_62_Closed_Text.style.display='none'; Codehighlighter1_18_62_Open_Image.style.display='inline'; Codehighlighter1_18_62_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
				<span id="Codehighlighter1_18_62_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_18_62_Open_Text">
						<span style="COLOR: #008000">/**</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /> *@hibernate.class table="MESSAGE"<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /> *<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" /> </span>
						<span style="COLOR: #008000">*/</span>
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						<br />
						<img id="Codehighlighter1_86_1159_Open_Image" onclick="this.style.display='none'; Codehighlighter1_86_1159_Open_Text.style.display='none'; Codehighlighter1_86_1159_Closed_Image.style.display='inline'; Codehighlighter1_86_1159_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
						<img id="Codehighlighter1_86_1159_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_86_1159_Closed_Text.style.display='none'; Codehighlighter1_86_1159_Open_Image.style.display='inline'; Codehighlighter1_86_1159_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"> </span>
				<span style="COLOR: #0000ff">class</span>
				<span style="COLOR: #000000"> Message </span>
				<span id="Codehighlighter1_86_1159_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_86_1159_Open_Text">
						<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">private</span>
						<span style="COLOR: #000000"> Long id;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">private</span>
						<span style="COLOR: #000000"> String text;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">private</span>
						<span style="COLOR: #000000"> String string;<br /><img id="Codehighlighter1_196_220_Open_Image" onclick="this.style.display='none'; Codehighlighter1_196_220_Open_Text.style.display='none'; Codehighlighter1_196_220_Closed_Image.style.display='inline'; Codehighlighter1_196_220_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_196_220_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_196_220_Closed_Text.style.display='none'; Codehighlighter1_196_220_Open_Image.style.display='inline'; Codehighlighter1_196_220_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">void</span>
						<span style="COLOR: #000000"> setString(String s)</span>
						<span id="Codehighlighter1_196_220_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_196_220_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        string</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">s;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img id="Codehighlighter1_226_270_Open_Image" onclick="this.style.display='none'; Codehighlighter1_226_270_Open_Text.style.display='none'; Codehighlighter1_226_270_Closed_Image.style.display='inline'; Codehighlighter1_226_270_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_226_270_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_226_270_Closed_Text.style.display='none'; Codehighlighter1_226_270_Open_Image.style.display='inline'; Codehighlighter1_226_270_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span id="Codehighlighter1_226_270_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_226_270_Open_Text">
								<span style="COLOR: #008000">/**</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     *@hibernate.property <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     *<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />     </span>
								<span style="COLOR: #008000">*/</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img id="Codehighlighter1_302_336_Open_Image" onclick="this.style.display='none'; Codehighlighter1_302_336_Open_Text.style.display='none'; Codehighlighter1_302_336_Closed_Image.style.display='inline'; Codehighlighter1_302_336_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_302_336_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_302_336_Closed_Text.style.display='none'; Codehighlighter1_302_336_Open_Image.style.display='inline'; Codehighlighter1_302_336_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />     </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> String getString()</span>
						<span id="Codehighlighter1_302_336_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_302_336_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">this</span>
								<span style="COLOR: #000000">.string;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</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" />    </span>
						<span style="COLOR: #0000ff">private</span>
						<span style="COLOR: #000000"> Message nextMessage;<br /><img id="Codehighlighter1_397_398_Open_Image" onclick="this.style.display='none'; Codehighlighter1_397_398_Open_Text.style.display='none'; Codehighlighter1_397_398_Closed_Image.style.display='inline'; Codehighlighter1_397_398_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_397_398_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_397_398_Closed_Text.style.display='none'; Codehighlighter1_397_398_Open_Image.style.display='inline'; Codehighlighter1_397_398_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> Message() </span>
						<span id="Codehighlighter1_397_398_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_397_398_Open_Text">
								<span style="COLOR: #000000">{}</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img id="Codehighlighter1_431_461_Open_Image" onclick="this.style.display='none'; Codehighlighter1_431_461_Open_Text.style.display='none'; Codehighlighter1_431_461_Closed_Image.style.display='inline'; Codehighlighter1_431_461_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_431_461_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_431_461_Closed_Text.style.display='none'; Codehighlighter1_431_461_Open_Image.style.display='inline'; Codehighlighter1_431_461_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> Message(String text)</span>
						<span id="Codehighlighter1_431_461_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_431_461_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">this</span>
								<span style="COLOR: #000000">.text</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">text;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img id="Codehighlighter1_499_528_Open_Image" onclick="this.style.display='none'; Codehighlighter1_499_528_Open_Text.style.display='none'; Codehighlighter1_499_528_Closed_Image.style.display='inline'; Codehighlighter1_499_528_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_499_528_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_499_528_Closed_Text.style.display='none'; Codehighlighter1_499_528_Open_Image.style.display='inline'; Codehighlighter1_499_528_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">void</span>
						<span style="COLOR: #000000"> setId(Long id) </span>
						<span id="Codehighlighter1_499_528_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_499_528_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">this</span>
								<span style="COLOR: #000000">.id </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> id; <br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
								<br />
								<img id="Codehighlighter1_568_601_Open_Image" onclick="this.style.display='none'; Codehighlighter1_568_601_Open_Text.style.display='none'; Codehighlighter1_568_601_Closed_Image.style.display='inline'; Codehighlighter1_568_601_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_568_601_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_568_601_Closed_Text.style.display='none'; Codehighlighter1_568_601_Open_Image.style.display='inline'; Codehighlighter1_568_601_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">void</span>
						<span style="COLOR: #000000"> setText(String text) </span>
						<span id="Codehighlighter1_568_601_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_568_601_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">this</span>
								<span style="COLOR: #000000">.text </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> text; <br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
								<br />
								<img id="Codehighlighter1_656_703_Open_Image" onclick="this.style.display='none'; Codehighlighter1_656_703_Open_Text.style.display='none'; Codehighlighter1_656_703_Closed_Image.style.display='inline'; Codehighlighter1_656_703_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_656_703_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_656_703_Closed_Text.style.display='none'; Codehighlighter1_656_703_Open_Image.style.display='inline'; Codehighlighter1_656_703_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">void</span>
						<span style="COLOR: #000000"> setNextMessage(Message nextMessage) </span>
						<span id="Codehighlighter1_656_703_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_656_703_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">this</span>
								<span style="COLOR: #000000">.nextMessage </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> nextMessage; <br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img id="Codehighlighter1_709_791_Open_Image" onclick="this.style.display='none'; Codehighlighter1_709_791_Open_Text.style.display='none'; Codehighlighter1_709_791_Closed_Image.style.display='inline'; Codehighlighter1_709_791_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_709_791_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_709_791_Closed_Text.style.display='none'; Codehighlighter1_709_791_Open_Image.style.display='inline'; Codehighlighter1_709_791_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span id="Codehighlighter1_709_791_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_709_791_Open_Text">
								<span style="COLOR: #008000">/**</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     *@hibernate.id column="MESSAGE_ID" generator-class="native"<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     *<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />     </span>
								<span style="COLOR: #008000">*/</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img id="Codehighlighter1_817_850_Open_Image" onclick="this.style.display='none'; Codehighlighter1_817_850_Open_Text.style.display='none'; Codehighlighter1_817_850_Closed_Image.style.display='inline'; Codehighlighter1_817_850_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_817_850_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_817_850_Closed_Text.style.display='none'; Codehighlighter1_817_850_Open_Image.style.display='inline'; Codehighlighter1_817_850_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> Long getId() </span>
						<span id="Codehighlighter1_817_850_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_817_850_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> (</span>
								<span style="COLOR: #0000ff">this</span>
								<span style="COLOR: #000000">.id); <br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img id="Codehighlighter1_856_921_Open_Image" onclick="this.style.display='none'; Codehighlighter1_856_921_Open_Text.style.display='none'; Codehighlighter1_856_921_Closed_Image.style.display='inline'; Codehighlighter1_856_921_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_856_921_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_856_921_Closed_Text.style.display='none'; Codehighlighter1_856_921_Open_Image.style.display='inline'; Codehighlighter1_856_921_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span id="Codehighlighter1_856_921_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_856_921_Open_Text">
								<span style="COLOR: #008000">/**</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     *@hibernate.property column="MESSAGE_TEXT"<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     *<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />     </span>
								<span style="COLOR: #008000">*/</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img id="Codehighlighter1_951_986_Open_Image" onclick="this.style.display='none'; Codehighlighter1_951_986_Open_Text.style.display='none'; Codehighlighter1_951_986_Closed_Image.style.display='inline'; Codehighlighter1_951_986_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_951_986_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_951_986_Closed_Text.style.display='none'; Codehighlighter1_951_986_Open_Image.style.display='inline'; Codehighlighter1_951_986_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> String getText() </span>
						<span id="Codehighlighter1_951_986_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_951_986_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> (</span>
								<span style="COLOR: #0000ff">this</span>
								<span style="COLOR: #000000">.text); <br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img id="Codehighlighter1_992_1077_Open_Image" onclick="this.style.display='none'; Codehighlighter1_992_1077_Open_Text.style.display='none'; Codehighlighter1_992_1077_Closed_Image.style.display='inline'; Codehighlighter1_992_1077_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_992_1077_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_992_1077_Closed_Text.style.display='none'; Codehighlighter1_992_1077_Open_Image.style.display='inline'; Codehighlighter1_992_1077_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span id="Codehighlighter1_992_1077_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_992_1077_Open_Text">
								<span style="COLOR: #008000">/**</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     *@hibernate.many-to-one column="NEXT_MESSAGE_ID" cascade="all"<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     *<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />     </span>
								<span style="COLOR: #008000">*/</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img id="Codehighlighter1_1115_1157_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1115_1157_Open_Text.style.display='none'; Codehighlighter1_1115_1157_Closed_Image.style.display='inline'; Codehighlighter1_1115_1157_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_1115_1157_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1115_1157_Closed_Text.style.display='none'; Codehighlighter1_1115_1157_Open_Image.style.display='inline'; Codehighlighter1_1115_1157_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> Message getNextMessage() </span>
						<span id="Codehighlighter1_1115_1157_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_1115_1157_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> (</span>
								<span style="COLOR: #0000ff">this</span>
								<span style="COLOR: #000000">.nextMessage); <br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
		</div>
		<p>
		</p>
		<p>这样的类由于没有实现equals()和haseCode()，所以如果用下面的代码我们可以看到如下输出：<br /></p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #000000">Message m1</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> Message("Hello");<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />Message m2</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> Message("Hello");<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000">(!(m2.equals(m1))</span>
				<span style="COLOR: #000000">)log.info(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">m1 and m2 are not idential</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #0000ff">else</span>
				<span style="COLOR: #000000"> log.info(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">m1 and m2 are identical</span>
				<span style="COLOR: #000000">"</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: #008000">//////////////</span>
				<span style="COLOR: #008000">/out put</span>
				<span style="COLOR: #008000">//////////////<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #000000">m1 and m2 are not idential</span>
		</div>
		<p>但是我们加上如下代码：<br /></p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img id="Codehighlighter1_32_225_Open_Image" onclick="this.style.display='none'; Codehighlighter1_32_225_Open_Text.style.display='none'; Codehighlighter1_32_225_Closed_Image.style.display='inline'; Codehighlighter1_32_225_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
				<img id="Codehighlighter1_32_225_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_32_225_Closed_Text.style.display='none'; Codehighlighter1_32_225_Open_Image.style.display='inline'; Codehighlighter1_32_225_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" />
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">boolean</span>
				<span style="COLOR: #000000"> equals(Object o)</span>
				<span id="Codehighlighter1_32_225_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_32_225_Open_Text">
						<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">if</span>
						<span style="COLOR: #000000">(</span>
						<span style="COLOR: #0000ff">this</span>
						<span style="COLOR: #000000">==</span>
						<span style="COLOR: #000000">o) </span>
						<span style="COLOR: #0000ff">return</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">true</span>
						<span style="COLOR: #000000">;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">if</span>
						<span style="COLOR: #000000">(</span>
						<span style="COLOR: #000000">!</span>
						<span style="COLOR: #000000">(o </span>
						<span style="COLOR: #0000ff">instanceof</span>
						<span style="COLOR: #000000"> Message)) <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                </span>
						<span style="COLOR: #0000ff">return</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">false</span>
						<span style="COLOR: #000000">;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">final</span>
						<span style="COLOR: #000000"> Message m</span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000">(Message)o;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">return</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">this</span>
						<span style="COLOR: #000000">.getText().equals(m.getText());<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />    }</span>
				</span>
				<span style="COLOR: #000000">
						<br />
						<img id="Codehighlighter1_252_408_Open_Image" onclick="this.style.display='none'; Codehighlighter1_252_408_Open_Text.style.display='none'; Codehighlighter1_252_408_Closed_Image.style.display='inline'; Codehighlighter1_252_408_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
						<img id="Codehighlighter1_252_408_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_252_408_Closed_Text.style.display='none'; Codehighlighter1_252_408_Open_Image.style.display='inline'; Codehighlighter1_252_408_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"> </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> hashCode()</span>
				<span id="Codehighlighter1_252_408_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_252_408_Open_Text">
						<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000">return text==null?System.identityHashCode(this):text.hashCode();</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #000000">        </span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> hase</span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000">20</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" />        </span>
						<span style="COLOR: #0000ff">return</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">20</span>
						<span style="COLOR: #000000">*</span>
						<span style="COLOR: #000000">hase</span>
						<span style="COLOR: #000000">+</span>
						<span style="COLOR: #000000">getText().hashCode();<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" />
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #008000">////////////</span>
				<span style="COLOR: #008000">out put</span>
				<span style="COLOR: #008000">////////////</span>
				<span style="COLOR: #008000">/</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">m1 and m2 are identical</span>
		</div>
		<p>这就是java的对象识别。如果你将修改后的Message对象加入到java.util.Set中，不管你加多少，最后size()仍然为1。<br /> 那么hibernate中的对象识别如何呢？见以后文章。呵呵。<br /><br /> </p>
<img src ="http://www.blogjava.net/zhuguangxiang/aggbug/99133.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhuguangxiang/" target="_blank">满山红叶</a> 2007-02-10 14:44 <a href="http://www.blogjava.net/zhuguangxiang/archive/2007/02/10/99133.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>