ebook img

Beginning Hibernate PDF

391 Pages·2010·8.377 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Beginning Hibernate

Beginning Hibernate Second Edition ■ ■ ■ Jeff Linwood and Dave Minter Beginning Hibernate, Second Edition Copyright © 2010 by Jeff Linwood and Dave Minter 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-4302-2850-9 ISBN-13 (electronic): 978-1-4302-2851-6 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. President and Publisher: Paul Manning Lead Editor: Jonathan Gennick Technical Reviewer: Sumit Pal Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Debra Kelly Copy Editor: Sharon Terdeman Production Support: Patrick Cunningham Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko 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 [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. 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 www.apress.com. Contents at a Glance About the Authors ..................................................................................................... xv About the Technical Reviewer ................................................................................. xvi Acknowledgments .................................................................................................. xvii Introduction ........................................................................................................... xviii ■Chapter 1: An Introduction to Hibernate 3.5 ............................................................ 1 ■Chapter 2: Integrating and Configuring Hibernate................................................... 9 ■Chapter 3: Building a Simple Application .............................................................. 27 ■Chapter 4: The Persistence Life Cycle ................................................................... 61 ■Chapter 5: An Overview of Mapping ...................................................................... 77 ■Chapter 6: Mapping with Annotations ................................................................... 91 ■Chapter 7: Creating Mappings with Hibernate XML Files ................................... 133 ■Chapter 8: Using the Session ............................................................................... 177 ■Chapter 9: Searches and Queries ........................................................................ 193 ■Chapter 10: Advanced Queries Using Criteria...................................................... 215 ■Chapter 11: Filtering the Results of Searches ..................................................... 227 ■Chapter 12: Case Study – Using Hibernate with an Existing Database ................ 235 iii ■ CONTENTS AT A GLANCE ■Appendix A: More Advanced Features ................................................................. 263 ■Appendix B: Hibernate Tools................................................................................ 287 ■Appendix C: Hibernate and Spring ....................................................................... 327 ■Appendix D: Upgrading from Hibernate 2 ............................................................ 337 Index ....................................................................................................................... 343 iv Contents About the Authors ..................................................................................................... xv About the Technical Reviewer ................................................................................. xvi Acknowledgments .................................................................................................. xvii Introduction ........................................................................................................... xviii ■Chapter 1: An Introduction to Hibernate 3.5 ............................................................ 1 Plain Old Java Objects (POJOs) .......................................................................................... 1 Origins of Hibernate and Object-Relational Mapping ........................................................ 3 Hibernate As a Persistence Solution .................................................................................. 4 A Hibernate Hello World Example ...................................................................................... 5 Mappings ........................................................................................................................... 6 Summary ........................................................................................................................... 7 ■Chapter 2: Integrating and Configuring Hibernate................................................... 9 The Steps Needed to Integrate and Configure Hibernate .................................................. 9 Understanding Where Hibernate Fits in Your Java Application ....................................... 10 Deploying Hibernate ........................................................................................................ 11 Required Libraries for Running Hibernate 3.5 ........................................................................................ 11 JMX and Hibernate ................................................................................................................................. 12 v ■ CONTENTS Hibernate Configuration ................................................................................................... 12 Hibernate Properties ............................................................................................................................... 14 XML Configuration .................................................................................................................................. 18 Annotated Classes .................................................................................................................................. 19 Naming Strategy ..................................................................................................................................... 20 Using a Container-Managed Data Source............................................................................................... 21 The Session Factory ........................................................................................................ 21 SQL Dialects ..................................................................................................................... 22 Summary ......................................................................................................................... 25 ■Chapter 3: Building a Simple Application .............................................................. 27 Installing the Tools ........................................................................................................... 27 Hibernate and Hibernate Tools ............................................................................................................... 27 Simple Logging Facade for Java (SLF4J) 1.5.11 .................................................................................... 28 HSQLDB 2.0.0 ......................................................................................................................................... 28 Ant 1.8.0 ................................................................................................................................................. 29 The Ant Tasks ......................................................................................................................................... 32 Creating a Hibernate Configuration File ........................................................................... 33 Running the Message Example ....................................................................................... 35 Persisting Multiple Objects .............................................................................................. 38 Creating Persistence Classes .......................................................................................... 39 Creating the Object Mappings ......................................................................................... 42 Creating the Tables .......................................................................................................... 48 Sessions .......................................................................................................................... 50 The Session and Related Objects ........................................................................................................... 50 Using the Session ................................................................................................................................... 52 The Example Client .......................................................................................................... 54 Summary ......................................................................................................................... 59 vi ■ CONTENTS ■Chapter 4: The Persistence Life Cycle ................................................................... 61 Introduction to the Life Cycle ........................................................................................... 61 Entities, Classes, and Names .......................................................................................... 62 Identifiers ......................................................................................................................... 63 Entities and Associations ................................................................................................. 63 Saving Entities ................................................................................................................. 67 Object Equality and Identity ............................................................................................. 68 Loading Entities ............................................................................................................... 69 Refreshing Entities........................................................................................................... 70 Updating Entities .............................................................................................................. 71 Deleting Entities ............................................................................................................... 72 Cascading Operations ...................................................................................................... 72 Lazy Loading, Proxies, and Collection Wrappers ............................................................. 74 Querying Objects ............................................................................................................. 75 Summary ......................................................................................................................... 75 ■Chapter 5: An Overview of Mapping ...................................................................... 77 Why Mapping Cannot Be Automated ............................................................................... 78 Primary Keys .................................................................................................................... 80 Lazy Loading .................................................................................................................... 82 Associations ..................................................................................................................... 82 The One-to-One Association ................................................................................................................... 84 The One-to-Many and Many-to-One Association ................................................................................... 86 The Many-to-Many Association .............................................................................................................. 88 Applying Mappings to Associations ........................................................................................................ 89 vii ■ CONTENTS Other Information Represented in Mappings ................................................................... 89 Specification of (Database) Column Types and Sizes ............................................................................. 89 The Mapping of Inheritance Relationships to the Database ................................................................... 89 Primary Key ............................................................................................................................................ 90 The Use of SQL Formula–Based Properties ............................................................................................ 90 Mandatory and Unique Constraints ........................................................................................................ 90 Cascading of Operations ......................................................................................................................... 90 Summary ......................................................................................................................... 90 ■Chapter 6: Mapping with Annotations ................................................................... 91 Creating Hibernate Mappings with Annotations .............................................................. 91 Cons of Annotations ................................................................................................................................ 91 Pros of Annotations ................................................................................................................................ 91 Choosing Which to Use ........................................................................................................................... 93 Using Annotations in Your Application .................................................................................................... 93 JPA 2 Persistence Annotations ............................................................................................................... 93 Entity Beans with @Entity ...................................................................................................................... 95 Primary Keys with @Id and @GeneratedValue ....................................................................................... 95 Generating Primary Key Values with @SequenceGenerator .................................................................. 97 Generating Primary Key Values with @TableGenerator.......................................................................... 98 Compound Primary Keys with @Id, @IdClass, or @EmbeddedId ........................................................... 99 Database Table Mapping with @Table and @SecondaryTable ............................................................ 103 Persisting Basic Types with @Basic..................................................................................................... 105 Omitting Persistence with @Transient ................................................................................................. 105 Mapping Properties and Fields with @Column .................................................................................... 106 Modeling Entity Relationships .............................................................................................................. 107 Inheritance ............................................................................................................................................ 114 Other JPA 2 Persistence Annotations ................................................................................................... 117 Ordering Collections with @OrderColumn ............................................................................................ 119 Configuring the Annotated Classes ................................................................................ 121 viii ■ CONTENTS Hibernate 3–Specific Persistence Annotations .............................................................. 122 @Entity ................................................................................................................................................. 123 Sorting Collections with @Sort ............................................................................................................. 123 Applying Indexes with @Table and @Index ......................................................................................... 124 Restricting Collections with @Where ................................................................................................... 124 Alternative Key Generation Strategies with @GenericGenerator .......................................................... 124 Using Ant with Annotation-Based Mappings ................................................................. 125 Code Listings ................................................................................................................. 126 Summary ....................................................................................................................... 131 ■Chapter 7: Creating Mappings with Hibernate XML Files ................................... 133 Hibernate Types ............................................................................................................. 133 Entities .................................................................................................................................................. 133 Components .......................................................................................................................................... 134 Values ................................................................................................................................................... 134 The Anatomy of a Mapping File ..................................................................................... 136 The <hibernate-mapping> Element ..................................................................................................... 136 The <class> Element ........................................................................................................................... 138 The <id> Element ................................................................................................................................. 141 The <property> Element ...................................................................................................................... 144 The <component> Element .................................................................................................................. 146 The <one-to-one> Element .................................................................................................................. 147 The <many-to-one> Element ............................................................................................................... 149 The Collection Elements ....................................................................................................................... 152 Mapping Simple Classes................................................................................................ 160 Mapping Composition .................................................................................................... 162 Mapping Other Associations .......................................................................................... 165 Mapping Collections ...................................................................................................... 168 ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.