BCD Session 05

  • View
    218

  • Download
    0

Embed Size (px)

Text of BCD Session 05

  • 8/10/2019 BCD Session 05

    1/52

    Slide 1 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    In this session, you will learn to:

    Define Java persistence

    Describe Java Persistence API (JPA)

    Define entity classes

    Define key concepts of Java Persistence APIManage entity instance life-cycle states

    Describe entity instance management

    Deploy entity classes

    Objectives

  • 8/10/2019 BCD Session 05

    2/52

    Slide 2 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    The Java persistence specification is the specification of the

    Java API for the management of:

    Persistence.

    Object/relational mapping with Java EE and Java SE.

    All Java EE application servers provide an implementationof the Java Persistence API.

    Examine the Java persistence model in the following two

    stages:

    The first stage presents a static view of persistence.

    The second stage presents the dynamic view of persistence.

    Examining Java Persistence

  • 8/10/2019 BCD Session 05

    3/52

    Slide 3 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    The tasks to be performed by an application component

    developer to use the Java Persistence API are:

    Define entity classes

    Package and deploy the entity classes

    Provide application code to create and manage entityinstances

    Examining Java Persistence (Contd.)

  • 8/10/2019 BCD Session 05

    4/52

    Slide 4 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    The following figures show three tables in the database tier.

    Object Tier / Data Tier Static and Dynamic Mapping Example

    Auct ion ID Sel ler I tem StartAm oun t Increment

    Auction Table

    BidID Auct ion Bidder Am oun t BidTime Autho r izat ion

    Bid Table

    I temID Descrip t ion Image

    Item Table

  • 8/10/2019 BCD Session 05

    5/52

    Slide 5 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    The following figures show three entity classes in the object

    tier.

    Object Tier / Data Tier Static and Dynamic Mapping Example (Contd.)

  • 8/10/2019 BCD Session 05

    6/52

    Slide 6 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    The following figure shows the dynamic relationship

    consisting of data synchronization between the object and

    data tiers.

    Object Tier / Data Tier Static and Dynamic Mapping Example (Contd.)

  • 8/10/2019 BCD Session 05

    7/52Slide 7 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    The following code shows the implementation of an entity

    class:1 @Entity

    2 public class Item {

    3

    4 @Id

    5 @GeneratedValue

    6 private Integer itemID;

    7 private String description;

    8 private String image;

    910

    11 /** Creates a new instance of Item */

    12 public Item() {

    13 }

    Object Tier / Data Tier Static and Dynamic Mapping Example (Contd.)

  • 8/10/2019 BCD Session 05

    8/52Slide 8 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    14

    15 public Item(String description, String image){

    16 setDescription(description);

    17 setImage(image);

    18 }

    19

    20 public Integer getItemID() {

    21 return itemID;

    22 }

    23

    24 public String getDescription() {

    25 return description;

    26 }

    Object Tier / Data Tier Static and Dynamic Mapping Example (Contd.)

  • 8/10/2019 BCD Session 05

    9/52Slide 9 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    27

    28 public void setDescription(String description)

    {

    29 this.description = description;

    30 }

    3132 public String getImage(){

    33 return image;

    34 }

    35

    36 public void setImage(String image) {

    37 this.image = image;

    38 }

    39 }

    Object Tier / Data Tier Static and Dynamic Mapping Example (Contd.)

  • 8/10/2019 BCD Session 05

    10/52Slide 10 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    Java Persistence API is the specification that deals with the

    way relational data are mapped to java objects and how

    these objects are stored in the relational databases.

    Java persistence API can be commonly used within Java SE

    and Java EE environments.

    The Java Persistence specification defines the static and

    dynamic relationships of the persistence model by defining

    the entity component and entity manager object.

    Introducing Java Persistence API

  • 8/10/2019 BCD Session 05

    11/52Slide 11 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    Introducing Java Persistence API (Contd.)

    The Java Persistence specifications:

    Provide a standard Object-Relational (OR) mapping.

    Are not tied to the Java EE container.

    Can be tested and used in the J2SE environment.

    Enable the use of different entity providers without affecting theentity code.

  • 8/10/2019 BCD Session 05

    12/52Slide 12 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    Persistent Fields

    If the entity class uses persistent fields then persistence

    provider accesses an objects state by reading its variables

    directly.

    Adhere to the following rules, while defining a persistent

    field:

    Persistent fields cannot be public.

    Persistent field should not be directly read by the client

    All fields are persisted regardless of whether they have

    been annotated with @Column.

    Fields annotated with @Transient or modified with transientkeyword are not persistent.

  • 8/10/2019 BCD Session 05

    13/52Slide 13 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    Persistent Properties

    If an entity class uses persistent properties, then

    persistence provider retrieves an objects state by calling its

    accessor methods.

    For every persistent property of the entity, there is a getter

    method getProperty and a setter method setProperty.

    If the defined property is of type Boolean then isProperty

    method is used instead of getProperty.

    While defining a persistent, the methods must be public or

    protected and must follow the JavaBeans naming

    convention.The persistence annotations can be applied only on getter

    methods.

  • 8/10/2019 BCD Session 05

    14/52Slide 14 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    Persistent Properties (Contd.)

    The method signature for single-valued persistent properties

    is:Type getProperty()

    void setProperty(Type type)

  • 8/10/2019 BCD Session 05

    15/52Slide 15 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    Persistent Properties (Contd.)

    The following code snippet displays the use of persistent

    properties:private int id;

    private String message;

    @Id @Column(name = "ID")

    public int getId()

    {

    return id;

    }

    public void setId(int id)

    {this.id = id;

    }

  • 8/10/2019 BCD Session 05

    16/52Slide 16 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    Persistent Properties (Contd.)

    @Column(name = "MSG")

    public String getMessage()

    { return message; }

    public void setMessage(String message)

    {

    this.message = message;}

  • 8/10/2019 BCD Session 05

    17/52Slide 17 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    Persistent Data Types

    The data types that can be mapped, while using persistent

    field or persistent properties are:

    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.TimeStamp

  • 8/10/2019 BCD Session 05

    18/52Slide 18 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    To define an entity class, you are required to:

    Declare the entity class.

    Verify and override the default mapping.

    Defining Entity Classes: Essential Tasks

  • 8/10/2019 BCD Session 05

    19/52Slide 19 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    The following steps shows a process to declare the entity

    class:

    1. Collect information required to declare the entity class.

    2. Declare a public Java technology class.

    3. If an entity instance is to be passed by value as a detached

    object through a remote interface, then ensure the entity class

    implements the Serializable interface.

    4. Ensure the class does not define the finalize method.

    5. Annotate the class with the Entity annotation.

    6. Declare the attributes of the entity class.

    7. You can optionally declare a set of public getter and setter

    methods for every attribute declared.

    Declare the Entity Class

  • 8/10/2019 BCD Session 05

    20/52Slide 20 of 52Ver. 1.0

    Business Component Development Using EJB Technologies

    8. Annotate the primary key field or the getter method.

    9. Declare a public or protected no-arg constructor that takes no

    parameters.

    Declare the Entity Class (Contd.)

  • 8/10/2019 BCD Session 05

    21/52Slide 21 of 52Ver. 1.0

    Business Component Developme