Pro JPA 2 Mastering the Java Persistence
Total Page:16
File Type:pdf, Size:1020Kb
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 Persistence API Pro compliant JPA 2 JPA Dear Reader, 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 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! Pro JPA 2.0 includes a host of new features, such as additional object-relational 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 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, JPA 2 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- Mastering the Java Persistence API ers, and hope that you will find this edition equally valuable. Mike Keith, JPA 2.0 Expert Group Member, and Merrick Schincariol Companion eBook Create robust, data-driven applications with this definitive guide to the new JPA 2 See last page for details Java™ EE 6 on $10 eBook version TITLES RELATED compliant Schincariol SOURCE CODE ONLINE www.apress.com ISBN 978-1-4302-1956-9 Keith Mike Keith and Merrick Schincariol 5 49 9 9 Foreword by Linda DeMichiel, JPA Specification Lead 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 Persistence API Pro compliant JPA 2 JPA Dear Reader, 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 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! Pro JPA 2.0 includes a host of new features, such as additional object-relational 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 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, JPA 2 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- Mastering the Java Persistence API ers, and hope that you will find this edition equally valuable. Mike Keith, JPA 2.0 Expert Group Member, and Merrick Schincariol Companion eBook Create robust, data-driven applications with this definitive guide to the new JPA 2 See last page for details Java™ EE 6 on $10 eBook version TITLES RELATED compliant Schincariol SOURCE CODE ONLINE www.apress.com ISBN 978-1-4302-1956-9 Keith Mike Keith and Merrick Schincariol 5 49 9 9 Foreword by Linda DeMichiel, JPA Specification Lead 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 ■Contents ................................................................................................................v ■Foreword............................................................................................................. xx ■About the Author................................................................................................ xxi ■About the Technical Reviewer........................................................................... xxii ■Acknowledgments............................................................................................ xxiii ■Preface............................................................................................................... xiv ■Chapter 1: Introduction .........................................................................................1 ■Chapter 2: Getting Started...................................................................................17 ■Chapter 3: Enterprise Applications .....................................................................33 ■Chapter 4: Object-Relational Mapping ................................................................69 ■Chapter 5: Collection Mapping..........................................................................107 ■Chapter 6: Entity Manager ................................................................................131 ■Chapter 7: Using Queries...................................................................................179 ■Chapter 8: Query Language...............................................................................207