Java Persistence API (JPA) for Data Management in Java Applications

undefined
 
JPA
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
 
 
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
 
Benefits of JPA
Benefits of JPA
 
Provides complete ORM solution for J2SE
and J2EE applications
Easy to use , no need to implement any
framework interfaces  or classes
Facilitate test driven development
Annotation Driven, No XML  mapping
needed
 
Goals of JPA
Goals of JPA
 
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
 
 
 
 
POJO Requirements
POJO Requirements
 
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
 
JPA Annotations
JPA Annotations
 
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
 
    @IdClass  -Maps multiple fields to table PK
 
    @EmbeddedId  -Map Pk class to table PK
Composite primary classes must
  
Implement serializable
  
Override equals() and hashCode()
 
Persistent Identifiers
Persistent Identifiers
 
Supports auto generated primary key value
 
Strategies  defined by GenerationType
enum are
       GenerationType.AUTO
 
GenerationType.IDENTITY
 
GeneratioType.SEQUENCE
 
GenerationType.TABLE
 
@GeneratedValue
@GeneratedValue
 
@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
Commonly Used
Annotations
Annotations
 
@
Temporal
   Used  with  java.uil.Date  or
java.util.Calendar to determine  how value
is persisted
Values defined are
    TemporalType.TIME (java.sql.Time)
    TemporalType.DATE (java.sql.Date)
TemporalType.TIMESTAMP
 
(java.sql.Timestamp)
 
Commonly Used
Commonly Used
Annotations  Contd
Annotations  Contd
 
@
Enumerated
    Used to determine the strategy for persisting
java enum values to database
 
Values defined are
     EnumType.ORDINAL
     EnumType.STRING
 
Commonly Used
Commonly Used
Annotations  Contd
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
 
Commonly Used
Commonly Used
Annotations  Contd
Annotations  Contd
 
JPA Architecture
JPA Architecture
 
EntityManagerFactory
EntityManagerFactory
 
It gives an application-managed entity
manager
An EntityManagerFactory can have several
Entity Managers
Once an EntityManagerFactory is closed, all
its entity managers are in the closed state.
 
EntityManager
EntityManager
 
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
Persistence Unit
 
A set of all entity classes managed by
EntityManager instances.
Defined in 
META-INF/persistence.xml
.
Identified with a unique name.
 
Entity
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 2.1 Vendors
 
ORM
ORM
 
Converts data between
incompatible type systems in
object-oriented programming
languages.
Creates a "virtual object DB“.
Make RDBMS looks like ODBMS.
 
Hibernate
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
CRUD
 
Persist()
Find()
Merge()
Remove()
 
Query
Query
 
Native SQL
JPQL
Criteria Query
Named Query (XML & Annotation)
 
Query
Query
 
C
r
i
t
e
r
i
a
Q
u
e
r
y
 
N
a
t
i
v
e
S
Q
L
 
J
P
Q
L
 
Associations
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.
 
Associations
Associations
 
B
 
A
 
a1
a2
a3
 
b1
b2
b3
 
One to One
 
B
 
A
 
 
a1
 
a2
 
b1
 
b2
 
b3
 
b4
 
One to Many
 
B
 
A
 
a1
a2
a3
 
b1
 
b2
 
Many to one
 
B
 
A
 
 
a1
 
a2
 
b1
 
b2
 
b3
 
b4
 
Many to Many
 
Demerits of ORM
Demerits of ORM
 
Thanks!
 
Questions ???
Slide Note
Embed
Share

Java Persistence API (JPA) is a specification that provides a framework for managing relational data in Java applications. It simplifies ORM solutions, offers vendor independence, and supports test-driven development with annotation-driven mapping.

  • Java Persistence API
  • JPA
  • ORM
  • Data Management
  • Java Applications

Uploaded on Oct 07, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. 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 - 11th May 2006 JPA 2.1 - 22nd April 2013

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

  3. Goals of JPA Provides complete ORM solution for J2SE and J2EE applications Easy to use , no need to implement any framework interfaces or classes Facilitate test driven development Annotation Driven, No XML mapping needed

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

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

  6. 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 @IdClass -Maps multiple fields to table PK @EmbeddedId -Map Pk class to table PK Composite primary classes must Implement serializable Override equals() and hashCode()

  7. @GeneratedValue Supports auto generated primary key value Strategies defined by GenerationType enum are GenerationType.AUTO GenerationType.IDENTITY GeneratioType.SEQUENCE GenerationType.TABLE

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

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

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

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

  12. JPA Architecture

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

  14. EntityManager 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

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

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

  17. Entity Life Cycle

  18. JPA 2.1 Vendors JPA

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

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

  21. Hibernate Architecture

  22. JPA with Hibernate

  23. CRUD Persist() Find() Merge() Remove()

  24. Query Native SQL JPQL Criteria Query Named Query (XML & Annotation)

  25. Query Native SQL Criteria Query JPQL

  26. Associations One to One Unidirectional One to Many Many to One Bidirectional Many to Many 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.

  27. Associations B A A B a1 a2 a3 b1 b2 b3 b1 a1 b2 a2 b3 One to One b4 A B One to Many A b1 B a1 b2 a1 a2 a3 b1 a2 b3 b2 b4 Many to Many Many to one

  28. Demerits of ORM

  29. Thanks!

  30. Questions ???

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#