CYAN YELLOW MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN JAVA™ TECHNOLOGY Companion eBook Available Pro JPA 2: Mastering the Java™ Java™ EE 6 Pro compliant Persistence API J Dear Reader, Pro It’s hard to believe that over 3 years have gone by since the initial 1.0 release of the Java™ Persistence API. In that time we have seen it go from fledgling API P to mainstream persistence standard. Along the way, many of you cut your JPA teeth using the first edition of this book, and we’re happy we were there to help! JPA 2.0 includes a host of new features, such as additional object-relational A JPA 2 mappings, more object modeling flexibility, typed queries, and a brand-new criteria API, to name a few. With so much to talk about, we were excited to update the book and explain all the new features, but we also included some hints and tips to help you use the API in practice. If you already have experience with JPA 1.0 then you should benefit from 2 the version tips that point out when a feature was added in 2.0. These tips were also designed to help users who are writing to a JPA 1.0 implementation, and are not yet able to make use of the 2.0 features. Those of you that are new to JPA can rest assured that you were not for- Authors of gotten. We have remained true to our original intent to take someone from Pro EJB™ 3: Java™ having no JPA knowledge all the way to being an advanced JPA’er. You should Persistence API be able to quickly learn in the first two chapters what you need to know to get started. (Veteran JPA programmers might want to start at Chapter 3!) Finally, Mastering the Java Persistence API we want to thank you for making the previous edition of this book such a suc- ™ cess. We are pleased that it has become the primary resource for JPA develop- ers, and hope that you will find this edition equally valuable. Mike Keith, JPA 2.0 Expert Group Member, and Merrick Schincariol Create robust, data-driven applications with Companion eBook ES this definitive guide to the new JPA 2 L T TI D E T LA Java™ EE 6 See last page for details E R on $10 eBook version compliant S c h Mike Keith and Merrick Schincariol SOURCE CODE ONLINE in www.apress.com ISBN 978-1-4302-1956-9 cK a 54999 rioeit Foreword by Linda DeMichiel, JPA Specification Lead h l US $49.99 Shelve in Java Programming User level: 9 781430 219569 Intermediate this print for content only—size & color not accurate Trim: 7.5 x 9.25 spine = 1.0" 536 page count CYAN YELLOW MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN JAVA™ TECHNOLOGY Companion eBook Available Pro JPA 2: Mastering the Java™ Java™ EE 6 Pro compliant Persistence API J Dear Reader, Pro It’s hard to believe that over 3 years have gone by since the initial 1.0 release of the Java™ Persistence API. In that time we have seen it go from fledgling API P to mainstream persistence standard. Along the way, many of you cut your JPA teeth using the first edition of this book, and we’re happy we were there to help! JPA 2.0 includes a host of new features, such as additional object-relational A JPA 2 mappings, more object modeling flexibility, typed queries, and a brand-new criteria API, to name a few. With so much to talk about, we were excited to update the book and explain all the new features, but we also included some hints and tips to help you use the API in practice. If you already have experience with JPA 1.0 then you should benefit from 2 the version tips that point out when a feature was added in 2.0. These tips were also designed to help users who are writing to a JPA 1.0 implementation, and are not yet able to make use of the 2.0 features. Those of you that are new to JPA can rest assured that you were not for- Authors of gotten. We have remained true to our original intent to take someone from Pro EJB™ 3: Java™ having no JPA knowledge all the way to being an advanced JPA’er. You should Persistence API be able to quickly learn in the first two chapters what you need to know to get started. (Veteran JPA programmers might want to start at Chapter 3!) Finally, Mastering the Java Persistence API we want to thank you for making the previous edition of this book such a suc- ™ cess. We are pleased that it has become the primary resource for JPA develop- ers, and hope that you will find this edition equally valuable. Mike Keith, JPA 2.0 Expert Group Member, and Merrick Schincariol Create robust, data-driven applications with Companion eBook ES this definitive guide to the new JPA 2 L T TI D E T LA Java™ EE 6 See last page for details E R on $10 eBook version compliant S c h Mike Keith and Merrick Schincariol SOURCE CODE ONLINE in www.apress.com ISBN 978-1-4302-1956-9 cK a 54999 rioeit Foreword by Linda DeMichiel, JPA Specification Lead h l US $49.99 Shelve in Java Programming User level: 9 781430 219569 Intermediate this print for content only—size & color not accurate Trim: 7.5 x 9.25 spine = 1.0" 536 page count Pro JPA 2 Mastering the Java™ Persistence API ■ ■ ■ Mike Keith and Merrick Schnicariol i Pro JPA 2 Copyright © 2009 by Mike Keith and Merrick Schincariol 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-1956-9 ISBN-13 (electronic): 978-1-4302-1957-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: Steve Anglin, Tom Welsh Technical Reviewer: Jim Farley Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Mary Tobin Copy Editor: Nancy Sixsmith Compositor: MacPS, LLC Indexer: BIM Indexers and e-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 orders-ny@springer- sbm.com, or visit http://www.springeronline.com. For information on translations, please e-mail [email protected], or visit http://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 http://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 http://www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. ii To the memory of my father, who selflessly offered all that he had, and to my wife Darleen who has devoted her life to helping children.—Mike To Anthony, whose boundless creativity continues to inspire me. To Evan, whose boisterous enthusiasm motivates me to take on new challenges. To Kate, who proves that size is no object when you have the right attitude. I love you all.—Merrick iii Contents at a Glance ■Contents at a Glance............................................................................................iv(cid:1) ■Contents................................................................................................................v(cid:1) ■Foreword.............................................................................................................xx(cid:1) ■About the Author................................................................................................xxi(cid:1) ■About the Technical Reviewer...........................................................................xxii(cid:1) ■Acknowledgments............................................................................................xxiii(cid:1) ■Preface...............................................................................................................xiv(cid:1) ■Chapter 1: Introduction.........................................................................................1(cid:1) ■Chapter 2: Getting Started...................................................................................17(cid:1) ■Chapter 3: Enterprise Applications.....................................................................33(cid:1) ■Chapter 4: Object-Relational Mapping................................................................69(cid:1) ■Chapter 5: Collection Mapping..........................................................................107(cid:1) ■Chapter 6: Entity Manager................................................................................131(cid:1) ■Chapter 7: Using Queries...................................................................................179(cid:1) ■Chapter 8: Query Language...............................................................................207(cid:1) ■Chapter 9: Criteria API......................................................................................239(cid:1) ■Chapter 10: Advanced Object-Relational Mapping............................................273(cid:1) ■Chapter 11: Advanced Topics............................................................................315(cid:1) ■Chapter 12: XML Mapping Files........................................................................371(cid:1) ■Chapter 13: Packaging and Deployment...........................................................407(cid:1) ■Chapter 14: Testing...........................................................................................429(cid:1) ■Chapter 15: Migration.......................................................................................457 ■Index.................................................................................................................481 iv Contents ■Contents at a Glance............................................................................................iv(cid:1) ■Contents................................................................................................................v(cid:1) ■Foreword.............................................................................................................xx ■About the Author................................................................................................xxi ■About the Technical Reviewer...........................................................................xxii(cid:1) ■Acknowledgments............................................................................................xxiii(cid:1) ■Preface.............................................................................................................xxiv ■Chapter 1: Introduction.........................................................................................1(cid:1) Object-Relational Mapping.............................................................................................2(cid:1) The Impedance Mismatch......................................................................................................................3(cid:1) Class Representation........................................................................................................................3(cid:1) Relationships....................................................................................................................................5(cid:1) Inheritance........................................................................................................................................7(cid:1) Java Support for Persistence..........................................................................................9(cid:1) Proprietary Solutions..............................................................................................................................9(cid:1) JDBC......................................................................................................................................................9(cid:1) Enterprise JavaBeans..........................................................................................................................10(cid:1) Java Data Objects................................................................................................................................10(cid:1) Why Another Standard?................................................................................................11(cid:1) The Java Persistence API..............................................................................................12(cid:1) History of the Specification..................................................................................................................12(cid:1) EJB 3.0 and JPA 1.0........................................................................................................................12(cid:1) JPA 2.0............................................................................................................................................13(cid:1) JPA and You....................................................................................................................................13(cid:1) Overview..............................................................................................................................................13(cid:1) POJO Persistence............................................................................................................................13(cid:1) Nonintrusiveness............................................................................................................................14(cid:1) Object Queries.................................................................................................................................14(cid:1) v ■ CONTENTS Mobile Entities................................................................................................................................14(cid:1) Simple Configuration.......................................................................................................................15(cid:1) Integration and Testability..............................................................................................................15(cid:1) Summary.......................................................................................................................15(cid:1) ■Chapter 2: Getting Started...................................................................................17(cid:1) Entity Overview.............................................................................................................17(cid:1) Persistability........................................................................................................................................17(cid:1) Identity.................................................................................................................................................18(cid:1) Transactionality....................................................................................................................................18(cid:1) Granularity...........................................................................................................................................18(cid:1) Entity Metadata.............................................................................................................19(cid:1) Annotations..........................................................................................................................................19(cid:1) XML......................................................................................................................................................19(cid:1) Configuration by Exception..................................................................................................................19(cid:1) Creating an Entity..........................................................................................................20(cid:1) Entity Manager..............................................................................................................22(cid:1) Obtaining an Entity Manager................................................................................................................23(cid:1) Persisting an Entity..............................................................................................................................24(cid:1) Finding an Entity..................................................................................................................................24(cid:1) Removing an Entity..............................................................................................................................25(cid:1) Updating an Entity................................................................................................................................26(cid:1) Transactions........................................................................................................................................26(cid:1) Queries.................................................................................................................................................27(cid:1) Putting It All Together...................................................................................................28(cid:1) Packaging It Up.............................................................................................................30(cid:1) Persistence Unit...................................................................................................................................30(cid:1) Persistence Archive.............................................................................................................................31(cid:1) Summary.......................................................................................................................32(cid:1) ■Chapter 3: Enterprise Applications.....................................................................33(cid:1) Application Component Models....................................................................................33(cid:1) Session Beans...............................................................................................................34(cid:1) Stateless Session Beans......................................................................................................................35(cid:1) Defining a Stateless Session Bean.................................................................................................35(cid:1) Lifecycle Callbacks.........................................................................................................................37(cid:1) Remote Business Interfaces...........................................................................................................38(cid:1) vi ■ CONTENTS Stateful Session Beans........................................................................................................................39(cid:1) Defining a Stateful Session Bean....................................................................................................39(cid:1) Lifecycle Callbacks.........................................................................................................................40(cid:1) Singleton Session Beans......................................................................................................................42(cid:1) Defining a Singleton Session Bean.................................................................................................42(cid:1) Lifecycle Callbacks.........................................................................................................................43(cid:1) Singleton Concurrency....................................................................................................................43(cid:1) Message-Driven Beans.................................................................................................45(cid:1) Defining a Message-Driven Bean.........................................................................................................45(cid:1) Servlets.........................................................................................................................46(cid:1) Dependency Management............................................................................................47(cid:1) Dependency Lookup.............................................................................................................................47(cid:1) Dependency Injection...........................................................................................................................49(cid:1) Field Injection..................................................................................................................................49(cid:1) Setter Injection................................................................................................................................50(cid:1) Declaring Dependencies......................................................................................................................51(cid:1) Referencing a Persistence Context.................................................................................................51(cid:1) Referencing a Persistence Unit.......................................................................................................52(cid:1) Referencing Enterprise JavaBeans.................................................................................................52(cid:1) Referencing Server Resources........................................................................................................53(cid:1) Transaction Management.............................................................................................53(cid:1) Transaction Review..............................................................................................................................54(cid:1) Enterprise Transactions in Java...........................................................................................................54(cid:1) Transaction Demarcation................................................................................................................55(cid:1) Container-Managed Transactions...................................................................................................56(cid:1) Bean-Managed Transactions..........................................................................................................58(cid:1) Using Java EE Components..........................................................................................60(cid:1) Using a Stateless Session Bean...........................................................................................................60(cid:1) Using a Stateful Session Bean.............................................................................................................60(cid:1) Using a Singleton Session Bean..........................................................................................................62(cid:1) Using a Message-Driven Bean.............................................................................................................62(cid:1) Adding the Entity Manager...................................................................................................................63(cid:1) Putting It All Together...................................................................................................64(cid:1) Defining the Component......................................................................................................................64(cid:1) Defining the User Interface..................................................................................................................66(cid:1) Packaging It Up....................................................................................................................................66(cid:1) vii ■ CONTENTS Summary.......................................................................................................................67(cid:1) ■Chapter 4: Object-Relational Mapping................................................................69(cid:1) Persistence Annotations...............................................................................................69(cid:1) Accessing Entity State..................................................................................................70(cid:1) Field Access.........................................................................................................................................70(cid:1) Property Access...................................................................................................................................71(cid:1) Mixed Access.......................................................................................................................................71(cid:1) Mapping to a Table.......................................................................................................73(cid:1) Mapping Simple Types..................................................................................................74(cid:1) Column Mappings................................................................................................................................75(cid:1) Lazy Fetching.......................................................................................................................................76(cid:1) Large Objects.......................................................................................................................................77(cid:1) Enumerated Types...............................................................................................................................78(cid:1) Temporal Types....................................................................................................................................80(cid:1) Transient State.....................................................................................................................................80(cid:1) Mapping the Primary Key..............................................................................................81(cid:1) Overriding the Primary Key Column.....................................................................................................81(cid:1) Primary Key Types...............................................................................................................................81(cid:1) Identifier Generation............................................................................................................................82(cid:1) Automatic Id Generation..................................................................................................................82(cid:1) Id Generation Using a Table............................................................................................................83(cid:1) Id Generation Using a Database Sequence.....................................................................................85(cid:1) Id Generation Using Database Identity............................................................................................86(cid:1) Relationships................................................................................................................87(cid:1) Relationship Concepts.........................................................................................................................87(cid:1) Roles...............................................................................................................................................87(cid:1) Directionality...................................................................................................................................87(cid:1) Cardinality.......................................................................................................................................88(cid:1) Ordinality.........................................................................................................................................89(cid:1) Mappings Overview..............................................................................................................................90(cid:1) Single-Valued Associations..................................................................................................................90(cid:1) Many-to-One Mappings..................................................................................................................90(cid:1) Using Join Columns........................................................................................................................91(cid:1) One-to-One Mappings.....................................................................................................................93(cid:1) Bidirectional One-to-One Mappings................................................................................................94(cid:1) Collection-Valued Associations............................................................................................................95(cid:1) viii
Description: