Pro JPA 2 Mastering the Java™ Persistence API

Total Page:16

File Type:pdf, Size:1020Kb

Pro JPA 2 Mastering the Java™ Persistence API 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
Recommended publications
  • Object-Relational Mapping Tools and Hibernate
    Universite´ libre de Bruxelles Advanced Databases Winter Semester 2017-2018 Object-relational mapping tools and Hibernate Authors: Bruno Baldez Correa Supervisor: Matricule ULB: 000456738 Prof. Esteban Zimanyi Yue Wang Matricule ULB: 00000000 December 20, 2017 Contents 1 Object-relational database2 1.1 Necessity of object relational database...............2 1.2 Evaluation of object relational database..............3 1.2.1 Extensibility.........................3 1.2.2 Reusability..........................3 1.2.3 Productivity.........................3 1.2.4 Performance.........................4 1.3 Comparison with object database and relational database....4 2 Object-relational mapping5 2.1 ORM Paradigm Mismatch Problems................5 2.2 Entity Mapping............................6 2.3 Java Persistence API (JPA)....................7 2.3.1 JPA advantages........................8 3 Object-relational mapping tools 10 3.1 Open JPA............................... 10 3.1.1 Data remote transmission / offline processing....... 10 3.1.2 Database / object view unified tool............. 10 3.1.3 Use cache to improve efficiency............... 11 3.2 EclipseLink.............................. 11 3.3 Hibernate............................... 12 3.3.1 Hibernate Mappings..................... 13 4 Use Case 15 4.1 Scenario Diagrams.......................... 15 4.2 Implementation............................ 17 5 Conclusion 21 1 1 Object-relational database The idea of object-relational database was raised in early 1990s. A research team add object-oriented concepts to relational database. Early commercial products appeared in mid-1990s. IBM, Oracle and some other companies developed early products like Illustra and UniSQL and so on. [8] The object-relational database (ORD) is a combination of object database and relational database (RDB). It inherits advantages from both database mod- els like a middle man.
    [Show full text]
  • Kodo™ 4.1.4 Developers Guide for JPA/JDO Copyright © 2001 - 2006 BEA Systems Inc
    Kodo™ 4.1.4 Developers Guide for JPA/JDO Copyright © 2001 - 2006 BEA Systems Inc. Kodo™ 4.1.4 Developers Guide for JPA/JDO Copyright © 2001 - 2006 BEA Systems Inc. Table of Contents 1. Introduction ............................................................................................................................................. 1 1. Kodo JPA/JDO ................................................................................................................................. 3 1.1. OpenJPA .............................................................................................................................. 3 1.2. About This Document ............................................................................................................. 3 1.2.1. Sales Inquiries ............................................................................................................. 3 2. Kodo Installation .............................................................................................................................. 4 2.1. Overview .............................................................................................................................. 4 2.2. Updates ................................................................................................................................ 4 2.3. Key Files in the Download ....................................................................................................... 4 2.4. Quick Start ...........................................................................................................................
    [Show full text]
  • An In-Depth Guide to the Spring Framework and Its Tools — Fifth Edition — Iuliana Cosmina Rob Harrop Chris Schaefer Clarence Ho Pro Spring 5
    Pro Spring 5 An In-Depth Guide to the Spring Framework and Its Tools — Fifth Edition — Iuliana Cosmina Rob Harrop Chris Schaefer Clarence Ho Pro Spring 5 An In-Depth Guide to the Spring Framework and Its Tools Fifth Edition Iuliana Cosmina Rob Harrop Chris Schaefer Clarence Ho Pro Spring 5: An In-Depth Guide to the Spring Framework and Its Tools Iuliana Cosmina Rob Harrop Sibiu, Sibiu, Romania Reddish, United Kingdom Chris Schaefer Clarence Ho Venice, Florida, USA Hong Kong, China ISBN-13 (pbk): 978-1-4842-2807-4 ISBN-13 (electronic): 978-1-4842-2808-1 DOI 10.1007/978-1-4842-2808-1 Library of Congress Control Number: 2017955423 Copyright © 2017 by Iuliana Cosmina, Rob Harrop, Chris Schaefer, and Clarence Ho This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
    [Show full text]
  • White Paper 20110305
    Publisher: Fraunhofer Institute for Open Communication Systems FOKUS Kaiserin-Augusta-Allee 31 10589 Berlin, Germany Phone +49 30 3463 7000 [email protected] www.fokus.fraunhofer.de Versant Corporation 255 Shoreline Drive, Suite 450, Redwood City, CA 94065 Phone +1 650-232-2400 [email protected] www.versant.com Design and Layout: Fuel Design Kirsten Felbert Picturesource: iStock Contact: Fraunhofer Institute for Open Communication Systems FOKUS Competence Center Future Applications and Media Dr. Stefan Arbanowski Phone +49 30 3463 7197 [email protected] Versant Corporation Dirk Bartels Phone +1 650-232-2432 [email protected] Benchmarking the Versant Object Database and MySQL with the FOKUS Open IPTV Ecosystem White Paper Abstract Databases for managing and controlling IPTV content are indispensable for an IPTV platform. In this application domain, the database must provide fast access to rather complex data, easy integration into an ever evolving application code, and support massive concurrent access from potentially millions of consumers. Choosing a database for such a system and the reasons this choice serve as the subject of this paper. We have implemented Fraunhofer FOKUS Open IPTV Ecosystem with two database systems: The Versant Object Database supports a native object-oriented data model which allows managing application objects directly in the database. Oracle’s MySQL, by contrast, a popular open source relational database, requires an object-to-relational mapping layer to manage the application objects in the relational data model. Analyzing the results of a number of IPTV related benchmark use cases should help the reader to evaluate the pros and cons of each of these two database systems in such an environment.
    [Show full text]
  • Mastering the Java™ Persistence API
    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.
    [Show full text]
  • Pro JPA 2 Mastering the Java Persistence
    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.
    [Show full text]
  • Java Object Persistence: State of the Union
    March 4, 2008 Java Object Persistence: State of the Union In this virtual panel, the editors of InfoQ.com (Floyd Marinescu) and ODBMS.org (Roberto V. Zicari) asked a group of leading persistence solution architects their views on the current state of the union in persistence in the Java community. Panelists: - Mike Keith: EJB co-spec lead, main architect of Oracle Toplink ORM -Ted Neward: Independent consultant, often blogging on ORM and persistence topics - Carl Rosenberger: lead architect of db4objects, open source embeddable object database - Craig Russell: Formerly the spec lead of Java Data Objects (JDO) JSR, architect of entity bean engine in Sun’s appservers prior to Glassfish Q1: Do we still have an "impedance mismatch problem"? Mike Keith: As long as an object model is being used to read from and write to a relational data model then the impedance mismatch will exist. It becomes a problem when there are no strategies, technologies or tools to help overcome the mismatch, or when the mismatch results in a convoluted design or prohibitively bad performance. There will always be some scenarios that lead to this situation simply because of the nature of the mismatch. Technologies and tools can help a great deal, and do meet the needs of the vast majority of applications, but they can’t turn water into wine. Ted Neward: Absolutely. So long as we have different 'shapes' to data in their transient and durable forms—such as the current dilemma where we try to take transient data in the shape of objects and store it to a relational durable data store—we will have an impedance mismatch problem.
    [Show full text]
  • An Adaptive, Searchable and Extendable Context Model
    Mid Sweden University The Department of Information Technology and Media (ITM) Author: Felix Dobslaw E­mail address: [[email protected]] Study programme: Distributed Systems, 30 ECTS Examiner: Theo Kanter, [email protected] Tutors: Theo Kanter, [email protected] , Åke Malmberg, [email protected] Scope: 22244 words inclusive of appendices Date: 2009­04­30 Mid Sweden University M.Sc. Thesis within Computer Science A, 30 points An Adaptive, Searchable and Extendable Context Model, enabling cross­domain Context Storage, Retrieval and Reasoning Architecture, Design, Implementation and Discussion Felix Dobslaw Felix Dobslaw Acknowledgement An Addaptive, Searchable and Extendable Context Model April 3rd 2009 Acknowledgement I would like to thank my first academic advisor Theo Kanter for inspiring discussions and my second academic advisor Ake˚ Malmberg for all the ef- forts, arranging a very pleasant room for me in Ostersund¨ and integrating me into the local staff. Special thanks to Annika Berggren and Patrik Oster-¨ berg, who helped me a lot with the administrative work. Thanks also to Aron Larsson for the very diverse discussions and brainstorming sessions. Further more I would like to thank the ITM department at the MidSwe- den University for covering my travel expenses. All your help is well appreciated. ii Felix Dobslaw Table of Contents An Addaptive, Searchable and Extendable Context Model April 3rd 2009 Table of Contents 1 Introduction 1 1.1 Importance of Context . .2 1.1.1 Theory and Practice . .3 1.2 Context of this Thesis . .4 1.3 Problem Statement . .5 1.4 Related Work . .6 1.5 Context on the Internet .
    [Show full text]
  • Kodo™ 4.2.0.Load03 Developers Guide for JPA/JDO
    Kodo™ 4.2.0.load03 Developers Guide for JPA/JDO Copyright © 1996, 2008 Oracle and/or its affiliates. All rights reserved. Kodo™ 4.2.0.load03 Developers Guide for JPA/JDO Copyright © 1996, 2008 Oracle and/or its affiliates. All rights reserved. Table of Contents 1. Introduction ............................................................................................................................................. 1 1. Kodo JPA/JDO ................................................................................................................................. 3 1.1. OpenJPA .............................................................................................................................. 3 1.2. About This Document ............................................................................................................. 3 2. Java Persistence API ................................................................................................................................. 4 1. Introduction ..................................................................................................................................... 9 1.1. Intended Audience .................................................................................................................. 9 1.2. Lightweight Persistence ........................................................................................................... 9 2. Why JPA? ......................................................................................................................................10
    [Show full text]
  • Sistemi Informativi
    Object-relational mapping Source http://www.datanucleus.org/ Object-relational impedance mismatch • The object-relational impedance mismatch is a set of conceptual and technical difficulties that are often encountered when a RDBMS is being used by a program written in an object-oriented programming language or style. 2 Data type differences • A major mismatch between existing relational and OO languages is the type system differences. • The relational model strictly prohibits by-reference attributes (or pointers), whereas OO languages embrace and expect by-reference behavior. • Scalar types and their operator semantics are also very often subtly to vastly different between the models, causing problems in mapping. • For example, most SQL systems support string types with varying collations and constrained maximum lengths, while most OO languages consider collation only as an argument to sort routines and strings are intrinsically sized to available memory 3 Structural and integrity differences • In OO languages, objects can be composed of other objects—often to a high degree—or specialize from a more general definition. This may make the mapping to relational schemas less straightforward. • This is because relational data tends to be represented in a named set of global, unnested relation variables. 4 Manipulative differences • The relational model has an intrinsic, relatively small and well defined set of primitive operators for usage in the query and manipulation of data, whereas OO languages generally handle query and manipulation through custom-built or lower-level physical access path specific imperative operations 5 Transactional differences • Relational database transactions, as the smallest unit of work performed by databases, are much larger than any operations performed by classes in OO languages.
    [Show full text]
  • Performance Evaluation of Java Object-Relational Mapping Tools
    PERFORMANCE EVALUATION OF JAVA OBJECT-RELATIONAL MAPPING TOOLS by HASEEB YOUSAF (Under the Direction of John A. Miller) ABSTRACT In the modern era of enterprise Web technology, there is strong competition growing between different vendors for highly scalable and reliable Web application frameworks. In designing these Web applications, the companies need some storage mechanism through which they manipulate the data efficiently. The traditional database management systems provide this functionality but require a complete knowledge of the SQL. Nowadays, there are many Object- Relational Mapping (ORM) persistent stores available, which make the application developer job easier by providing the same functionality as that of a traditional database. The Java language also provides ORM tools such as Hibernate, EclipseLink that provide the same functionality. Similarly, we have created a lightweight persistent tool named GlycoVault, which provides similar functionality. This study is focused on comparing different Java ORM tools including GlycoVault. The study provides information about the tools’ functionalities and also compares the results of several queries we have designed to test their performance. INDEX WORDS: Persistent, ORM, Hibernate, GlycoVault, Ebean, EclipseLink, OpenJPA, database, SQL, cache, performance. PERFORMANCE EVALUATION OF JAVA OBJECT-RELATIONAL MAPPING TOOLS by HASEEB YOUSAF BS, Iqra University, Pakistan, 2005 MBA, Iqra University, Pakistan, 2007 A Thesis Submitted to the Graduate Faculty of The University of Georgia in Partial Fulfillment of the Requirements for the Degree MASTER OF SCIENCE ATHENS, GEORGIA 2012 © 2012 Haseeb Yousaf All Rights Reserved PERFORMANCE EVALUATION OF JAVA OBJECT-RELATIONAL MAPPING TOOLS by HASEEB YOUSAF Major Professor: John A. Miller Committee: Krzysztof J. Kochut William S.
    [Show full text]
  • Java Persistence What's New What's (Still) the Same
    Java Persistence What's New What's (Still) The Same Craig L. Russell JAX India 2009 Agenda Objects vs. Relational Alternative Approaches Object-Relational Mapping (ORM) Some Cool ORM Features Java Persistence API (JPA) Java Data Objects (JDO) Where Do We Go From Here? Q & A 2 ©Sun Microsystems, Inc. 2 Licensed under the Apache License, Version 2.0 Presenter's "Baggage" JavaTM Data Objects Specification Lead Apache OpenJPA PMC Chair Java EE Container Managed Persistence Architect 3 ©Sun Microsystems, Inc. 3 Licensed under the Apache License, Version 2.0 Objects vs. Relational ● Classes ● Tables ● Fields ● Columns ● Methods ● Stored Procedures ● References ● Foreign Keys ● Multi-valued types ● Single-valued types Department ID NAME SALARY 128 Harry 12900.09 Employee Project 245 Joe 23929.88 4 ©Sun Microsystems, Inc. 4 Licensed under the Apache License, Version 2.0 Alternative Approaches ● Direct API ● Write SQL, Embed in Statement, Get ResultSet ● SQL Helper Classes ● Write SQL, Put Results into User's Classes ● Data Access Object ● Write SQL, Translate Results to Domain Objects ● Object-Relational Mapping ● Write Mapping, Write Queries in Domain Model ● Automatic Change Detection 5 ©Sun Microsystems, Inc. 5 Licensed under the Apache License, Version 2.0 Object-Relational Mapping ● Class <--> Table ● Property <--> Column ● Identity <--> Primary Key ● Bidirectional-Relationship <--> Foreign Key ● Inheritance <--> Foreign Key == Primary Key ● HashMap <--> Join Table with 3 columns ● Embedded <--> Multiple Columns ● Class <--> Multiple Tables with same Primary Key 6 ©Sun Microsystems, Inc. 6 Licensed under the Apache License, Version 2.0 Some Cool ORM Features ● Auto-increment fields/properties ● Especially useful for primary keys ● Orphan deletion ● Implements containment (composite) pattern ● Generate source from database schema ● When you already have a database ● Surrogate (artificial) primary keys ● Enforces "best practice" automatically 7 ©Sun Microsystems, Inc.
    [Show full text]