this print for content only—size & color not accurate spine = 0.838" 360 page count EMPOWERING PRODUCTIVITY FOR THE JAVA ™ DEVELOPER Beginning Hibernate: From Novice to Professional Dear Reader, Hibernate lets you save normal Java™ objects into a relational database, and retrieve them without having to write a line of SQL. It makes database persist- ence as natural to use as other Java ™ libraries. More and more companies are using Hibernate in their systems. In our view, this trend is inevitable and unstoppable—once you have successfully shipped a project with Hibernate, there is no going back. And Hibernate’s support for and influence over the EJB ™ 3 specification gives it tremendous credibility in any organization that prefers standards to proprietary solutions. Hibernate is definitely a great product, but it is not a simple one. We take a pragmatic view of the benefits of tools, and we believe that the best way to learn any new tool is to use it. To that end, we have provided lots of simple working examples of all the features we describe. We believe that you will find in this book everything that you need to build a fully functional Hibernate-based application and become a Hibernate aficionado. Our aim is to give you a firm understanding of the basic features, such as creating mapping files and querying databases. With this understanding of the fundamental features, you will then be in a great position to take advantage of the more advanced or obscure features that we discuss in later chapters and appendixes, including the use of the Hibernate plug-ins for Eclipse and Ant, the EJB ™ 3 EntityManager, and the integration of Hibernate with the Spring API. We congratulate you on your choice of Hibernate, and wish you luck in all your endeavors. Dave Minter and Jeff Linwood Dave Minter, coauthor of Building Portals with the Java ™ Portlet API Pro Hibernate 3 US $39.99 Shelve in Java Programming User level: Beginner–Intermediate Hibernate Minter, Linwood THE EXPERT’S VOICE ® IN JAVA ™ TECHNOLOGY Dave Minter and Jeff Linwood Beginning Hibernate From Novice to Professional CYAN MAGENTA YELLOW BLACK PANTONE 123 CV ISBN 1-59059-693-5 9 781590 596937 53999 6 89253 59693 7 Jeff Linwood, coauthor of Professional Struts Applications Building Portals with the Java ™ Portlet API Pro Hibernate 3 Companion eBook Available An introduction to all the new features of the Hibernate 3.2 persistence API www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version THE APRESS ROADMAP Beginning Hibernate Beginning POJOs Pro Apache Geronimo Beginning Spring 2 Pro Spring Expert Spring MVC and Web Flow forums.apress.com FOR PROFESSIONALS BY PROFESSIONALS ™ Join online discussions: Beginning Covers Hibernate 3.2 Covers Hibernate 3.2 Covers Hibernate 3.2 Dave Minter and Jeff Linwood Beginning Hibernate From Novice to Professional 6935fm_final.qxd 8/2/06 9:44 PM Page i B eginning Hibernate: From Novice to Professional Copyright © 2006 by Dave Minter, Jeff Linwood All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-693-7 ISBN-10 (pbk): 1-59059-693-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries. Apress, Inc. is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc. Lead Editor: Steve Anglin Technical Reviewer: Sumit Pal Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Senior Project Manager: Kylie Johnston Copy Edit Manager: Nicole LeClerc Copy Editor: Damon Larson Assistant Production Director: Kari Brooks-Copony Senior Production Editor: Laura Cheu Compositor: Linda Weidemann, Wolf Creek Press Proofreader: April Eddy Indexer: Michael Brinkman Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code section. 6935fm_final.qxd 8/2/06 9:44 PM Page ii Contents at a Glance About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix ■CHAPTER 1 An Introduction to Hibernate 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■CHAPTER 2 Integrating and Configuring Hibernate. . . . . . . . . . . . . . . . . . . . . . . . . . 11 ■CHAPTER 3 Building a Simple Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 ■CHAPTER 4 The Persistence Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 ■CHAPTER 5 An Overview of Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 ■CHAPTER 6 Mapping with Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 ■CHAPTER 7 Creating Mappings with Hibernate XML Files . . . . . . . . . . . . . . . . . . 139 ■CHAPTER 8 Using the Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 ■CHAPTER 9 Searches and Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 ■CHAPTER 10 Advanced Queries Using Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 ■CHAPTER 11 Filtering the Results of Searches. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 ■APPENDIX A More Advanced Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 ■APPENDIX B Hibernate Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 ■APPENDIX C Hibernate and Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 ■APPENDIX D Upgrading from Hibernate 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 iii 6935fm_final.qxd 8/2/06 9:44 PM Page iii 6935fm_final.qxd 8/2/06 9:44 PM Page iv Contents About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix ■CHAPTER 1 An Introduction to Hibernate 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Plain Old Java Objects (POJOs). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Origins of Hibernate and Object-Relational Mapping . . . . . . . . . . . . . . . . . . 3 EJBs As a Persistence Solution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Hibernate As a Persistence Solution. . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 A Thin Solution?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 A Hiberna te Hello World Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Database Generation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 The Rela tionship of Hiberna te 3 with EJB 3.0 . . . . . . . . . . . . . . . . . . . . . . . . 8 Summar y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 ■CHAPTER 2 Integrating and Configuring Hibernate . . . . . . . . . . . . . . . . . . . . 11 The Steps Needed to Integrate and Configure Hibernate . . . . . . . . . . . . . . 11 Understanding Where Hibernate Fits in Your Java Application . . . . . . . . . 12 Deploying Hibernate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Required Libraries for Running Hibernate 3 . . . . . . . . . . . . . . . . . . . . 13 Annotations and Enterprise JavaBeans 3 . . . . . . . . . . . . . . . . . . . . . . 14 JMX and Hibernate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Hibernate Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Hibernate Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 XML Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Ma pping Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Naming Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Using a Container -Mana ged Data Source . . . . . . . . . . . . . . . . . . . . . . 22 The Session F actory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 SQL Dialects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 v 6935fm_final.qxd 8/2/06 9:44 PM Page v ■CHAPTER 3 Building a Simple Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Installing the Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Hibernate and Hibernate Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 HSQLDB 1.8.0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Ant 1.6.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 The Ant Tasks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Enabling Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Creating a Hibernate Configuration File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Running the Message Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Persisting Multiple Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Creating Persistence Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Creating the Object Mappings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Creating the Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 The Session and Related Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Using the Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Building DAOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 The Example Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 ■CHAPTER 4 The Persistence Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Introduction to the Life Cyc le . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Entities, Classes, and Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Entities and Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Saving Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Object Equality and Identity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Loading Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Refreshing Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Updating Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Deleting Entities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Cascading Opera tions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Lazy Loading, Proxies, and Collection Wrappers . . . . . . . . . . . . . . . . . . . . . 76 Querying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 ■CHAPTER 5 An Overview of Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Why Ma pping Cannot Be Automa ted . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 ■CONTENTSvi 6935fm_final.qxd 8/2/06 9:44 PM Page vi Lazy Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 The One-to-One Association . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 The One-to-Many and Many-to-One Association. . . . . . . . . . . . . . . . 87 The Many-to-Many Association. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Applying Mappings to Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Types of Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Other Information Represented in Mappings . . . . . . . . . . . . . . . . . . . . . . . . 90 Specification of (Database) Column Types and Sizes . . . . . . . . . . . . 90 The Mapping of Inheritance Relationships to the Database. . . . . . . 90 Primary Key. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 The Use of SQL Formula–Based Properties . . . . . . . . . . . . . . . . . . . . 91 Mandatory and Unique Constraints. . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Cascading of Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 ■CHAPTER 6 Mapping with Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Java 5 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Creating Hibernate Ma ppings with Annotations . . . . . . . . . . . . . . . . . . . . . . 93 Cons of Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Pros of Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Choosing Which to Use. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Using Annotations in Your Application . . . . . . . . . . . . . . . . . . . . . . . . . 96 EJB 3 P ersistence Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Entity Beans with @Entity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Primary Keys with @Id and @GeneratedValue. . . . . . . . . . . . . . . . . 101 Genera ting Primary Key Values with @SequenceGenerator . . . . . 103 Generating Primary Key Values with @TableGenerator . . . . . . . . . 104 Compound Primar y Keys with @Id, @IdClass, or @EmbeddedId . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Da tabase T able Ma pping with @T able and @SecondaryTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 P ersisting Basic T ypes with @Basic . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Omitting P ersistence with @T ransient . . . . . . . . . . . . . . . . . . . . . . . . 112 Ma pping Properties and Fields with @Column . . . . . . . . . . . . . . . . . 112 Modeling Entity Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Other EJB 3 Persistence Annotations. . . . . . . . . . . . . . . . . . . . . . . . . 122 Configuring the Annotated Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 ■CONTENTS vii 6935fm_final.qxd 8/2/06 9:44 PM Page vii Hibernate 3–Specific Persistence Annotations. . . . . . . . . . . . . . . . . . . . . . 126 @Entity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Sorting Collections with @Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Ordering Collections with @IndexColumn. . . . . . . . . . . . . . . . . . . . . 129 Applying Indexes with @Table and @Index . . . . . . . . . . . . . . . . . . . 130 Restricting Collections with @Where . . . . . . . . . . . . . . . . . . . . . . . . . 130 Alternative Key Generation Strategies with @GenericGenerator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Using Ant with Annotation-Based Mappings . . . . . . . . . . . . . . . . . . . . . . . 131 Code Listings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 ■CHAPTER 7 Creating Mappings with Hibernate XML Files. . . . . . . . . . . . 139 Hibernate Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Entities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 The Anatomy of a Mapping File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 The <hibernate-mapping> Element. . . . . . . . . . . . . . . . . . . . . . . . . . 141 The <class> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 The <id> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 The <property> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 The <component> Element. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 The <one-to-one> Element. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 The <man y-to-one> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 The Collection Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Ma pping Simple Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Mapping Composition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Ma pping Other Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Mapping Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Ma pping Inheritance Rela tionships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 One Table per Concrete Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 One T able per Subc lass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 One T able per Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 More Exotic Ma ppings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 The any Tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 The array Tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 The <dynamic-component> Element . . . . . . . . . . . . . . . . . . . . . . . . 177 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 ■CONTENTSviii 6935fm_final.qxd 8/2/06 9:44 PM Page viii ■CHAPTER 8 Using the Session. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Transactions and Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Deadlocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 ■CHAPTER 9 Searches and Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 HQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Syntax Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 UPDA TE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 INSERT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 The First Example with HQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Logging the Underlying SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Commenting the Generated SQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 The from Clause and Aliases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 The select Clause and Projection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Using Restrictions with HQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Using Named Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Paging Through the Result Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Obtaining a Unique Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Sorting Results with the order by Clause . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Aggrega te Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Bulk Updates and Deletes with HQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Named Queries for HQL and SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Using Na tive SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 ■CONTENTS ix 6935fm_final.qxd 8/2/06 9:44 PM Page ix [...]... ibernate is an amazing piece of software With a little experience and the power of Java 5 annotations, you can build a complex database-backed system with disturbing ease Once you have built a system using Hibernate, you will never want to go back to the traditional approaches While Hibernate is incredibly powerful, it presents a steep learning curve when you first encounter it—steep learning curves are... will want to turn to the appendixes for discussion of more arcane topics How This Book Is Structured This book is informally divided into three parts Chapters 1 through 8 describe the fundamentals of Hibernate, including configuration, the creation of mapping files, and the basic APIs Chapters 9 through 11 then describe the use of queries, criteria, and filters to access the persistent information... of the methods available on the core interfaces It also introduces key terminology and discusses the need for cascading and lazy loading Chapter 5 explains why mapping information must be retained by Hibernate, and demonstrates the various types of associations that can be represented by a relational database It briefly discusses the other information that can be maintained within a Hibernate mapping... the Apress web site and download all the code from there Contacting the Authors We welcome feedback from our readers If you have any queries or suggestions about this book, or technical questions about Hibernate, or if you just want to share a really good joke, you can e-mail Dave Minter at dave@paperstack.com and Jeff Linwood at jlinwood@gmail.com xxi 6935fm_final.qxd 8/2/06 9:44 PM Page xxii 6935ch01_final.qxd . details on $10 eBook version THE APRESS ROADMAP Beginning Hibernate Beginning POJOs Pro Apache Geronimo Beginning Spring 2 Pro Spring Expert Spring MVC and. PROFESSIONALS ™ Join online discussions: Beginning Covers Hibernate 3.2 Covers Hibernate 3.2 Covers Hibernate 3.2 Dave Minter and Jeff Linwood Beginning Hibernate From