JAVA PERSISTENCE API (JPA) Tutorial JAVA PERSISTENCE API (JPA) S imply Easy Learning 2 JAVA PERSISTENCE API (JPA) About the Tutorial This tutorial provides a basic understanding of how to store a copy of database objects into temporary memory using JAVA Persistence API (JPA). Audience This tutorial is designed for readers intend to do Java programing with Database connectivity, using Persistence API. Prerequisites Awareness of Java programming with JDK 1.6 or later is a prerequisite to understand this tutorial. In addition, we assume the readers are acquainted with the concepts of JDBC in Java. Copyright & Disclaimer Copyright 2014 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected] . 3 JAVA PERSISTENCE API (JPA) Table of Contents About the Tutorial .................................................................................................................................... 3 Audience ................................................................................................................................................... 3 Prerequisites ............................................................................................................................................. 3 Copyright & Disclaimer ............................................................................................................................. 3 Table of Contents ...................................................................................................................................... 4 1. JPA – INTRODUCTION ........................................................................................................... 7 Mismatch between Relational and Object Models .................................................................................... 7 What is JPA? ............................................................................................................................................. 7 Where to use JPA? .................................................................................................................................... 7 JPA History ................................................................................................................................................ 8 JPA Providers ............................................................................................................................................ 8 2. JPA – ARCHITECTURE ............................................................................................................ 9 Class Level Architecture ............................................................................................................................ 9 JPA Class Relationships ........................................................................................................................... 10 3. JPA – ORM COMPONENTS .................................................................................................. 12 Object Relational Mapping ..................................................................................................................... 12 Advanced Features ................................................................................................................................. 12 ORM Architecture ................................................................................................................................... 12 Mapping.xml ........................................................................................................................................... 14 Annotations ............................................................................................................................................ 17 Java Bean Standard ................................................................................................................................. 19 Bean Conventions ................................................................................................................................... 19 4 JAVA PERSISTENCE API (JPA) 4. JPA – INSTALLATION ............................................................................................................ 21 Step 1 : Verify your Java Installation ....................................................................................................... 21 Step 2 : Set your Java Environment ......................................................................................................... 22 Step 3 : Installing JPA .............................................................................................................................. 22 Adding MySQL connector to the Project ................................................................................................. 27 5. JPA – ENTITY MANAGERS .................................................................................................... 29 Creating Entities ...................................................................................................................................... 30 Persistence Operations ........................................................................................................................... 34 Create Employee ..................................................................................................................................... 34 Update Employee ................................................................................................................................... 35 Deleting Employee .................................................................................................................................. 38 6. JPA – JPQL ........................................................................................................................... 40 Java Persistence Query language ............................................................................................................ 40 Query Structure ...................................................................................................................................... 40 Scalar and Aggregate Functions .............................................................................................................. 41 Between, And, Like Keywords ................................................................................................................. 43 Ordering ................................................................................................................................................. 45 Named Queries ....................................................................................................................................... 46 Eager and Lazy Fetching .......................................................................................................................... 50 7. JPA – ADVANCED MAPPINGS .............................................................................................. 51 Inheritance Strategies ............................................................................................................................. 51 Single Table strategy ............................................................................................................................... 51 Joined Table Strategy .............................................................................................................................. 58 5 JAVA PERSISTENCE API (JPA) Table per Class Strategy .......................................................................................................................... 66 8. JPA – ENTITY RELATIONSHIPS .............................................................................................. 74 @ManyToOne Relation ........................................................................................................................... 74 @OneToMany Relation ........................................................................................................................... 81 @OneToOne Relation ............................................................................................................................. 89 @ManyToMany Relation ........................................................................................................................ 96 9. JPA – CRITERIA API ............................................................................................................ 106 History of Criteria.................................................................................................................................. 106 Criteria Query Structure ........................................................................................................................ 106 Example of Criteria API ......................................................................................................................... 107 6 1. JPA – INT RODUCJAVTAI POERNSIS TENCE API (JPA) Any enterprise application performs database operations by storing and retrieving vast amounts of data. Despite all the available technologies for storage management, application developers normally struggle to perform database operations efficiently. Generally, Java developers use lots of code, or use the proprietary framework to interact with the database, whereas using JPA, the burden of interacting with the database reduces significantly. It forms a bridge between object models (Java program) and relational models (database program). Mismatch between Relational and Object Models Relational objects are represented in a tabular format, while object models are represented in an interconnected graph of object format. While storing and retrieving an object model from a relational database, some mismatch occurs due to the following reasons: Granularity: Object model has more granularity than relational model. Subtypes: Subtypes (means inheritance) are not supported by all types of relational databases. Identity: Like object model, relational model does not expose identity while writing equality. Associations: Relational models cannot determine multiple relationships while looking into an object domain model. Data navigation: Data navigation between objects in an object network is different in both models. What is JPA? Java Persistence API is a collection of classes and methods to persistently store the vast amounts of data into a database which is provided by the Oracle Corporation. Where to use JPA? To reduce the burden of writing codes for relational object management, a programmer follows the ‘JPA Provider’ framework, which allows easy interaction with database instance. Here the required framework is taken over by JPA. 7 JAVA PERSISTENCE API (JPA) JPA History Earlier versions of EJB, defined the persistence layer combined with the business logic layer using javax.ejb.EntityBean Interface. While introducing EJB 3.0, the persistence layer was separated and specified as JPA 1.0 (Java Persistence API). The specifications of this API were released along with the specifications of JAVA EE5 on May 11, 2006 using JSR 220. JPA 2.0 was released with the specifications of JAVA EE6 on December 10, 2009 as a part of Java Community Process JSR 317. JPA 2.1 was released with the specification of JAVA EE7 on April 22, 2013 using JSR 338. JPA Providers JPA is an open source API, therefore various enterprise vendors such as Oracle, Redhat, Eclipse, etc. provide new products by adding the JPA persistence flavor in them. Some of these products include: Hibernate Eclipselink Toplink Spring Data JPA 8 2. JPA – ARC HITECJATVAU PERRSEIS TENCE API (JPA) Java Persistence API is a source to store business entities as relational entities. It shows how to define a Plain Oriented Java Object (POJO) as an entity and how to manage entities with relations. Class Level Architecture The following image shows the class level architecture of JPA. It shows the core classes and the interfaces of JPA. The following table describes each of the units shown in the above architecture. Units Description This is a factory class of EntityManager. It creates and EntityManagerFactory manages multiple EntityManager instances. 9 JAVA PERSISTENCE API (JPA) It is an Interface. It manages the persistence operations on EntityManager objects. It works like a factory for Query instance. Entities are the persistence objects, stored as records in the Entity database. It has one-to-one relationship with the EntityManager. For EntityTransaction each EntityManager, operations are maintained by the EntityTransaction class. This class contains static methods to obtain the Persistence EntityManagerFactory instance. This interface is implemented by each JPA vendor to obtain Query the relational objects that meet the criteria. The above classes and interfaces are used for storing entities into a database as a record. They help programmers by reducing their efforts to write codes for storing data into a database so that they can concentrate on more important activities such as writing codes for mapping the classes with database tables. JPA Class Relationships In the above architecture, the relations between the classes and interfaces belong to the javax.persistence package. The following diagram shows the relationship between them. 10
Description: