Module 8 The Java Persistence API ... Java Persistence API 8 Developing Applications for Java EE...

download Module 8 The Java Persistence API ... Java Persistence API 8 Developing Applications for Java EE Platform

of 16

  • date post

    14-Jul-2020
  • Category

    Documents

  • view

    6
  • download

    0

Embed Size (px)

Transcript of Module 8 The Java Persistence API ... Java Persistence API 8 Developing Applications for Java EE...

  • 1

    Module 8 The Java Persistence API

    Ja va

    P er

    si st

    en ce

    A P

    I8

    220 Ω Omega ΩDeveloping Applications for Java EE Platform

    ► Describe the role of the Java Persistence API (JPA) in a Java EE application

    ► Describe the basics of Object Relational Mapping

    ► Describe the elements and environment of an entity Component

    ► Describe the life cycle and operational characteristics of entity components

    Objectives

  • 2

    Ja va

    P er

    si st

    en ce

    A P

    I8

    221 Ω Omega ΩDeveloping Applications for Java EE Platform

    ► What is data persistence?

    ► What is the Java persistence specification?

    ► How does the Java Persistence API relate to Java EE application servers?

    ► What are the key features of the persistence model specified in the Java Persistence API?

    Examining Java Persistence Ja

    va P

    er si

    st en

    ce A

    P I8

    222 Ω Omega ΩDeveloping Applications for Java EE Platform

    Static Relationship Mapping - Data Tier

    Elements

  • 3

    Ja va

    P er

    si st

    en ce

    A P

    I8

    223 Ω Omega ΩDeveloping Applications for Java EE Platform

    Static Relationship Mapping - Object Tier

    Elements

    Ja va

    P er

    si st

    en ce

    A P

    I8

    224 Ω Omega ΩDeveloping Applications for Java EE Platform

    Dynamic Relationship - Object/Data Tier Data

    Synchronization

    With entities, the data synchronization is maintained by the persistence provider.

  • 4

    Ja va

    P er

    si st

    en ce

    A P

    I8

    225 Ω Omega ΩDeveloping Applications for Java EE Platform

    The Java Persistence API

    The Java Persistence API: ► Replaces EJB 2.1 Entity Beans with non-EJB entity classes ► Is a standard API for specifying Object-to-Relational mapping information ► Can be used with or without a Java EE Application Server ─ Container-Managed Persistence ─ Application-Managed Persistence

    Ja va

    P er

    si st

    en ce

    A P

    I8

    226 Ω Omega ΩDeveloping Applications for Java EE Platform

    Object Relational Mapping

    Object Relational Mapping (ORM) software: ► Provides an object-oriented view of the database ► Examples include Oracle’s Toplink and Hibernate

  • 5

    Ja va

    P er

    si st

    en ce

    A P

    I8

    227 Ω Omega ΩDeveloping Applications for Java EE Platform

    Normalized Data Mapping Ja

    va P

    er si

    st en

    ce A

    P I8

    228 Ω Omega ΩDeveloping Applications for Java EE Platform

    Use of an Entity Component Across a Set of

    Database Tables

  • 6

    Ja va

    P er

    si st

    en ce

    A P

    I8

    229 Ω Omega ΩDeveloping Applications for Java EE Platform

    Entity Class Requirements

    Entity classes are coded as standard Java classes with the following requirements: ► javax.persistence.Entity annotation on the class or declared as an Entity in a deployment descriptor ► Must be a public class ► No-argument constructor with public or protected access ► Open to extension, in other word they are not final classes ► Implement Serializable if they are to be returned by remote session beans ► Top-level class, entity classes cannot be inner classes

    Ja va

    P er

    si st

    en ce

    A P

    I8

    230 Ω Omega ΩDeveloping Applications for Java EE Platform

    Entity Class Example import java.io.Serializable; import javax.persistence.*;

    @Entity @Table( name = "TABLE1") public class MyEntity implements Serializable {

    @Id @Column( name = "ID") private int id; @Column( name = "MSG") private String message;

    protected MyEntity() { } public MyEntity(int id, String message) {

    this.id = id; this.message = message;

    }

    public int getId() { return id; } public String getMessage() { return message; } public void setMessage(String message) {

    this.message = message; } }

  • 7

    Ja va

    P er

    si st

    en ce

    A P

    I8

    231 Ω Omega ΩDeveloping Applications for Java EE Platform

    Persistent Fields as Opposed to Persistent

    Properties

    Entity classes have their state synchronized with a database. The state of an entity class is obtained from either its variables (fields) or its accessor methods (properties). Field- based or property-based access: ► Determined by the placement of annotations ► Cannot have both field-based and property-based access

    Ja va

    P er

    si st

    en ce

    A P

    I8

    232 Ω Omega ΩDeveloping Applications for Java EE Platform

    Persistent Fields

    When using persistent fields, the persistence provider retrieves an object’s state by reading its variables. ► Persistent fields cannot be public ► Should not be read by clients directly ► Unless annotated with @Transient or modified with the

    transient keyword, all variables are persisted regardless of whether they have a @Column annotation.

    @Id @Column(name = "ID") private int id; @Column(name = "MSG") private String message;

    public int getId() { return id; } public String getMessage() { return message; } public void setMessage(String message){ this.message=message; }

  • 8

    Ja va

    P er

    si st

    en ce

    A P

    I8

    233 Ω Omega ΩDeveloping Applications for Java EE Platform

    Persistent Properties

    When using persistent properties, the persistence provider retrieves an object’s state by calling its accessor methods. ► Methods must be public or protected ► Methods follow the JavaBeans naming convention ► Persistence annotations can only be on getter methods

    private int id; private String message;

    @Id @Column(name = "ID") public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name = "MSG") public String getMessage() { return message; } public void setMessage(String message) {this.message=message;}

    Ja va

    P er

    si st

    en ce

    A P

    I8

    234 Ω Omega ΩDeveloping Applications for Java EE Platform

    Persistence Data Types

    Persistence fields or properties can be of the following data types ► Java primitive types ► Java wrappers, such as java.lang.Integer ► java.lang.String ► byte[] and Byte[] ► char[] and Character[] ► Any serializable types including but not limited to: ─ java.util.Date ─ java.sql.Date ─ java.sql.TimeStamp

  • 9

    Ja va

    P er

    si st

    en ce

    A P

    I8

    235 Ω Omega ΩDeveloping Applications for Java EE Platform

    The Concept of a Primary Key

    ► An entity component distinguishes itself and the data it represents from other entities using a primary key. ► A primary key gives an entity instance its persistent identity. ► They are typically a string or an integer but can also be custom classes that correspond to several database table columns. ► Every entity class must have a primary key. ► Can be auto incrementing.

    @Id @GeneratedValue( strategy = GenerationType.IDENTITY) @Column( name = "ID", nullable = false) private int id;

    Ja va

    P er

    si st

    en ce

    A P

    I8

    236 Ω Omega ΩDeveloping Applications for Java EE Platform

    Entity Component Primary Key Association

  • 10

    Ja va

    P er

    si st

    en ce

    A P

    I8

    237 Ω Omega ΩDeveloping Applications for Java EE Platform

    Persistence Units

    A persistence unit is a collection of entity classes stored in a EJB-Jar, WAR, or JAR archive along with a persistence.xml file. A persistence unit: ► Defines what entity classes will be controlled by an entity manager ► Is limited to a single DataSource

    Ja va

    P er

    si st

    en ce

    A P

    I8

    238 Ω Omega ΩDeveloping Applications for Java EE Platform

    The persistence.xml file

    The persistence.xml file: ► Configures which classes make up a persistence unit ► Defines the base of a persistence unit ► Specifies the DataSource used

  • 11

    Ja va

    P er

    si st

    en ce

    A P

    I8

    239 Ω Omega ΩDeveloping Applications for Java EE Platform

    The Persistence Context

    A persistence context can be thought of as a working copy of a persistence unit. Several persistence contexts using the same persistence unit can be active at the same time. A persistence context: ► Typically lasts the duration of a transaction ► Limits entity instances to a single instance per persistent identity ► Has a management API, known as the entity manager

    Ja va

    P er

    si st

    en ce

    A P

    I8

    240 Ω Omega ΩDeveloping Applications for Java EE Platform

    The Entity Manager

    An Entity Manager provides methods to control events of a persistence context and the life cycle of entity instances in a persistence context. An Entity Manager: ► Provides operations, such as flush() , find() , and createQuery() , to control a persistence context ► Replaces som