Enterprise Java Development@TOPIC@

Java Persistence API: Entities

Mapping POJOs to RDBMS Tables

Revision: v2013-09-23

Built on: 2014-03-07 00:01 EST

Abstract

This presentation provides information for mapping Java classes (without relationships) to the database using JPA.


Purpose
1. Goals
2. Objectives
1. Identifying Entity Classes
1.1. Entities
1.2. Entity Name
1.3. Field/Property Access
1.3.1. Access Types
1.3.2. Defining Access using @Annotations
1.3.3. Defining Access using orm.xml
1.4. Approaches
1.4.1. Java First
1.4.2. Schema First
2. Mapping Entity Classes
2.1. Core Mappings
2.2. More Detailed Property Mappings
2.3. Entity Mapping Example
2.3.1. Example Schema
2.3.2. Mapping Entity Class with Annotations
2.3.3. Mapping Entity Class with orm.xml
2.4. More on Precision/Scale
2.5. Transient Properties
2.6. Lob Properties
2.7. Temporal and Enumerated Properties
2.7.1. Temporals
2.7.2. Enums
2.7.3. Temporal/Enum Example
3. Primary Keys
3.1. Generated Simple Primary Keys
3.1.1. GenerationType.AUTO
3.1.2. GenerationType.IDENTITY
3.1.3. GenerationType.SEQUENCE
3.1.4. GenerationType.TABLE
4. Composite Primary Keys
4.1. Example Composite Primary Key Class
4.2. Composite @IdClass
4.3. Composite @EmbeddedId
4.4. Composite Overrides
4.5. Composite Primary Key Summary
5. Multi-table Mapping
5.1. Multi-table Example
5.2. Summary
6. Embedded Object
6.1. Embedded Object Example
6.2. Summary













Notice impact of temporal DB-mapping does effect until data saved and retrieved from database

  • Every entity must have a primary key

  • Primary keys must be unique

  • Map to one ("simple") or more ("composite") properties

  • Properties must be of type

    • Java primitive types -- including object wrappers

    • java.lang.String

    • Custom classes made up of legal property types








IDENTITY Requires Provider to Get All PK Values from Database

The provider must obtain the next primary key value from the database each time. Notice in the first case above -- the provider has already flushed the INSERT to the database during the persist and before out manual call to flush()





SEQUENCE Strategy Allows Clients to Self-Generate Groups of PK Values

The provider generates allocationSize primary key values before performing a flush or follow-on poll of the sequence. An allocationSize greater than one (1) is much less communication with the database than the IDENTITY strategy -- which would be somewhat analogous to a SEQUENCE allocationSize=1.





TABLE Strategy Allows Clients to Self-Generate Groups of PK Values

As will SEQUENCE, the TABLE strategy allows each client to generate an allocationSize amount of primary key values before requiring a flush of the current batch or polling for a new table value.

  • Primary key consisting of multiple properties

  • Represented using a primary key class

    • Must implement Serializable

      public class MowerPK implements Serializable {
    • Must have a public no-arg constructor

      public MowerPK() {}
    • Must implement hashCode() and equals() methods

      @Override
      
      public int hashCode() { ... }
      @Override
      public boolean equals(Object obj) { ... }
    • Must define properties that match with the definition of the entity, including access

      private String make;
      private String model;
  • Accessed using same strategy as entity using it













  • Map a single Java class to multiple tables in database

  • Uses a join of two or more tables and a single Java class

    1. One table is designated as primary

    2. Secondary and primary must have a column to join





  • Similar to @EmbeddedId except embedded object is not a primary key

  • Embedded object has no primary key -- housed in parent entity table

  • Embedded object represents some abstraction

  • Parent entity represents an abstraction with a primary key and the embedded object

    • The primary key could be the only thing the parent entity is providing