Just Hibernate Madhusudhan Konda Just Hibernate by Madhusudhan Konda Copyright © 2014 Madhusudhan Konda. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected]. Editors: Meghan Blanchette and Brian Anderson Indexer: Ellen Troutman-Zaig Production Editor: Melanie Yarbrough Cover Designer: Karen Montgomery Copyeditor: Rachel Monaghan Interior Designer: David Futato Proofreader: Jasmine Kwityn Illustrator: Rebecca Demarest June 2014: First Edition Revision History for the First Edition: 2014-06-04: First release 2014-06-27: Second release See http://oreilly.com/catalog/errata.csp?isbn=9781449334376 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Just Hibernate, the cover image of a garden dormouse, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-33437-6 [LSI] In memory of my loving Dad; we all miss you! Table of Contents Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi 1. Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Birth of Hibernate 1 Problem Domain 3 MovieManager Application 3 Improvising the Movie Application 6 Using Hibernate 8 Configure the Database Connection 9 Create Mapping Definitions 10 Persist the Objects 11 Creating the Persist Method 12 Testing the Persisted Data 13 Setting Up Hibernate 14 Summary 15 2. Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Object-Relational Mismatch 17 Inheritance Mismatch 18 Identity Mismatch 18 Relations and Associations Mismatch 19 Hibernate’s Main Parts 19 Persistent Classes 20 Example: Trading Application 20 Using Annotations 20 Configuration 22 Using a Properties File 22 Using the XML File 23 v Configuration Properties 23 Programmatic Configuration 23 Mapping 24 XML Mapping Files 24 Identifier Generation Strategies 26 Session APIs 27 Transactions 28 Summary 30 3. Annotations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Working Through an Example 31 Digging into Details 33 ID Generation Strategies 34 Composite Identifiers 36 Using Primary Key Class and @Id 36 Using Primary Key Class and @EmbeddedId 37 Using @IdClass 38 Summary 39 4. Persisting Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Designing to Interfaces 41 Persisting Lists 42 List Example: Car Showroom 42 Test Client for List Persistence 44 Persisting Sets 45 Persisting Maps 47 Persisting Arrays 49 Persisting Bags and IdBags 50 Persisting Collections Using Annotations 52 Using a Foreign Key 52 Using a Join Table 54 Summary 55 5. Associations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Associations 57 Multiplicity 58 Directionality 59 One-to-One Association 60 Using a Primary Key 61 Testing the Association 63 Using a Foreign Key 64 Annotations 66 vi | Table of Contents One-to-Many (or Many-to-One) Association 67 Bidirectional One-to-Many Association 70 Many-to-Many Association 71 Summary 72 6. Advanced Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Hibernate Types 73 Entity and Value Types 73 Custom Types 74 Components 75 Caching 77 First-Level Caching 77 Second-Level Caching 78 Caching Queries 79 Inheritance Strategies 79 Table-per-Class Strategy 79 Table-per-Subclass Strategy 83 Table-per-Concrete-Class Strategy 85 Table-per-Concrete-Class Strategy Using XML mapping 85 Filters 87 Creating Filter Definitions 87 Enabling Filters 88 Relationship Owner (aka Inverse Attribute) 89 Cascading Entities 89 Summary 91 7. Hibernate Query Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Working with the Query Class 93 Fetching All Rows 94 Pagination 96 Retrieving a Unique Record 96 Named Parameters 96 Using the IN option 97 Positional Parameters 98 Aliases 98 Iterators 99 Selects 99 Aggregate Functions 100 Updates and Deletes 101 Criterias 101 Named Queries 102 Native SQL 104 Table of Contents | vii Summary 104 8. Java Persistence API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Hibernate and JPA 106 Persistence Context 106 EntityManagerFactory 108 EntityManager 109 Persisting Objects 110 Saving and Querying Entities 110 Summary 111 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 viii | Table of Contents