﻿<?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-Scott@JAVA-随笔分类-Java EE 5</title><link>http://www.blogjava.net/cisco/category/6574.html</link><description>Java, 一杯浓浓的咖啡伴你到深夜&lt;br&gt;
&lt;span id="dict_daily"&gt;
&lt;a href="http://dict.cn/" target="_blank"&gt;Dict.CN 在线词典, 英语学习, 在线翻译&lt;/a&gt;
&lt;/span&gt; 
&lt;script language="JavaScript" src="http://dict.cn/daily.php" defer="defer"&gt;
&lt;/script&gt;</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 07:05:04 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 07:05:04 GMT</pubDate><ttl>60</ttl><item><title>The essential Java language library</title><link>http://www.blogjava.net/cisco/archive/2007/01/22/95275.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Mon, 22 Jan 2007 04:27:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2007/01/22/95275.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/95275.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2007/01/22/95275.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/95275.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/95275.html</trackback:ping><description><![CDATA[
		<p>
				<a name="N10058">
						<span class="atitle">
								<font face="Arial" size="4">Books</font>
						</span>
				</a>
		</p>
		<p>Every programmer has certain books that he or she wears out by constantly referencing them as a professional. The following books should be on every Java language programmer's bookshelf. Books can be expensive, so this list is intentionally small, limited to the critical ones.</p>
		<p>
				<a name="N10062">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Thinking in Java (Bruce Eckel)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0131002872">
						<i>
								<font color="#5c81a7">Thinking in Java, 3rd edition</font>
						</i>
				</a> (Bruce Eckel; Prentice Hall PTR, 2002)<br />Eckel's book is an extremely practical book for learning about how to use object oriented well in a Java language context. Lots of code samples illustrate the concepts as he introduces them. The text is absolutely practical, from a person who doesn't think Java technology is always the right answer. Eckel has lots of experience with lots of languages, and has solid object oriented thinking skills. This book puts those skills in a practical Java language context. He?s also working on a new book called <i>Thinking in Enterprise Java</i>.</p>
		<p>
				<a name="N10074">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Effective Java (Joshua Bloch)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0201310058">
						<i>
								<font color="#5c81a7">Effective Java: Programming Language Guide</font>
						</i>
				</a> (Joshua Bloch; Addison-Wesley, 2001)<br />This the best book for understanding the principles from which good Java programs are designed. Most of this material is not to be found in other "learning to Java" books. For example, Bloch's chapter on overriding <code>equals()</code> is one of the best I've ever read. He also includes practical advice on using interfaces instead of abstract classes, and using exceptions intelligently. Bloch was Sun's Java platform libraries architect, so he knows the language from the inside out. In fact, he wrote lots of the useful libraries included with the language. This is a must-read.</p>
		<p>
				<a name="N10087">
						<span class="smalltitle">
								<strong>
										<font face="Arial">The Java Programming Language (Ken Arnold, James Gosling, David Holmes)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0201704331">
						<i>
								<font color="#996699">The Java Programming Language</font>
						</i>
				</a> (Ken Arnold, James Gosling, David Holmes; Addison-Wesley, 2000)<br />This is probably the best Java language primer book available. It?s not a formal specification, but a readable introduction to each language feature. It has the right balance of rigor and pedagogy, enabling someone who understands programming to wrap their head around the Java language (and its copious class libraries) quickly.</p>
		<p>
				<a name="N10096">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Concurrent Programming in Java: Design Principles and Patterns (Doug Lea)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0201310090">
						<i>
								<font color="#5c81a7">Concurrent Programming in Java: Design Principles and Patterns, 2nd edition</font>
						</i>
				</a> (Doug Lea; Addison-Wesley, 1999)<br />Not every developer needs to know about concurrency in such detail, and not every engineer is up to the level of this book, but there's no better survey of concurrent programming than this. If you?re interested, this is the source. Lea is a professional programmer at SUNY, and his work and ideas related to concurrency have been included in the JDK 5.0 spec (from JSR166), so you can be sure what he has to say about using the Java language effectively is worth listening to. He's a good communicator as well.</p>
		<p>
				<a name="N100A5">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Expert One-On-One J2EE Design and Development (Rod Johnson)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0764543857">
						<i>
								<font color="#5c81a7">Expert One-On-One J2EE Design and Development</font>
						</i>
				</a> (Rod Johnson)<br />For newcomers to J2EE, this is the only book that really tells it like it is. This book is the result of years of experience of what works and what doesn't, and unlike a lot of other authors, Johnson is willing to say what doesn't. J2EE is often used when it's unnecessary overkill. Johnson's book can help you avoid that.</p>
		<p>
				<a name="N100B3">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Refactoring (Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0201485672">
						<i>
								<font color="#5c81a7">Refactoring: Improving the Design of Existing Code</font>
						</i>
				</a> (Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts; Addison-Wesley, 1999)<br />Fowler has written some of the most popular programming books ever published, including <i>Analysis Patterns</i>. His book on <i>refactoring</i> is the seminal work on the subject. Refactoring code is a neglected programmer discipline, but an intuitive programmer idea. Refactoring is improving the design of existing code without changing its results. It's the best way to keep your code clean, which keeps it easy to change over time. When do you refactor? Whenever your code 'smells'. Fowler?s book is full of example Java language code. Many Java language integrated development environments (IDEs), including IBM's Eclipse, incorporate Fowler's refactorings, using his names for each one, so it pays to be familiar with things like <i>extract method</i>.</p>
		<p>
				<a name="N100CB">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Design Patterns (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0201633612">
						<i>
								<font color="#5c81a7">Design Patterns: Elements of Reusable Object Oriented Software</font>
						</i>
				</a> (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides; Addison-Wesley, 1997)<br />This is one of the more famous books among professional programmers, and has become known as "the Gang of Four (GOF) book," based on the collective nickname of the authors. Patterns are reusable ways of thinking about and solving common programming problems. Learning patterns is a discipline. Using them well (or knowing when <b>not</b> to use them) is a skill. Ignoring them is a mistake. All of the examples in the book are in C++, but the Java language was born from that world, and it?s relatively simple for Java language programmers to relate to how to implement the patterns in the Java language. Being familiar with patterns, and knowing how to use them well, makes programming easier. It also makes communicating with other programmers easier, because patterns are a shortcut way to describe lots of related programming concepts that work together in a common solution to a common problem. Some of the more common ones, like <i>Factory Method</i>, are ubiquitous, even in the Java language itself. On the subject of using patterns wisely, you also might want to read Joshua Kerievsky's <i>Refactoring to Patterns</i>, which says you should let your code tell you when to implement a pattern.</p>
		<p>
				<a name="N100E3">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Patterns of Enterprise Application Architecture (Martin Fowler)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0321127420">
						<i>
								<font color="#5c81a7">Patterns of Enterprise Application Architecture</font>
						</i>
				</a> (Martin Fowler; Addison-Wesley, 2002)<br />Enterprise development certainly presents more challenges than small, one-off projects. That doesn?t mean that all enterprise development challenges are new. In fact, more often than not, it <i>has</i> been done before. In many cases, Fowler's worked on projects that did it. His book talks about some of the common solutions, and offers guidance on usage, compromises, and alternatives. Fowler includes some familiar patterns here, such as Model View Controller (MVC), but he also includes some that may be new to you, like Page Controller for handling requests for specific pages or actions on a Web site. As with most patterns, once you read many of them, you think, 'I knew that already'. Perhaps, but it helps to have a common presentation of the pattern to refer to. That kind of reference is a superb help on large projects with multiple components developed by different people.</p>
		<p>
				<a name="N100F5">
						<span class="smalltitle">
								<strong>
										<font face="Arial">UML Distilled (Martin Fowler)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0321193687">
						<i>
								<font color="#5c81a7">UML Distilled: A Brief Guide to the Standard Object Modeling Language</font>
						</i>
				</a> (Martin Fowler; Addison-Wesley 2003)<br />UML is an important common visual communication language for professional programmers, but it's overused and grossly abused. You don't need to know much to communicate with UML. Martin's distillation gives you the essentials. In fact, the inside front and back covers give you almost everything you'll ever use on a regular basis. All of the code behind the UML examples in the book is Java code.</p>
		<p>
				<a name="N10104">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Test-Driven Development: By Example (Kent Beck)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0321146530">
						<i>
								<font color="#5c81a7">Test-Driven Development: By Example</font>
						</i>
				</a> (Kent Beck; Addison-Wesley 2002)<br />Test-first programming will revolutionize your programming, and can help you become one of the better programmers out there. Writing tests before you write code is an initially awkward, but powerful development skill. By writing tests first, you can keep your code simpler, and you can be sure it works from the start (practicing what he preaches, Beck co-wrote JUnit, the most prevalent testing framework for the Java language, test-first). Beck?s book is the definitive source, and the extended Money example is in the Java language. Beck walks through how to <b>think</b> test-first, which can be an initial barrier for many programmers.</p>
		<p>
				<a name="N10116">
						<span class="smalltitle">
								<strong>
										<font face="Arial">The Pragmatic Programmer: From Journeyman to Master (Andy Hunt and Dave Thomas)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/020161622X">
						<i>
								<font color="#5c81a7">The Pragmatic Programmer: From Journeyman to Master</font>
						</i>
				</a> (Andrew Hunt and David Thomas; Addison-Wesley 1999)<br />Being an object oriented purist has its advantages. In today?s complex world, you often have to compromise in order to get things done as a Java language developer. The guiding principle is to be pragmatic. Hunt and Thomas talk about how to do that, without compromising what?s really important. This isn?t a Java language book, but it?s a critical mindset book for Java language developers. For example, I don't know of a single programmer who couldn't benefit from the admonition to "fix the problem, not the blame" and to sign your work like a proud craftsman.</p>
		<p>
				<a name="N10125">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Peopleware: Productive Projects and Teams (Tom DeMarco and Timothy Lister)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0932633439/qid=1112295380/sr=2-2/ref=pd_ka_b_2_2/102-0023248-0497724">
						<i>
								<font color="#5c81a7">Peopleware: Productive Projects and Teams</font>
						</i>
				</a> (Tom DeMarco, Timothy Lister; Dorset House, 1999)<br />All of the other books on this list are at least somewhat technical. This one is not. In all of the technical jargon, and in the sea of acronyms, sometimes software developers and managers forget that <i>people</i> make software. DeMarco and Lister remind us of that fact, and why it should make a difference. This isn?t a book about a particular programming language, but every Java language programmer should read it. There are other good books about how killing programmers is counterproductive for managers, but this is one of the best.<br /><br /></p>
		<hr />
		<p>
				<a name="N10137">
						<span class="atitle">
								<font face="Arial" size="4">Web sites</font>
						</span>
				</a>
		</p>
		<p>There are more Web sites than you could visit in a lifetime, that is, if you wanted to digest any of their content. An exhaustive list of sites with content about some aspect of the Java language would be ridiculously large. The following sites are tried and true.</p>
		<p>
				<a name="N10141">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Sun's Java Technology site</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://java.sun.com/">
						<font color="#5c81a7">Sun's Java language site</font>
				</a>
				<br />This is Sun?s main Java language site. You will find yourself visiting this site very frequently as a Java language developer. The following links are particularly important, especially to new Java language developers: 
</p>
		<ul>
				<li>
						<b>New to Java Center</b>
						<br />
						<a href="http://java.sun.com/learning/new2java/index.html">
								<font color="#5c81a7">New to Java Center</font>
						</a>
						<br />The New to Java Center is a good clearing house for step-by-step links to Java technology resources. If you're new to the language, this is a great place to start. 
</li>
				<li>
						<b>Tutorials and Code Camps</b>
						<br />
						<a href="http://java.sun.com/docs/books/tutorial/index.html">
								<font color="#5c81a7">Java Tutorial</font>
						</a>
						<br />The infamous Java Tutorial is here, along with other tutorials on various aspects of the Java language (such as Collections). </li>
		</ul>
		<p>
		</p>
		<p>
				<a name="N10169">
						<span class="smalltitle">
								<strong>
										<font face="Arial">IBM developerWorks</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.ibm.com/developerworks/">
						<font color="#5c81a7">IBM's developerWorks</font>
				</a>
				<br />This may seem shameless self-promotion, but developerWorks is a great resource for tutorials and articles about Java language tools and techniques. Content ranges from a beginner's guide to learning the language, to advanced concurrency techniques. You can search content by topic, and can browse by type.</p>
		<p>
				<a name="N10177">
						<span class="smalltitle">
								<strong>
										<font face="Arial">The Apache Software Foundation</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.apache.org/">
						<font color="#5c81a7">The Apache Software Foundation</font>
				</a>
				<br />The Apache site is home to many reusable libraries (in the Commons area) and tools to help Java developers. It?s all Open Source, so download what you want. Many extremely popular Java language libraries and tools (such as Struts, Ant, and Tomcat) began as Apache projects. The Jakarta area has most of the emerging Java language stuff.</p>
		<p>
				<a name="N10184">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Eclipse.org</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.eclipse.org/">
						<font color="#5c81a7">Eclipse</font>
				</a>
				<br />There are several good Java language integrated development environments (IDEs). Eclipse (from IBM) is one of the newest, and is rapidly becoming the premier IDE for Java language development. It?s entirely Open Source, which means it?s free. This site contains all sorts of resources for learning about how to use Eclipse effectively. It also has information about the Standard Widget Toolkit (SWT), a lighter weight alternative to Swing.</p>
		<p>
				<a name="N10191">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Eclipse Plugin Central and EclipsePlugins</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.eclipseplugincentral.com/">
						<font color="#5c81a7">Eclipse Plugin Central</font>
				</a> and <a href="http://eclipse-plugins.2y.net/"><font color="#5c81a7">EclipsePlugins</font></a><br />Eclipse is based on a plug-in architecture. In fact, the Java language development components of Eclipse are plug-ins. But there are literally hundreds of plug-ins for everything from Web development to playing games within the Eclipse environment. These two sites list most of them, by category, and are searchable. They are excellent resources. If you use Eclipse, and want to do something new in your development environment, odds are good that there?s a plug-in for it, and that you can find it here. Both sites let people review the plug-ins, so you can get some information about which ones are good and which ones aren?t worth your time.</p>
		<p>
				<a name="N101A2">
						<span class="smalltitle">
								<strong>
										<font face="Arial">JUnit.org</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.junit.org/">
						<font color="#5c81a7">JUnit.org</font>
				</a>
				<br />JUnit is the seminal unit testing framework for the Java language. The site contains the latest and greatest version, plus a huge number of other resources about testing (in the Java language and others) at various levels, for desktop apps, Web apps, J2EE apps, etc. If you want to find testing resources, this is the best place to start.</p>
		<p>
				<a name="N101AF">
						<span class="smalltitle">
								<strong>
										<font face="Arial">TheServerSide.com</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.theserverside.com/">
						<font color="#5c81a7">TheServerSide.com</font>
				</a>
				<br />If you are (or will be) involved with Java language development on the server side, this site is a vital resource. You can find articles here on JBoss, J2EE, LDAP, Struts, and a host of other topics, and it?s fully searchable. They don't simply describe features of the Java language, or supporting libraries. They take it one step further to describe novel uses of libraries (such as using Jakarta's Velocity as a rules engine, rather than a templating engine). They also provide running commentary on the state of the Java language (a current article is titled <i>Java is boring</i>, by Tim Bray). One of the better common features of the site are matrix comparisons of Java language tools and products (application servers, etc.).</p>
		<p>
				<a name="N101BF">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Bruce Eckel's MindView, Inc.</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.bruceeckel.com/">
						<font color="#5c81a7">Bruce Eckel's MindView, Inc.</font>
				</a>
				<br />Eckel wrote several books about "thinking in" the Java language, Python, and C++. His <i>Thinking in Java</i> was particularly helpful to me as I was learning the language. It?s practical and to the point, with excellent insights about how to think in objects in a Java language context. You can download free electronic copies of all of his books at this site. He also has written many good articles, and he?s got links to all of them here (including articles on Jython, Java vs. .NET, etc.).</p>
		<p>
				<a name="N101CF">
						<span class="smalltitle">
								<strong>
										<font face="Arial">ONJava.com</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.onjava.com/">
						<font color="#996699">ONJava.com</font>
				</a>
				<br />O?Reilley has published excellent books about programming languages and tools for years. Their Java-language focused Web site is just as good. It has articles on various Java language tools (like JDOM and Hibernate), different areas for different parts of the Java Platform (like J2SE and J2EE). It?s fully searchable. They have excellent articles and tutorials. The site is arranged by topic. Some examples include Java and XML, Java Security, Wireless Java, and Java SysAdmin. The site also has a link to the O'Reilley Learning Lab, where you can take online courses (Java language-related and otherwise). They aren't free, but many count toward college credit, so you get a convenient way to learn skills, and some credentials as well.</p>
		<p>
				<a name="N101DC">
						<span class="smalltitle">
								<strong>
										<font face="Arial">java.net</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://community.java.net/">
						<font color="#996699">java.net Communities</font>
				</a>
				<br />There are multiple "communities" here, with subject-specific forums and articles. For example, the Java Desktop community has all sorts of stuff related to Java language development for the desktop. The Java Patterns community might be of particular interest as a portal for pattern resources from a Java language perspective. There is also a community for Java User Groups (JUGs), where you can find information about creating, joining and running a JUG.<br /></p>
<img src ="http://www.blogjava.net/cisco/aggbug/95275.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2007-01-22 12:27 <a href="http://www.blogjava.net/cisco/archive/2007/01/22/95275.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>别让Hibernate偷走了您的身份</title><link>http://www.blogjava.net/cisco/archive/2006/12/02/84956.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Fri, 01 Dec 2006 16:53:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2006/12/02/84956.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/84956.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2006/12/02/84956.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/84956.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/84956.html</trackback:ping><description><![CDATA[
		<p>[转] <a href="http://dev2dev.bea.com.cn/techdoc/20061103895.html">http://dev2dev.bea.com.cn/techdoc/20061103895.html</a><br /><br /></p>
		<p>　　 企业级Java应用程序常常把数据在Java对象和相关数据库之间来回移动。从手工编写SQL代码到诸如<a href="http://www.hibernate.org/">Hibernate</a>这样成熟的对象关系映射（ORM）解决方案，有很多种方法可以实现这个过程。无论采用什么样的技术，一旦开始将Java对象持久存储到数据库中，身份将成为一个复杂且难以管理的课题。可能出现的情况是：您实例化了两个不同的对象，而它们却代表数据库中的同一行。为了解决这个问题，您可能采取的措施是在持久性对象中实现equals()和hashCode()，可是要恰当地实现这两个方法比乍看之下要有技巧一些。让问题更糟糕的是，那些传统的思路（包括Hibernate官方文档所提倡的）对于新的项目并不一定能提出最实用的解决方案。</p>
		<p>　　 对象身份在虚拟机（VM）中和在数据库中的差异是问题滋生的温床。在虚拟机中，您并不会得到对象的ID，您只是简单地持有对象的直接引用。而在幕后，虚拟机确实给每个对象指派了一个8字节大小的ID，这个ID才是对象的真实引用。当您将对象持久存储到数据库中的时候，问题开始产生了。假定您创建了一个Person对象并将它存入数据库（我们可以叫它person1）。而您的其他某段代码从数据库中读取了这个Person对象的数据，并将它实例化为另一个新的Person对象（我们可以叫它Person2）。现在您的内存中有了两个映射到数据库中同一行的对象。一个对象引用只能指向它们的其中一个，可是我们需要一种方法来表示这两个对象实际上表示着同一个实体。这就是（在虚拟机中）引入对象身份的原因。</p>
		<p>　　 在Java语言中，对象身份是由每个对象都持有的equals()方法（以及相关的hashCode()方法）来定义的。无论两个对象是否为同一个实例，equals()方法都应该能够判别出它们是否表示同一个实体。hashCode()方法和equals()方法有关联是因为所有相等的对象都应该返回相同的hashCode。默认情况下，equals()方法仅仅比较对象引用。一个对象和它自身是相等的，而和其他任何实例都不相等。对于持久性对象来说，重写这两个方法，让代表着数据库中同一行的两个对象被视为相等是很重要的。而这对于Java中Collection（Set、Map和List）的正确工作更是尤为重要。</p>
		<p>　　 为了阐明实现equal()和hashCode()的不同途径，让我们考虑一个准备持久存储到数据库中的简单对象Person。 </p>
		<pre class="code">public class Person {
    private Long id;
    private Integer version;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Integer getVersion() {
        return version;
    }
    public void setVersion(Integer version) {
        this.version = version;
    }

    // person-specific properties and behavior

}
</pre>
		<p>　　在这个例子中，我们遵循了同时持有id字段和version字段的最佳实践。Id字段保存了在数据库中作为主键使用的值，而version字段则是一个从0开始增长的增量，随着对象的每次更新而变化（这帮助我们避免并发更新的问题）。为了更清楚一些，让我们看看允许Hibernate把这个对象持久存储到数据库的Hibernate映射文件： </p>
		<pre class="code">&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE hibernate-mapping SYSTEM
    "http://hibernate.sourceforge.net/
    hibernate-mapping-3.0.dtd"&gt;

&lt;hibernate-mapping package="my.package"&gt;

  &lt;class name="Person" table="PERSON"&gt;

    &lt;id name="id" column="ID"
        unsaved-value="null"&gt;
      &lt;generator class="sequence"&gt;
        &lt;param name="sequence"&gt;PERSON_SEQ&lt;/param&gt;
      &lt;/generator&gt;
    &lt;/id&gt;

    &lt;version name="version" column="VERSION" /&gt;

    &lt;!-- Map Person-specific properties here. --&gt;

  &lt;/class&gt;

&lt;/hibernate-mapping&gt;

</pre>
		<p>　　Hibernate映射文件指明了Person的id字段代表数据库中的ID列（也就是说，它是PERSON表的主键）。包含在id标签中的unsaved-value="null"属性告诉Hibernate使用id字段来判断一个Person对象之前是否被保存过。ORM框架必须依靠这个来判断保存一个对象的时候应该使用SQL的INSERT子句还是UPDATE子句。在这个例子中，Hibernate假定一个新对象的id字段一开始为null值，当它第一次被保存时id才被赋予一个值。generator标签告诉Hibernate当对象第一次保存时，应该从哪里获得指派的id。在这个例子中，Hibernate使用数据库序列作为唯一ID的来源。最后，version标签告诉Hibernate使用Person对象的version字段进行并发控制。Hibernate将会执行乐观锁定方案，根据这个方案，Hibernate在保存对象之前会根据数据库版本号检查对象的版本号。</p>
		<p>　　 我们的Person对象还缺少的是equals()方法和hashCode()方法的实现。既然这是一个持久性对象，我们并不想依赖于这两个方法的默认实现，因为默认实现并不能分辨代表数据库中同一行的两个不同实例。一种简单而又显然的实现方法是利用id字段来进行equal()方法的比较以及生成hashCode()方法的结果。 </p>
		<pre class="code">public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || !(o instanceof Person))
        return false;

    Person other = (Person)o;

    if (id == other.getId()) return true;
    if (id == null) return false;

    // equivalence by id
    return id.equals(other.getId());
}

public int hashCode() {
    if (id != null) {
        return id.hashCode();
    } else {
        return super.hashCode();
    }
}
</pre>
		<p>　　不幸的是，这个实现存在着问题。当我们首次创建Person对象时id的值为null，这意味着任何两个Person对象只要尚未保存，就将被认为是相等的。如果我们想创建一个Person对象并把它放到一个Set中，再创建一个完全不同的Person对象也把它放到同一个Set里面，事实上第二个Person对象并不能被加入。这是因为Set会断定所有未保存的对象都是相同的。</p>
		<p>　　 您可能会试图去实现一个使用id（只在已设置id的情况下）的equals()方法。毕竟，如果两个对象都没有被保存过，我们可以假定它们是不同的对象。这是因为在它们被保存到数据库的时候，它们会被赋予不同的主键。 </p>
		<pre class="code">public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || !(o instanceof Person))
        return false;

    Person other = (Person)o;

    // unsaved objects are never equal
    if (id == null || other.getId() == null)
        return false;

    return id.equals(other.getId());
}
</pre>
		<p>　　这里有个隐含的问题。Java Collection框架在Collection的生命周期中需要基于不变字段的equals()和hashCode()方法。换句话来说，当一个对象处在Collection中的时候，不可以改变equals()和hashCode()的值。举个例子，下面这段程序： </p>
		<pre class="code">Person p = new Person();
Set set = new HashSet();
set.add(p);
System.out.println(set.contains(p));
p.setId(new Long(5));
System.out.println(set.contains(p));
</pre>
		<p>　　输出结果：true false</p>
		<p>　　 对set.contains(p)的第2次调用返回false，这是因为Set再也找不到p了。用专业术语来讲，就是Set丢失了这个对象！这是因为当对象在集合中时，我们改变了hashCode()的值。</p>
		<p>　　 当您想要创建一个将其他域对象保存在Set、Map或是List中的域对象时，这是一个问题。为了解决这个问题，您必须为所有对象提供一种equals()和hashCode()的实现，这种实现能够保证在它们在对象保存前后正确工作并且当对象在内存中时（返回值）不可变。Hibernate Reference Documentation (v. 3)提供了以下的建议：</p>
		<p>　　 “不要使用数据库标识符来实现相等性判断，而应该使用业务键（business key），这是一个唯一的、通常不改变的属性的组合体。当一个瞬态对象（transient object）被持久化的时候，数据库标识符会发生改变。当一个瞬态实例（常常与detached实例一起使用）保存在一个Set中时，哈希码的改变会破坏Set的约定。业务键的属性并不要求和数据库主键一样稳定，只要保证当对象在同一个Set中时它们的稳定性。”（<a href="http://www.hibernate.org/hib_docs/v3/reference/en/html/transactions.html#transactions-basics-identity">Hibernate Reference Documentation v. 3.1.1</a>）。</p>
		<p>　　 “我们推荐通过判断业务键相等性来实现equals()和hashCode()。业务键相等性意味着equals()方法只比较能够区分现实世界中实例的业务键（普通候选键）的属性。”（<a href="http://www.hibernate.org/hib_docs/v3/reference/en/html/persistent-classes.html#persistent-classes-equalshashcode">Hibernate Reference Documentation v. 3.1.1</a>）。</p>
		<p>　　 换句话说，普通键用于equals()和hashCode()，而Hibernate生成的代理项键用于对象的id。这要求对于每个对象有一个相关的不可变的业务键。可是，并不是每个对象类型都有这样的一种键，这时候您可能会尝试使用会改变但不经常改变的字段。这和业务键不必与数据库主键一样稳定的思想相吻合。如果这种键在对象所在集合的生存期中不改变，那这就“足够好”了。这是一种危险的观点，因为这意味着您的应用程序可能不会崩溃，但是前提是没有人在特定的情况下更新了特定的字段。所以，应当有一种更好的解决方案，这种解决方案确实也存在。</p>
		<p>　　 不要让Hibernate管理您的id。</p>
		<p>　　 试图创建和维护对象及数据库行的各自身份定义是目前为止所有讨论问题的根源。如果我们统一所有身份形式，这些问题都将不复存在。也就是说，作为以数据库为中心和以对象为中心的ID的替代品，我们应该创建一种通用的、特定于实体的ID来代表数据实体，这种ID应该在数据第一次输入的时候创建。无论这个唯一数据实体是保存在数据库中，是作为对象驻留在内存中，还是存储在其他格式的介质中，这个通用ID都应该可以识别它。通过使用数据实体第一次创建时指派的实体ID，我们可以安全地回到equals()和hashCode()的原始定义，它们只需使用这个id： </p>
		<pre class="code">public class Person {
    // assign an id as soon as possible
    private String id = IdGenerator.createId();
    private Integer version;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public Integer getVersion() {
        return version;
    }
    public void setVersion(Integer version) {
        this.version = version;
    }

    // Person-specific fields and behavior here

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || !(o instanceof Person))
            return false;

        Person other = (Person)o;

        if (id == null) return false;
        return id.equals(other.getId());
    }

    public int hashCode() {
        if (id != null) {
            return id.hashCode();
        } else {
            return super.hashCode();
        }
    }
}
</pre>
		<p>　　这个例子使用对象id作为equals()方法判断相等的标准，以及hashCode()返回哈希码的来源。这就简单了许多。但是，要让它正常工作，我们需要两样东西。首先，我们需要保证每个对象在被保存之前都有一个id值。在这个例子里，当id变量被声明的时候，它就被指派了一个值。其次，我们需要一种判断这个对象是新生成的还是之前保存过的的手段。在我们最早的例子中，Hibernate通过检查id字段是否为null来判断对象是否为新的。既然对象id永不为null，很显然这种方法不再有效。通过配置Hibernate，让它检查version字段，而不是id字段是否为null, 我们可以很容易地解决这个问题。version字段是一个更恰当的用来判断对象是否被保存过的指示符。</p>
		<p>　　 下面是我们改进过的Person类的Hibernate映射文件。 </p>
		<pre class="code">&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE hibernate-mapping SYSTEM
"http://hibernate.sourceforge.net/
hibernate-mapping-3.0.dtd"&gt;

&lt;hibernate-mapping package="my.package"&gt;

  &lt;class name="Person" table="PERSON"&gt;

    &lt;id name="id" column="ID"&gt;
      &lt;generator class="assigned" /&gt;
    &lt;/id&gt;

    &lt;version name="version" column="VERSION"
        unsaved-value="null" /&gt;

    &lt;!-- Map Person-specific properties here. --&gt;

  &lt;/class&gt;

&lt;/hibernate-mapping&gt;
</pre>
		<p>　　注意，id下面的generator标签包含了属性class="assigned"。这个属性告诉Hibernate我们不是从数据库指派id值，而是在代码中指派id值。Hibernate会简单地认为即使是新的未保存的对象也有id值。我们也给version标签新增了一个属性：unsaved-value="null"。这个属性告诉Hibernate应该把version值而不是id值为null作为对象是新创建而成的指示器。我们也可以简单地告诉Hibernate把负值作为对象未保存的指示符，如果您喜欢把version字段的类型设置为int而不是Integer，这将是很有用的。</p>
		<p>　　 我们已经从转移到纯对象id中获取了不少好处。我们对equals()和hashCode()方法的实现更加简单而且更易阅读。这些方法再也不易出错而且无论在保存对象之前还是之后，它们都能与Collection一起正常工作。Hibernate也变得更快一些，这是因为在保存新的对象之前它再也不需要从数据库读取一个序列值。此外，新定义的equals()和hashCode()对于所有包含id对象的对象来说是通用的。这意味着我们可以把这些方法移至一个抽象父类。我们不再需要为每个域对象重新实现equals()和hashCode()，而且我们也不再需要考虑对于每个类来说哪些字段组合是唯一且不变的。我们只要简单地扩展这个抽象父类。当然，我们没必要强迫域对象从父类中扩展出来，所以我们定义了一个接口来保证设计的灵活性。 </p>
		<pre class="code">public interface PersistentObject {
    public String getId();
    public void setId(String id);

    public Integer getVersion();
    public void setVersion(Integer version);
}

public abstract class AbstractPersistentObject
        implements PersistentObject {

    private String id = IdGenerator.createId();
    private Integer version;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public Integer getVersion() {
        return version;
    }
    public void setVersion(Integer version) {
        this.version = version;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null ||
            !(o instanceof PersistentObject)) {

            return false;
        }

        PersistentObject other
            = (PersistentObject)o;

        // if the id is missing, return false
        if (id == null) return false;

        // equivalence by id
        return id.equals(other.getId());
    }

    public int hashCode() {
        if (id != null) {
            return id.hashCode();
        } else {
            return super.hashCode();
        }
    }

    public String toString() {
        return this.getClass().getName()
            + "[id=" + id + "]";
    }
}
</pre>
		<p>　　现在我们有了一个简单而高效的方法来创建域对象。它们扩展了AbstractPersistentObject，该父类能在它们第一次创建时自动赋予一个id，并且恰当地实现了equals()和hashCode()。域对象也得到了一个toString()方法的合理默认实现，这个方法可以有选择地被重写。如果这是一个查询例子的测试对象或者示例对象，id可以被修改或者被设为null。否则它是不应当被改变的。如果因为某些原因我们需要创建一个扩展其他类的域对象，这个对象就应当实现PersistentObject接口而不是扩展抽象类。</p>
		<p>　　 Person类现在就简单多了： </p>
		<pre class="code">public class Person
    extends AbstractPersistentObject {

    // Person-specific fields and behavior here

}
</pre>
		<p>　　从上一个例子开始Hibernate映射文件就不会再改变了。我们不想麻烦Hibernate去了解抽象父类，我们只要保证每个PersistentObject映射文件包含一个id项（和一个“被指派的”生成器）和一个带有unsaved-value="null"属性的version标签。机敏的读者可能已经注意到，每当一个持久性对象被实例化的时候，它的id得到了指派。这意味着当Hibernate在内存中创建一个已保存对象的实例时，虽然这个对象是已经存在并从数据库中读取的，它也会得到一个新的id。这说好了。然后Hibernate会接着调用对象的setId()方法，用保存的id来替换新分配的id。额外的id生成并不是什么问题，因为id生成算法是廉价的（也就是说，它并不牵扯到数据库）。</p>
		<p>　　 到现在为止一切都很好，但是我们遗漏了一个重要的细节：如何实现IdGenerator.createId()。我们可以为理想中的键生成（key-generation）算法定义一些标准： </p>
		<ul type="disc">
				<li>键可以不牵扯到数据库而很廉价地生成。 
</li>
				<li>即使跨越不同的虚拟机和不同机器，键也要保证唯一性。 
</li>
				<li>如果可能，键可以由其他程序、编程语言和数据库生成，但是至少要能与它们兼容。 </li>
		</ul>
		<p>　　我们所需的是通用唯一标识符（universally unique identifier，UUID）。UUID由16个字节（128位）的数字组成，遵守标准格式。UUID的String版本看起来类似如下：</p>
		<p>　　 2cdb8cee-9134-453f-9d7a-14c0ae8184c6</p>
		<p>　　 里面的字符是简单的字节16进制表示，横线把数字的不同部分分隔开来。这种格式简单而且易于处理，只是36个字符有点长了。因为横线总是被安置在相同的位置，所以可以把它们去掉，从而把字符的数目减少到32个。为了更为简洁地表示，可以创建一个byte[16]的数组或是两个8字节大小的long来保存这些数字。如果您使用的是Java 1.5或更高版本，可以直接使用UUID类，虽然这不是它在内存中最简洁的格式。有关更多信息，请参阅<a href="http://en.wikipedia.org/wiki/UUID">Wikipedia UUID条目</a>和<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/UUID.html">JavaDoc UUID类条目</a>。</p>
		<p>　　 UUID生成算法有多种实现。既然最终UUID是一种标准格式，我们在IdGenerator类中采用哪一种实现都没有关系。既然无论采用什么算法每个id都会被保证唯一，我们甚至可以在任何时候改变算法的实现或是混合匹配不同的实现。如果您使用的是Java 1.5或更高版本，最方便的实现是java.util.UUID类： </p>
		<pre class="code">public class IdGenerator {
    public static String createId() {
        UUID uuid = java.util.UUID.randomUUID();
        return uuid.toString();
    }
}
</pre>
		<p>　　对不使用Java 1.5或更高版本的人来说，至少有两种扩展库实现了UUID并且与1.5之前的Java版本兼容：<a href="http://jakarta.apache.org/commons/sandbox/id/uuid.html">Apache Commons ID项目</a>和<a href="http://jug.safehaus.org/">Java UUID Generator</a> (JUG)项目。它们在Apache License之下都是可用的（在LGPL之下JUG也是可用的）。</p>
		<p>　　 这是使用JUG库实现IdGenerator的例子： </p>
		<pre class="code">import org.safehaus.uuid.UUIDGenerator;
public class IdGenerator {

    public static final UUIDGenerator uuidGen
        = UUIDGenerator.getInstance();

    public static String createId() {
        UUID uuid
            = uuidGen.generateRandomBasedUUID();
        return uuid.toString();
    }
}
</pre>
		<p>　　Hibernate中内置的UUID生成器算法又如何呢？这是获得对象身份的UUID的适当途径吗？如果您想让对象身份独立于对象持久性，这就不是一个好方法。虽然Hibernate确实提供了生成UUID的选项，但这样的话我们又回到了最早的那个问题上：对象ID的获得并不在它们被创建的时候，而是在它们被保存的时候。</p>
		<p>　　 使用UUID作为数据库主键的最大障碍是它们在数据库中（而不是在内存中）的大小，在数据库中索引和外键的复合会促使主键大小的增加。您必须在不同情况下使用不同的表示方法。使用String表示，数据库的主键大小将会是32或36字节。数字也可以直接以字节存储，这样大小就减少一半，但是如果直接查询数据库，标识符将变得难以理解。这些方法对您的项目是否可行取决于您的需求。</p>
		<p>　　 如果数据库不接受UUID作为主键，您可以考虑使用数据库序列。但总是应该在新对象创建的时候被指派一个ID而不是让Hibernate管理ID。在这种情况下，创建新域对象的业务对象可以调用一个使用数据访问对象（DAO）从数据库序列中检索id的服务。如果使用一个Long数据类型来表示对象id，一个单独的数据库序列（以及服务方法）对您的域对象来说就已经足够了。 </p>
		<h3>结束语</h3>
		<p>　　当对象持久存储到数据库中时，对象身份总是很难被恰当地实现。尽管如此，问题其实完全在于，对象在保存之前允许对象没有id就存在。我们可以通过从诸如Hibernate这样的对象关系映射框架中获得指派对象ID的职责来解决这个问题。一旦对象被实例化，它就应该被指派一个ID。这使对象身份变得简单而不易出错，也减少了域模型中需要的代码量。</p>
		<!--文章其他信息-->
<img src ="http://www.blogjava.net/cisco/aggbug/84956.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2006-12-02 00:53 <a href="http://www.blogjava.net/cisco/archive/2006/12/02/84956.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>