锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
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.
Thinking in Java (Bruce Eckel)
Thinking in Java, 3rd edition
(Bruce Eckel; Prentice Hall PTR, 2002)
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 Thinking in Enterprise Java.
Effective Java: Programming Language Guide
(Joshua Bloch; Addison-Wesley, 2001)
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 equals()
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.
The Java Programming Language (Ken Arnold, James Gosling, David Holmes)
The Java Programming Language
(Ken Arnold, James Gosling, David Holmes; Addison-Wesley, 2000)
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.
Concurrent Programming in Java: Design Principles and Patterns (Doug Lea)
Concurrent Programming in Java: Design Principles and Patterns, 2nd edition
(Doug Lea; Addison-Wesley, 1999)
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.
Expert One-On-One J2EE Design and Development (Rod Johnson)
Expert One-On-One J2EE Design and Development
(Rod Johnson)
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.
Refactoring (Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts)
Refactoring: Improving the Design of Existing Code
(Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts; Addison-Wesley, 1999)
Fowler has written some of the most popular programming books ever published, including Analysis Patterns. His book on refactoring 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 extract method.
Design Patterns (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides)
Design Patterns: Elements of Reusable Object Oriented Software
(Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides; Addison-Wesley, 1997)
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 not 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 Factory Method, are ubiquitous, even in the Java language itself. On the subject of using patterns wisely, you also might want to read Joshua Kerievsky's Refactoring to Patterns, which says you should let your code tell you when to implement a pattern.
Patterns of Enterprise Application Architecture (Martin Fowler)
Patterns of Enterprise Application Architecture
(Martin Fowler; Addison-Wesley, 2002)
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 has 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.
UML Distilled: A Brief Guide to the Standard Object Modeling Language
(Martin Fowler; Addison-Wesley 2003)
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.
Test-Driven Development: By Example (Kent Beck)
Test-Driven Development: By Example
(Kent Beck; Addison-Wesley 2002)
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 think test-first, which can be an initial barrier for many programmers.
The Pragmatic Programmer: From Journeyman to Master (Andy Hunt and Dave Thomas)
The Pragmatic Programmer: From Journeyman to Master
(Andrew Hunt and David Thomas; Addison-Wesley 1999)
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.
Peopleware: Productive Projects and Teams (Tom DeMarco and Timothy Lister)
Peopleware: Productive Projects and Teams
(Tom DeMarco, Timothy Lister; Dorset House, 1999)
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 people 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.
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.
Sun's Java language site
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:
IBM's developerWorks
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.
The Apache Software Foundation
The Apache Software Foundation
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.
Eclipse
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.
Eclipse Plugin Central and EclipsePlugins
Eclipse Plugin Central
and EclipsePlugins
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.
JUnit.org
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.
TheServerSide.com
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 Java is boring, by Tim Bray). One of the better common features of the site are matrix comparisons of Java language tools and products (application servers, etc.).
Bruce Eckel's MindView, Inc.
Eckel wrote several books about "thinking in" the Java language, Python, and C++. His Thinking in Java 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.).
ONJava.com
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.
java.net Communities
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.
銆銆 浼佷笟綰ava搴旂敤紼嬪簭甯稿父鎶婃暟鎹湪Java瀵硅薄鍜岀浉鍏蟲暟鎹簱涔嬮棿鏉ュ洖縐誨姩銆備粠鎵嬪伐緙栧啓SQL浠g爜鍒拌濡?a >Hibernate榪欐牱鎴愮啛鐨勫璞″叧緋繪槧灝勶紙ORM錛夎В鍐蟲柟妗堬紝鏈夊緢澶氱鏂規硶鍙互瀹炵幇榪欎釜榪囩▼銆傛棤璁洪噰鐢ㄤ粈涔堟牱鐨勬妧鏈紝涓鏃﹀紑濮嬪皢Java瀵硅薄鎸佷箙瀛樺偍鍒版暟鎹簱涓紝韜喚灝嗘垚涓轟竴涓鏉備笖闅句互綆$悊鐨勮棰樸傚彲鑳藉嚭鐜扮殑鎯呭喌鏄細鎮ㄥ疄渚嬪寲浜嗕袱涓笉鍚岀殑瀵硅薄錛岃屽畠浠嵈浠h〃鏁版嵁搴撲腑鐨勫悓涓琛屻備負浜嗚В鍐寵繖涓棶棰橈紝鎮ㄥ彲鑳介噰鍙栫殑鎺柦鏄湪鎸佷箙鎬у璞′腑瀹炵幇equals()鍜宧ashCode()錛屽彲鏄鎭板綋鍦板疄鐜拌繖涓や釜鏂規硶姣斾箥鐪嬩箣涓嬭鏈夋妧宸т竴浜涖傝闂鏇寸碂緋曠殑鏄紝閭d簺浼犵粺鐨勬濊礬錛堝寘鎷琀ibernate瀹樻柟鏂囨。鎵鎻愬$殑錛夊浜庢柊鐨勯」鐩茍涓嶄竴瀹氳兘鎻愬嚭鏈瀹炵敤鐨勮В鍐蟲柟妗堛?/p>
銆銆 瀵硅薄韜喚鍦ㄨ櫄鎷熸満錛圴M錛変腑鍜屽湪鏁版嵁搴撲腑鐨勫樊寮傛槸闂婊嬬敓鐨勬俯搴娿傚湪铏氭嫙鏈轟腑錛屾偍騫朵笉浼氬緱鍒板璞$殑ID錛屾偍鍙槸綆鍗曞湴鎸佹湁瀵硅薄鐨勭洿鎺ュ紩鐢ㄣ傝屽湪騫曞悗錛岃櫄鎷熸満紜疄緇欐瘡涓璞℃寚媧句簡涓涓?瀛楄妭澶у皬鐨処D錛岃繖涓狪D鎵嶆槸瀵硅薄鐨勭湡瀹炲紩鐢ㄣ傚綋鎮ㄥ皢瀵硅薄鎸佷箙瀛樺偍鍒版暟鎹簱涓殑鏃跺欙紝闂寮濮嬩駭鐢熶簡銆傚亣瀹氭偍鍒涘緩浜嗕竴涓狿erson瀵硅薄騫跺皢瀹冨瓨鍏ユ暟鎹簱錛堟垜浠彲浠ュ彨瀹僷erson1錛夈傝屾偍鐨勫叾浠栨煇孌典唬鐮佷粠鏁版嵁搴撲腑璇誨彇浜嗚繖涓狿erson瀵硅薄鐨勬暟鎹紝騫跺皢瀹冨疄渚嬪寲涓哄彟涓涓柊鐨凱erson瀵硅薄錛堟垜浠彲浠ュ彨瀹働erson2錛夈傜幇鍦ㄦ偍鐨勫唴瀛樹腑鏈変簡涓や釜鏄犲皠鍒版暟鎹簱涓悓涓琛岀殑瀵硅薄銆備竴涓璞″紩鐢ㄥ彧鑳芥寚鍚戝畠浠殑鍏朵腑涓涓紝鍙槸鎴戜滑闇瑕佷竴縐嶆柟娉曟潵琛ㄧず榪欎袱涓璞″疄闄呬笂琛ㄧず鐫鍚屼竴涓疄浣撱傝繖灝辨槸錛堝湪铏氭嫙鏈轟腑錛夊紩鍏ュ璞¤韓浠界殑鍘熷洜銆?/p>
銆銆 鍦↗ava璇█涓紝瀵硅薄韜喚鏄敱姣忎釜瀵硅薄閮芥寔鏈夌殑equals()鏂規硶錛堜互鍙婄浉鍏崇殑hashCode()鏂規硶錛夋潵瀹氫箟鐨勩傛棤璁轟袱涓璞℃槸鍚︿負鍚屼竴涓疄渚嬶紝equals()鏂規硶閮藉簲璇ヨ兘澶熷垽鍒嚭瀹冧滑鏄惁琛ㄧず鍚屼竴涓疄浣撱俬ashCode()鏂規硶鍜宔quals()鏂規硶鏈夊叧鑱旀槸鍥犱負鎵鏈夌浉絳夌殑瀵硅薄閮藉簲璇ヨ繑鍥炵浉鍚岀殑hashCode銆傞粯璁ゆ儏鍐典笅錛宔quals()鏂規硶浠呬粎姣旇緝瀵硅薄寮曠敤銆備竴涓璞″拰瀹冭嚜韜槸鐩哥瓑鐨勶紝鑰屽拰鍏朵粬浠諱綍瀹炰緥閮戒笉鐩哥瓑銆傚浜庢寔涔呮у璞℃潵璇達紝閲嶅啓榪欎袱涓柟娉曪紝璁╀唬琛ㄧ潃鏁版嵁搴撲腑鍚屼竴琛岀殑涓や釜瀵硅薄琚涓虹浉絳夋槸寰堥噸瑕佺殑銆傝岃繖瀵逛簬Java涓瑿ollection錛圫et銆丮ap鍜孡ist錛夌殑姝g‘宸ヤ綔鏇存槸灝や負閲嶈銆?/p>
銆銆 涓轟簡闃愭槑瀹炵幇equal()鍜宧ashCode()鐨勪笉鍚岄斿緞錛岃鎴戜滑鑰冭檻涓涓噯澶囨寔涔呭瓨鍌ㄥ埌鏁版嵁搴撲腑鐨勭畝鍗曞璞erson銆?
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 }
銆銆鍦ㄨ繖涓緥瀛愪腑錛屾垜浠伒寰簡鍚屾椂鎸佹湁id瀛楁鍜寁ersion瀛楁鐨勬渶浣沖疄璺點侷d瀛楁淇濆瓨浜嗗湪鏁版嵁搴撲腑浣滀負涓婚敭浣跨敤鐨勫鹼紝鑰寁ersion瀛楁鍒欐槸涓涓粠0寮濮嬪闀跨殑澧為噺錛岄殢鐫瀵硅薄鐨勬瘡嬈℃洿鏂拌屽彉鍖栵紙榪欏府鍔╂垜浠伩鍏嶅茍鍙戞洿鏂扮殑闂錛夈備負浜嗘洿娓呮涓浜涳紝璁╂垜浠湅鐪嬪厑璁窰ibernate鎶婅繖涓璞℃寔涔呭瓨鍌ㄥ埌鏁版嵁搴撶殑Hibernate鏄犲皠鏂囦歡錛?
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/ hibernate-mapping-3.0.dtd"> <hibernate-mapping package="my.package"> <class name="Person" table="PERSON"> <id name="id" column="ID" unsaved-value="null"> <generator class="sequence"> <param name="sequence">PERSON_SEQ</param> </generator> </id> <version name="version" column="VERSION" /> <!-- Map Person-specific properties here. --> </class> </hibernate-mapping>
銆銆Hibernate鏄犲皠鏂囦歡鎸囨槑浜哖erson鐨刬d瀛楁浠h〃鏁版嵁搴撲腑鐨処D鍒楋紙涔熷氨鏄錛屽畠鏄疨ERSON琛ㄧ殑涓婚敭錛夈傚寘鍚湪id鏍囩涓殑unsaved-value="null"灞炴у憡璇塇ibernate浣跨敤id瀛楁鏉ュ垽鏂竴涓狿erson瀵硅薄涔嬪墠鏄惁琚繚瀛樿繃銆侽RM妗嗘灦蹇呴』渚濋潬榪欎釜鏉ュ垽鏂繚瀛樹竴涓璞$殑鏃跺欏簲璇ヤ嬌鐢⊿QL鐨処NSERT瀛愬彞榪樻槸UPDATE瀛愬彞銆傚湪榪欎釜渚嬪瓙涓紝Hibernate鍋囧畾涓涓柊瀵硅薄鐨刬d瀛楁涓寮濮嬩負null鍊鹼紝褰撳畠絎竴嬈¤淇濆瓨鏃秈d鎵嶈璧嬩簣涓涓箋俫enerator鏍囩鍛婅瘔Hibernate褰撳璞$涓嬈′繚瀛樻椂錛屽簲璇ヤ粠鍝噷鑾峰緱鎸囨淳鐨刬d銆傚湪榪欎釜渚嬪瓙涓紝Hibernate浣跨敤鏁版嵁搴撳簭鍒椾綔涓哄敮涓ID鐨勬潵婧愩傛渶鍚庯紝version鏍囩鍛婅瘔Hibernate浣跨敤Person瀵硅薄鐨剉ersion瀛楁榪涜騫跺彂鎺у埗銆侶ibernate灝嗕細鎵ц涔愯閿佸畾鏂規錛屾牴鎹繖涓柟妗堬紝Hibernate鍦ㄤ繚瀛樺璞′箣鍓嶄細鏍規嵁鏁版嵁搴撶増鏈彿媯鏌ュ璞$殑鐗堟湰鍙楓?/p>
銆銆 鎴戜滑鐨凱erson瀵硅薄榪樼己灝戠殑鏄痚quals()鏂規硶鍜宧ashCode()鏂規硶鐨勫疄鐜般傛棦鐒惰繖鏄竴涓寔涔呮у璞★紝鎴戜滑騫朵笉鎯充緷璧栦簬榪欎袱涓柟娉曠殑榛樿瀹炵幇錛屽洜涓洪粯璁ゅ疄鐜板茍涓嶈兘鍒嗚鯨浠h〃鏁版嵁搴撲腑鍚屼竴琛岀殑涓や釜涓嶅悓瀹炰緥銆備竴縐嶇畝鍗曡屽張鏄劇劧鐨勫疄鐜版柟娉曟槸鍒╃敤id瀛楁鏉ヨ繘琛宔qual()鏂規硶鐨勬瘮杈冧互鍙婄敓鎴恏ashCode()鏂規硶鐨勭粨鏋溿?
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(); } }
銆銆涓嶅垢鐨勬槸錛岃繖涓疄鐜板瓨鍦ㄧ潃闂銆傚綋鎴戜滑棣栨鍒涘緩Person瀵硅薄鏃秈d鐨勫間負null錛岃繖鎰忓懗鐫浠諱綍涓や釜Person瀵硅薄鍙灝氭湭淇濆瓨錛屽氨灝嗚璁や負鏄浉絳夌殑銆傚鏋滄垜浠兂鍒涘緩涓涓狿erson瀵硅薄騫舵妸瀹冩斁鍒頒竴涓猄et涓紝鍐嶅垱寤轟竴涓畬鍏ㄤ笉鍚岀殑Person瀵硅薄涔熸妸瀹冩斁鍒板悓涓涓猄et閲岄潰錛屼簨瀹炰笂絎簩涓狿erson瀵硅薄騫朵笉鑳借鍔犲叆銆傝繖鏄洜涓篠et浼氭柇瀹氭墍鏈夋湭淇濆瓨鐨勫璞¢兘鏄浉鍚岀殑銆?/p>
銆銆 鎮ㄥ彲鑳戒細璇曞浘鍘誨疄鐜頒竴涓嬌鐢╥d錛堝彧鍦ㄥ凡璁劇疆id鐨勬儏鍐典笅錛夌殑equals()鏂規硶銆傛瘯绔燂紝濡傛灉涓や釜瀵硅薄閮芥病鏈夎淇濆瓨榪囷紝鎴戜滑鍙互鍋囧畾瀹冧滑鏄笉鍚岀殑瀵硅薄銆傝繖鏄洜涓哄湪瀹冧滑琚繚瀛樺埌鏁版嵁搴撶殑鏃跺欙紝瀹冧滑浼氳璧嬩簣涓嶅悓鐨勪富閿?
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()); }
銆銆榪欓噷鏈変釜闅愬惈鐨勯棶棰樸侸ava Collection妗嗘灦鍦–ollection鐨勭敓鍛藉懆鏈熶腑闇瑕佸熀浜庝笉鍙樺瓧孌電殑equals()鍜宧ashCode()鏂規硶銆傛崲鍙ヨ瘽鏉ヨ錛屽綋涓涓璞″鍦–ollection涓殑鏃跺欙紝涓嶅彲浠ユ敼鍙榚quals()鍜宧ashCode()鐨勫箋備婦涓緥瀛愶紝涓嬮潰榪欐紼嬪簭錛?
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));
銆銆杈撳嚭緇撴灉錛歵rue false
銆銆 瀵箂et.contains(p)鐨勭2嬈¤皟鐢ㄨ繑鍥瀎alse錛岃繖鏄洜涓篠et鍐嶄篃鎵句笉鍒皃浜嗐傜敤涓撲笟鏈鏉ヨ錛屽氨鏄疭et涓㈠け浜嗚繖涓璞★紒榪欐槸鍥犱負褰撳璞″湪闆嗗悎涓椂錛屾垜浠敼鍙樹簡hashCode()鐨勫箋?/p>
銆銆 褰撴偍鎯寵鍒涘緩涓涓皢鍏朵粬鍩熷璞′繚瀛樺湪Set銆丮ap鎴栨槸List涓殑鍩熷璞℃椂錛岃繖鏄竴涓棶棰樸備負浜嗚В鍐寵繖涓棶棰橈紝鎮ㄥ繀欏諱負鎵鏈夊璞℃彁渚涗竴縐峞quals()鍜宧ashCode()鐨勫疄鐜幫紝榪欑瀹炵幇鑳藉淇濊瘉鍦ㄥ畠浠湪瀵硅薄淇濆瓨鍓嶅悗姝g‘宸ヤ綔騫朵笖褰撳璞″湪鍐呭瓨涓椂錛堣繑鍥炲鹼級涓嶅彲鍙樸侶ibernate Reference Documentation (v. 3)鎻愪緵浜嗕互涓嬬殑寤鴻錛?/p>
銆銆 鈥滀笉瑕佷嬌鐢ㄦ暟鎹簱鏍囪瘑絎︽潵瀹炵幇鐩哥瓑鎬у垽鏂紝鑰屽簲璇ヤ嬌鐢ㄤ笟鍔¢敭錛坆usiness key錛夛紝榪欐槸涓涓敮涓鐨勩侀氬父涓嶆敼鍙樼殑灞炴х殑緇勫悎浣撱傚綋涓涓灛鎬佸璞★紙transient object錛夎鎸佷箙鍖栫殑鏃跺欙紝鏁版嵁搴撴爣璇嗙浼氬彂鐢熸敼鍙樸傚綋涓涓灛鎬佸疄渚嬶紙甯稿父涓巇etached瀹炰緥涓璧蜂嬌鐢級淇濆瓨鍦ㄤ竴涓猄et涓椂錛屽搱甯岀爜鐨勬敼鍙樹細鐮村潖Set鐨勭害瀹氥備笟鍔¢敭鐨勫睘鎬у茍涓嶈姹傚拰鏁版嵁搴撲富閿竴鏍風ǔ瀹氾紝鍙淇濊瘉褰撳璞″湪鍚屼竴涓猄et涓椂瀹冧滑鐨勭ǔ瀹氭с傗濓紙Hibernate Reference Documentation v. 3.1.1錛夈?/p>
銆銆 鈥滄垜浠帹鑽愰氳繃鍒ゆ柇涓氬姟閿浉絳夋ф潵瀹炵幇equals()鍜宧ashCode()銆備笟鍔¢敭鐩哥瓑鎬ф剰鍛崇潃equals()鏂規硶鍙瘮杈冭兘澶熷尯鍒嗙幇瀹炰笘鐣屼腑瀹炰緥鐨勪笟鍔¢敭錛堟櫘閫氬欓夐敭錛夌殑灞炴с傗濓紙Hibernate Reference Documentation v. 3.1.1錛夈?/p>
銆銆 鎹㈠彞璇濊錛屾櫘閫氶敭鐢ㄤ簬equals()鍜宧ashCode()錛岃孒ibernate鐢熸垚鐨勪唬鐞嗛」閿敤浜庡璞$殑id銆傝繖瑕佹眰瀵逛簬姣忎釜瀵硅薄鏈変竴涓浉鍏崇殑涓嶅彲鍙樼殑涓氬姟閿傚彲鏄紝騫朵笉鏄瘡涓璞$被鍨嬮兘鏈夎繖鏍風殑涓縐嶉敭錛岃繖鏃跺欐偍鍙兘浼氬皾璇曚嬌鐢ㄤ細鏀瑰彉浣嗕笉緇忓父鏀瑰彉鐨勫瓧孌點傝繖鍜屼笟鍔¢敭涓嶅繀涓庢暟鎹簱涓婚敭涓鏍風ǔ瀹氱殑鎬濇兂鐩稿惢鍚堛傚鏋滆繖縐嶉敭鍦ㄥ璞℃墍鍦ㄩ泦鍚堢殑鐢熷瓨鏈熶腑涓嶆敼鍙橈紝閭h繖灝扁滆凍澶熷ソ鈥濅簡銆傝繖鏄竴縐嶅嵄闄╃殑瑙傜偣錛屽洜涓鴻繖鎰忓懗鐫鎮ㄧ殑搴旂敤紼嬪簭鍙兘涓嶄細宕╂簝錛屼絾鏄墠鎻愭槸娌℃湁浜哄湪鐗瑰畾鐨勬儏鍐典笅鏇存柊浜嗙壒瀹氱殑瀛楁銆傛墍浠ワ紝搴斿綋鏈変竴縐嶆洿濂界殑瑙e喅鏂規錛岃繖縐嶈В鍐蟲柟妗堢‘瀹炰篃瀛樺湪銆?/p>
銆銆 涓嶈璁〩ibernate綆$悊鎮ㄧ殑id銆?/p>
銆銆 璇曞浘鍒涘緩鍜岀淮鎶ゅ璞″強鏁版嵁搴撹鐨勫悇鑷韓浠藉畾涔夋槸鐩墠涓烘鎵鏈夎璁洪棶棰樼殑鏍規簮銆傚鏋滄垜浠粺涓鎵鏈夎韓浠藉艦寮忥紝榪欎簺闂閮藉皢涓嶅瀛樺湪銆備篃灝辨槸璇達紝浣滀負浠ユ暟鎹簱涓轟腑蹇冨拰浠ュ璞′負涓績鐨処D鐨勬浛浠e搧錛屾垜浠簲璇ュ垱寤轟竴縐嶉氱敤鐨勩佺壒瀹氫簬瀹炰綋鐨処D鏉ヤ唬琛ㄦ暟鎹疄浣擄紝榪欑ID搴旇鍦ㄦ暟鎹涓嬈¤緭鍏ョ殑鏃跺欏垱寤恒傛棤璁鴻繖涓敮涓鏁版嵁瀹炰綋鏄繚瀛樺湪鏁版嵁搴撲腑錛屾槸浣滀負瀵硅薄椹葷暀鍦ㄥ唴瀛樹腑錛岃繕鏄瓨鍌ㄥ湪鍏朵粬鏍煎紡鐨勪粙璐ㄤ腑錛岃繖涓氱敤ID閮藉簲璇ュ彲浠ヨ瘑鍒畠銆傞氳繃浣跨敤鏁版嵁瀹炰綋絎竴嬈″垱寤烘椂鎸囨淳鐨勫疄浣揑D錛屾垜浠彲浠ュ畨鍏ㄥ湴鍥炲埌equals()鍜宧ashCode()鐨勫師濮嬪畾涔夛紝瀹冧滑鍙渶浣跨敤榪欎釜id錛?
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(); } } }
銆銆榪欎釜渚嬪瓙浣跨敤瀵硅薄id浣滀負equals()鏂規硶鍒ゆ柇鐩哥瓑鐨勬爣鍑嗭紝浠ュ強hashCode()榪斿洖鍝堝笇鐮佺殑鏉ユ簮銆傝繖灝辯畝鍗曚簡璁稿銆備絾鏄紝瑕佽瀹冩甯稿伐浣滐紝鎴戜滑闇瑕佷袱鏍蜂笢瑗褲傞鍏堬紝鎴戜滑闇瑕佷繚璇佹瘡涓璞″湪琚繚瀛樹箣鍓嶉兘鏈変竴涓猧d鍊箋傚湪榪欎釜渚嬪瓙閲岋紝褰搃d鍙橀噺琚0鏄庣殑鏃跺欙紝瀹冨氨琚寚媧句簡涓涓箋傚叾嬈★紝鎴戜滑闇瑕佷竴縐嶅垽鏂繖涓璞℃槸鏂扮敓鎴愮殑榪樻槸涔嬪墠淇濆瓨榪囩殑鐨勬墜孌點傚湪鎴戜滑鏈鏃╃殑渚嬪瓙涓紝Hibernate閫氳繃媯鏌d瀛楁鏄惁涓簄ull鏉ュ垽鏂璞℃槸鍚︿負鏂扮殑銆傛棦鐒跺璞d姘鎬笉涓簄ull錛屽緢鏄劇劧榪欑鏂規硶涓嶅啀鏈夋晥銆傞氳繃閰嶇疆Hibernate錛岃瀹冩鏌ersion瀛楁錛岃屼笉鏄痠d瀛楁鏄惁涓簄ull, 鎴戜滑鍙互寰堝鏄撳湴瑙e喅榪欎釜闂銆倂ersion瀛楁鏄竴涓洿鎭板綋鐨勭敤鏉ュ垽鏂璞℃槸鍚﹁淇濆瓨榪囩殑鎸囩ず絎︺?/p>
銆銆 涓嬮潰鏄垜浠敼榪涜繃鐨凱erson綾葷殑Hibernate鏄犲皠鏂囦歡銆?
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/ hibernate-mapping-3.0.dtd"> <hibernate-mapping package="my.package"> <class name="Person" table="PERSON"> <id name="id" column="ID"> <generator class="assigned" /> </id> <version name="version" column="VERSION" unsaved-value="null" /> <!-- Map Person-specific properties here. --> </class> </hibernate-mapping>
銆銆娉ㄦ剰錛宨d涓嬮潰鐨刧enerator鏍囩鍖呭惈浜嗗睘鎬lass="assigned"銆傝繖涓睘鎬у憡璇塇ibernate鎴戜滑涓嶆槸浠庢暟鎹簱鎸囨淳id鍊鹼紝鑰屾槸鍦ㄤ唬鐮佷腑鎸囨淳id鍊箋侶ibernate浼氱畝鍗曞湴璁や負鍗充嬌鏄柊鐨勬湭淇濆瓨鐨勫璞′篃鏈塱d鍊箋傛垜浠篃緇檝ersion鏍囩鏂板浜嗕竴涓睘鎬э細unsaved-value="null"銆傝繖涓睘鎬у憡璇塇ibernate搴旇鎶妚ersion鍊艱屼笉鏄痠d鍊間負null浣滀負瀵硅薄鏄柊鍒涘緩鑰屾垚鐨勬寚紺哄櫒銆傛垜浠篃鍙互綆鍗曞湴鍛婅瘔Hibernate鎶婅礋鍊間綔涓哄璞℃湭淇濆瓨鐨勬寚紺虹錛屽鏋滄偍鍠滄鎶妚ersion瀛楁鐨勭被鍨嬭緗負int鑰屼笉鏄疘nteger錛岃繖灝嗘槸寰堟湁鐢ㄧ殑銆?/p>
銆銆 鎴戜滑宸茬粡浠庤漿縐誨埌綰璞d涓幏鍙栦簡涓嶅皯濂藉銆傛垜浠equals()鍜宧ashCode()鏂規硶鐨勫疄鐜版洿鍔犵畝鍗曡屼笖鏇存槗闃呰銆傝繖浜涙柟娉曞啀涔熶笉鏄撳嚭閿欒屼笖鏃犺鍦ㄤ繚瀛樺璞′箣鍓嶈繕鏄箣鍚庯紝瀹冧滑閮借兘涓嶤ollection涓璧鋒甯稿伐浣溿侶ibernate涔熷彉寰楁洿蹇竴浜涳紝榪欐槸鍥犱負鍦ㄤ繚瀛樻柊鐨勫璞′箣鍓嶅畠鍐嶄篃涓嶉渶瑕佷粠鏁版嵁搴撹鍙栦竴涓簭鍒楀箋傛澶栵紝鏂板畾涔夌殑equals()鍜宧ashCode()瀵逛簬鎵鏈夊寘鍚玦d瀵硅薄鐨勫璞℃潵璇存槸閫氱敤鐨勩傝繖鎰忓懗鐫鎴戜滑鍙互鎶婅繖浜涙柟娉曠Щ鑷充竴涓娊璞$埗綾匯傛垜浠笉鍐嶉渶瑕佷負姣忎釜鍩熷璞¢噸鏂板疄鐜癳quals()鍜宧ashCode()錛岃屼笖鎴戜滑涔熶笉鍐嶉渶瑕佽冭檻瀵逛簬姣忎釜綾繪潵璇村摢浜涘瓧孌電粍鍚堟槸鍞竴涓斾笉鍙樼殑銆傛垜浠彧瑕佺畝鍗曞湴鎵╁睍榪欎釜鎶借薄鐖剁被銆傚綋鐒訛紝鎴戜滑娌″繀瑕佸己榪煙瀵硅薄浠庣埗綾諱腑鎵╁睍鍑烘潵錛屾墍浠ユ垜浠畾涔変簡涓涓帴鍙f潵淇濊瘉璁捐鐨勭伒媧繪с?
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 + "]"; } }
銆銆鐜板湪鎴戜滑鏈変簡涓涓畝鍗曡岄珮鏁堢殑鏂規硶鏉ュ垱寤哄煙瀵硅薄銆傚畠浠墿灞曚簡AbstractPersistentObject錛岃鐖剁被鑳藉湪瀹冧滑絎竴嬈″垱寤烘椂鑷姩璧嬩簣涓涓猧d錛屽茍涓旀伆褰撳湴瀹炵幇浜唀quals()鍜宧ashCode()銆傚煙瀵硅薄涔熷緱鍒頒簡涓涓猼oString()鏂規硶鐨勫悎鐞嗛粯璁ゅ疄鐜幫紝榪欎釜鏂規硶鍙互鏈夐夋嫨鍦拌閲嶅啓銆傚鏋滆繖鏄竴涓煡璇緥瀛愮殑嫻嬭瘯瀵硅薄鎴栬呯ず渚嬪璞★紝id鍙互琚慨鏀規垨鑰呰璁句負null銆傚惁鍒欏畠鏄笉搴斿綋琚敼鍙樼殑銆傚鏋滃洜涓烘煇浜涘師鍥犳垜浠渶瑕佸垱寤轟竴涓墿灞曞叾浠栫被鐨勫煙瀵硅薄錛岃繖涓璞″氨搴斿綋瀹炵幇PersistentObject鎺ュ彛鑰屼笉鏄墿灞曟娊璞$被銆?/p>
銆銆 Person綾葷幇鍦ㄥ氨綆鍗曞浜嗭細
public class Person extends AbstractPersistentObject { // Person-specific fields and behavior here }
銆銆浠庝笂涓涓緥瀛愬紑濮婬ibernate鏄犲皠鏂囦歡灝變笉浼氬啀鏀瑰彉浜嗐傛垜浠笉鎯抽夯鐑ibernate鍘諱簡瑙f娊璞$埗綾伙紝鎴戜滑鍙淇濊瘉姣忎釜PersistentObject鏄犲皠鏂囦歡鍖呭惈涓涓猧d欏癸紙鍜屼竴涓滆鎸囨淳鐨勨濈敓鎴愬櫒錛夊拰涓涓甫鏈塽nsaved-value="null"灞炴х殑version鏍囩銆傛満鏁忕殑璇昏呭彲鑳藉凡緇忔敞鎰忓埌錛屾瘡褰撲竴涓寔涔呮у璞¤瀹炰緥鍖栫殑鏃跺欙紝瀹冪殑id寰楀埌浜嗘寚媧俱傝繖鎰忓懗鐫褰揌ibernate鍦ㄥ唴瀛樹腑鍒涘緩涓涓凡淇濆瓨瀵硅薄鐨勫疄渚嬫椂錛岃櫧鐒惰繖涓璞℃槸宸茬粡瀛樺湪騫朵粠鏁版嵁搴撲腑璇誨彇鐨勶紝瀹冧篃浼氬緱鍒頒竴涓柊鐨刬d銆傝繖璇村ソ浜嗐傜劧鍚嶩ibernate浼氭帴鐫璋冪敤瀵硅薄鐨剆etId()鏂規硶錛岀敤淇濆瓨鐨刬d鏉ユ浛鎹㈡柊鍒嗛厤鐨刬d銆傞澶栫殑id鐢熸垚騫朵笉鏄粈涔堥棶棰橈紝鍥犱負id鐢熸垚綆楁硶鏄粔浠風殑錛堜篃灝辨槸璇達紝瀹冨茍涓嶇壍鎵埌鏁版嵁搴擄級銆?/p>
銆銆 鍒扮幇鍦ㄤ負姝竴鍒囬兘寰堝ソ錛屼絾鏄垜浠仐婕忎簡涓涓噸瑕佺殑緇嗚妭錛氬浣曞疄鐜癐dGenerator.createId()銆傛垜浠彲浠ヤ負鐞嗘兂涓殑閿敓鎴愶紙key-generation錛夌畻娉曞畾涔変竴浜涙爣鍑嗭細
銆銆鎴戜滑鎵闇鐨勬槸閫氱敤鍞竴鏍囪瘑絎︼紙universally unique identifier錛孶UID錛夈俇UID鐢?6涓瓧鑺傦紙128浣嶏級鐨勬暟瀛楃粍鎴愶紝閬靛畧鏍囧噯鏍煎紡銆俇UID鐨凷tring鐗堟湰鐪嬭搗鏉ョ被浼煎涓嬶細
銆銆 2cdb8cee-9134-453f-9d7a-14c0ae8184c6
銆銆 閲岄潰鐨勫瓧絎︽槸綆鍗曠殑瀛楄妭16榪涘埗琛ㄧず錛屾í綰挎妸鏁板瓧鐨勪笉鍚岄儴鍒嗗垎闅斿紑鏉ャ傝繖縐嶆牸寮忕畝鍗曡屼笖鏄撲簬澶勭悊錛屽彧鏄?6涓瓧絎︽湁鐐歸暱浜嗐傚洜涓烘í綰挎繪槸琚畨緗湪鐩稿悓鐨勪綅緗紝鎵浠ュ彲浠ユ妸瀹冧滑鍘繪帀錛屼粠鑰屾妸瀛楃鐨勬暟鐩噺灝戝埌32涓備負浜嗘洿涓虹畝媧佸湴琛ㄧず錛屽彲浠ュ垱寤轟竴涓猙yte[16]鐨勬暟緇勬垨鏄袱涓?瀛楄妭澶у皬鐨刲ong鏉ヤ繚瀛樿繖浜涙暟瀛椼傚鏋滄偍浣跨敤鐨勬槸Java 1.5鎴栨洿楂樼増鏈紝鍙互鐩存帴浣跨敤UUID綾伙紝铏界劧榪欎笉鏄畠鍦ㄥ唴瀛樹腑鏈綆媧佺殑鏍煎紡銆傛湁鍏蟲洿澶氫俊鎭紝璇峰弬闃?a >Wikipedia UUID鏉$洰鍜?a >JavaDoc UUID綾繪潯鐩?/a>銆?/p>
銆銆 UUID鐢熸垚綆楁硶鏈夊縐嶅疄鐜般傛棦鐒舵渶緇圲UID鏄竴縐嶆爣鍑嗘牸寮忥紝鎴戜滑鍦↖dGenerator綾諱腑閲囩敤鍝竴縐嶅疄鐜伴兘娌℃湁鍏崇郴銆傛棦鐒舵棤璁洪噰鐢ㄤ粈涔堢畻娉曟瘡涓猧d閮戒細琚繚璇佸敮涓錛屾垜浠敋鑷沖彲浠ュ湪浠諱綍鏃跺欐敼鍙樼畻娉曠殑瀹炵幇鎴栨槸娣峰悎鍖歸厤涓嶅悓鐨勫疄鐜般傚鏋滄偍浣跨敤鐨勬槸Java 1.5鎴栨洿楂樼増鏈紝鏈鏂逛究鐨勫疄鐜版槸java.util.UUID綾伙細
public class IdGenerator { public static String createId() { UUID uuid = java.util.UUID.randomUUID(); return uuid.toString(); } }
銆銆瀵逛笉浣跨敤Java 1.5鎴栨洿楂樼増鏈殑浜烘潵璇達紝鑷沖皯鏈変袱縐嶆墿灞曞簱瀹炵幇浜哢UID騫朵笖涓?.5涔嬪墠鐨凧ava鐗堟湰鍏煎錛?a >Apache Commons ID欏圭洰鍜?a >Java UUID Generator (JUG)欏圭洰銆傚畠浠湪Apache License涔嬩笅閮芥槸鍙敤鐨勶紙鍦↙GPL涔嬩笅JUG涔熸槸鍙敤鐨勶級銆?/p>
銆銆 榪欐槸浣跨敤JUG搴撳疄鐜癐dGenerator鐨勪緥瀛愶細
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(); } }
銆銆Hibernate涓唴緗殑UUID鐢熸垚鍣ㄧ畻娉曞張濡備綍鍛紵榪欐槸鑾峰緱瀵硅薄韜喚鐨刄UID鐨勯傚綋閫斿緞鍚楋紵濡傛灉鎮ㄦ兂璁╁璞¤韓浠界嫭绔嬩簬瀵硅薄鎸佷箙鎬э紝榪欏氨涓嶆槸涓涓ソ鏂規硶銆傝櫧鐒禜ibernate紜疄鎻愪緵浜嗙敓鎴怳UID鐨勯夐」錛屼絾榪欐牱鐨勮瘽鎴戜滑鍙堝洖鍒頒簡鏈鏃╃殑閭d釜闂涓婏細瀵硅薄ID鐨勮幏寰楀茍涓嶅湪瀹冧滑琚垱寤虹殑鏃跺欙紝鑰屾槸鍦ㄥ畠浠淇濆瓨鐨勬椂鍊欍?/p>
銆銆 浣跨敤UUID浣滀負鏁版嵁搴撲富閿殑鏈澶ч殰紕嶆槸瀹冧滑鍦ㄦ暟鎹簱涓紙鑰屼笉鏄湪鍐呭瓨涓級鐨勫ぇ灝忥紝鍦ㄦ暟鎹簱涓儲寮曞拰澶栭敭鐨勫鍚堜細淇冧嬌涓婚敭澶у皬鐨勫鍔犮傛偍蹇呴』鍦ㄤ笉鍚屾儏鍐典笅浣跨敤涓嶅悓鐨勮〃紺烘柟娉曘備嬌鐢⊿tring琛ㄧず錛屾暟鎹簱鐨勪富閿ぇ灝忓皢浼氭槸32鎴?6瀛楄妭銆傛暟瀛椾篃鍙互鐩存帴浠ュ瓧鑺傚瓨鍌紝榪欐牱澶у皬灝卞噺灝戜竴鍗婏紝浣嗘槸濡傛灉鐩存帴鏌ヨ鏁版嵁搴擄紝鏍囪瘑絎﹀皢鍙樺緱闅句互鐞嗚В銆傝繖浜涙柟娉曞鎮ㄧ殑欏圭洰鏄惁鍙鍙栧喅浜庢偍鐨勯渶姹傘?/p>
銆銆 濡傛灉鏁版嵁搴撲笉鎺ュ彈UUID浣滀負涓婚敭錛屾偍鍙互鑰冭檻浣跨敤鏁版嵁搴撳簭鍒椼備絾鎬繪槸搴旇鍦ㄦ柊瀵硅薄鍒涘緩鐨勬椂鍊欒鎸囨淳涓涓狪D鑰屼笉鏄Hibernate綆$悊ID銆傚湪榪欑鎯呭喌涓嬶紝鍒涘緩鏂板煙瀵硅薄鐨勪笟鍔″璞″彲浠ヨ皟鐢ㄤ竴涓嬌鐢ㄦ暟鎹闂璞★紙DAO錛変粠鏁版嵁搴撳簭鍒椾腑媯绱d鐨勬湇鍔°傚鏋滀嬌鐢ㄤ竴涓狶ong鏁版嵁綾誨瀷鏉ヨ〃紺哄璞d錛屼竴涓崟鐙殑鏁版嵁搴撳簭鍒楋紙浠ュ強鏈嶅姟鏂規硶錛夊鎮ㄧ殑鍩熷璞℃潵璇村氨宸茬粡瓚沖浜嗐?
銆銆褰撳璞℃寔涔呭瓨鍌ㄥ埌鏁版嵁搴撲腑鏃訛紝瀵硅薄韜喚鎬繪槸寰堥毦琚伆褰撳湴瀹炵幇銆傚敖綆″姝わ紝闂鍏跺疄瀹屽叏鍦ㄤ簬錛屽璞″湪淇濆瓨涔嬪墠鍏佽瀵硅薄娌℃湁id灝卞瓨鍦ㄣ傛垜浠彲浠ラ氳繃浠庤濡侶ibernate榪欐牱鐨勫璞″叧緋繪槧灝勬鏋朵腑鑾峰緱鎸囨淳瀵硅薄ID鐨勮亴璐f潵瑙e喅榪欎釜闂銆備竴鏃﹀璞¤瀹炰緥鍖栵紝瀹冨氨搴旇琚寚媧句竴涓狪D銆傝繖浣垮璞¤韓浠藉彉寰楃畝鍗曡屼笉鏄撳嚭閿欙紝涔熷噺灝戜簡鍩熸ā鍨嬩腑闇瑕佺殑浠g爜閲忋?/p>