JPA JAVA API Specification Entity persistence model for

  • Slides: 31
Download presentation

JPA § JAVA API Specification § Entity persistence model for EJB 3. 0 §

JPA § JAVA API Specification § Entity persistence model for EJB 3. 0 § POJO based framework for Java Persistence § Management of Relational Data in JAVA applications § javax. persistence package § JPA 1. 0 - 11 th May 2006 § JPA 2. 1 - 22 nd April 2013

Benefits of JPA § JDBC was a low level API, It is simple, but

Benefits of JPA § JDBC was a low level API, It is simple, but error prone § JPA leverages best ideas from ORM community § Developers can choose between different implementations § Vendor Independence

Goals of JPA § Provides complete ORM solution for J 2 SE and J

Goals of JPA § Provides complete ORM solution for J 2 SE and J 2 EE applications § Easy to use , no need to implement any framework interfaces or classes § Facilitate test driven development § Annotation Driven, No XML mapping needed

POJO Requirements § Annotated with @Entity § A lightweight persistence domain object § Contains

POJO Requirements § Annotated with @Entity § A lightweight persistence domain object § Contains a persistent @Id field § Should be a top level class § class , method or persistent field shouldn’t be final § Represents a table in a relational DB

JPA Annotations § Annotations are from the package javax. persistence § Annotation can be

JPA Annotations § Annotations are from the package javax. persistence § Annotation can be placed either on field or properties § Field level access is preferred to prevent executing logic § property level annotations are applied on getter methods § Can’t mix the style in inheritance hierarchy

Persistent Identifiers § § § Entities must define an id field/fields corresponding to the

Persistent Identifiers § § § Entities must define an id field/fields corresponding to the database primary key The Id can either be simple or composite value Strategies @Id Single valued type –Most common @Id. Class -Maps multiple fields to table PK @Embedded. Id -Map Pk class to table PK Composite primary classes must Implement serializable Override equals() and hash. Code()

@Generated. Value § Supports auto generated primary key value Strategies defined by Generation. Type

@Generated. Value § Supports auto generated primary key value Strategies defined by Generation. Type enum are • Generation. Type. AUTO • Generation. Type. IDENTITY • Generatio. Type. SEQUENCE • Generation. Type. TABLE

Commonly Used Annotations § @Table and @Column Used to define name mappings between java

Commonly Used Annotations § @Table and @Column Used to define name mappings between java objects and database table or column @Table applied at the persistent class level and @Column applied at the persistent field/property level

Commonly Used Annotations Contd § @Temporal Used with java. uil. Date or java. util.

Commonly Used Annotations Contd § @Temporal Used with java. uil. Date or java. util. Calendar to determine how value is persisted § Values defined are Temporal. Type. TIME (java. sql. Time) Temporal. Type. DATE (java. sql. Date) Temporal. Type. TIMESTAMP (java. sql. Timestamp)

Commonly Used Annotations Contd § @Enumerated Used to determine the strategy for persisting java

Commonly Used Annotations Contd § @Enumerated Used to determine the strategy for persisting java enum values to database § Values defined are Enum. Type. ORDINAL Enum. Type. STRING

Commonly Used Annotations Contd § @Transient By default JPA assumes all fields are persistent

Commonly Used Annotations Contd § @Transient By default JPA assumes all fields are persistent Non persistent fields should be marked as transient or annotated with @Transient § @Lob Used to persist values to BLOB /CLOB fields Often used with @Basic to lazy load value

JPA Architecture

JPA Architecture

Entity. Manager. Factory § It gives an application-managed entity manager § An Entity. Manager.

Entity. Manager. Factory § It gives an application-managed entity manager § An Entity. Manager. Factory can have several Entity Managers § Once an Entity. Manager. Factory is closed, all its entity managers are in the closed state.

Entity. Manager § Handles O/R Mapping of Entities to the database § Provides APIs

Entity. Manager § Handles O/R Mapping of Entities to the database § Provides APIs Ø inserting objects into database Ø fetching objects from database Ø synchronizing objects with database Ø querying database § Provides caching and coordinates transactional services

Persistence Unit § A set of all entity classes managed by Entity. Manager instances.

Persistence Unit § A set of all entity classes managed by Entity. Manager instances. § Defined in META-INF/persistence. xml. § Identified with a unique name.

Entity § A lightweight persistence domain object § Represents a table in a relational

Entity § A lightweight persistence domain object § Represents a table in a relational DB § Entity class annotated with javax. persistence. Entity § A top level class § Have a unique object identifier

Entity Life Cycle

Entity Life Cycle

JPA 2. 1 Vendors JPA

JPA 2. 1 Vendors JPA

ORM § Converts data between incompatible type systems in object -oriented programming languages. §

ORM § Converts data between incompatible type systems in object -oriented programming languages. § Creates a "virtual object DB“. § Make RDBMS looks like ODBMS.

Hibernate § A well known ORM tool. § Most popular JPA API implementation. §

Hibernate § A well known ORM tool. § Most popular JPA API implementation. § Maps the Relational Model in the database to the Object Model in Java.

Hibernate Architecture

Hibernate Architecture

JPA with Hibernate

JPA with Hibernate

CRUD § Persist() § Find() § Merge() § Remove()

CRUD § Persist() § Find() § Merge() § Remove()

Query § Native SQL § JPQL § Criteria Query § Named Query (XML &

Query § Native SQL § JPQL § Criteria Query § Named Query (XML & Annotation)

Query Native SQL Criteria Query JPQL

Query Native SQL Criteria Query JPQL

Associations § One to One § One to Many § Many to One §

Associations § One to One § One to Many § Many to One § Many to Many Ø Unidirectional Ø Bidirectional § A unidirectional relationship is valid in only in one direction. It has only an owning side. § A bidirectional relationship is valid in both directions. It has both an owning side and an inverse (non-owning) side.

A B a 1 a 2 a 3 b 1 b 2 b 3

A B a 1 a 2 a 3 b 1 b 2 b 3 Associations A B b 1 a 1 b 2 a 2 b 3 One to One A b 4 B b 1 a 1 b 2 a 2 b 3 b 4 Many to Many One to Many A B a 1 a 2 a 3 b 1 b 2 Many to one

Demerits of ORM

Demerits of ORM

Thanks!

Thanks!

Questions ? ? ?

Questions ? ? ?