Pro JPA 2 Mastering the Java™ Persistence API
Total Page:16
File Type:pdf, Size:1020Kb
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 ■Chapter 9: Criteria API ......................................................................................239 ■Chapter 10: Advanced Object-Relational Mapping............................................273 ■Chapter 11: Advanced Topics............................................................................315 ■Chapter 12: XML Mapping Files ........................................................................371 ■Chapter 13: Packaging and Deployment ...........................................................407 ■Chapter 14: Testing ...........................................................................................429 ■Chapter 15: Migration .......................................................................................457 ■Index .................................................................................................................481 iv Contents ■Contents at a Glance ............................................................................................iv ■Contents ................................................................................................................v ■Foreword............................................................................................................. xx ■About the Author ................................................................................................ xxi ■About the Technical Reviewer........................................................................... xxii ■Acknowledgments............................................................................................ xxiii ■Preface ............................................................................................................. xxiv ■Chapter 1: Introduction .........................................................................................1 Object-Relational Mapping ............................................................................................. 2 The Impedance Mismatch......................................................................................................................3 Class Representation ........................................................................................................................3 Relationships ....................................................................................................................................5 Inheritance........................................................................................................................................7 Java Support for Persistence.......................................................................................... 9 Proprietary Solutions..............................................................................................................................9 JDBC ......................................................................................................................................................9 Enterprise JavaBeans ..........................................................................................................................10 Java Data Objects ................................................................................................................................10 Why Another Standard? ................................................................................................ 11 The Java Persistence API.............................................................................................. 12 History of the Specification..................................................................................................................12 EJB 3.0 and JPA 1.0........................................................................................................................12 JPA 2.0............................................................................................................................................13 JPA and You....................................................................................................................................13 Overview ..............................................................................................................................................13 POJO Persistence............................................................................................................................13 Nonintrusiveness ............................................................................................................................14 Object Queries.................................................................................................................................14 v ■ CONTENTS Mobile Entities ................................................................................................................................14 Simple Configuration.......................................................................................................................15