KEMBAR78
Java persistence api | PPT
Luis Goldster
 What is Java Persistence API
 Primary Features
 Five Steps to Implement JPA
◦ Download Hibernate Components
◦ Prepare Database, and Download JDBC Driver
◦ Implemented POJO entities and add annotations
◦ Persistence.xml
◦ Implemented client side code via EntityManager
 The Java Persistence API is the standard
object/relational mapping and persistence
management interface of the Java EE 5.0
platform. As part of the EJB 3.0 specification
effort, it is supported by all major vendors of the
Java industry.
Inheritance, polymorphism, etc.Inheritance, polymorphism, etc.
Using annotations and/or XMLUsing annotations and/or XML
Java Application Java Persistence API
Hibernate TopLink Kodo (OpenJPA)
Everyone can use their own favorite persistence technology
Hibernate CoreHibernate Core
Hibernate Annotation (JPA)
Hibernate EntityManager
Hibernate Annotation (JPA)
Hibernate EntityManager
Hibernate Annotation (Hibernate)
Hibernate XML Mapping File
Hibernate Annotation (Hibernate)
Hibernate XML Mapping File
Java Persistence APIJava Persistence API Hibernate APIHibernate API
1. Hibernate Core
2. Hibernate EntityManager
3. Hibernate Annotations
http://www.hibernate.org/
MySQL JDBC Driver
http://tinyurl.com/ymt6rb
1. Hibernate Core
2. Hibernate EntityManager
3. Hibernate Annotations
http://www.hibernate.org/
1. Hibernate Core
2. Hibernate EntityManager
3. Hibernate Annotations
http://www.hibernate.org/
MySQL JDBC Driver
http://tinyurl.com/ymt6rb
MySQL JDBC Driver
http://tinyurl.com/ymt6rb
To label artifacts
(classes, methods etc.) for
persistence or persistence
related operations
To label artifacts
(classes, methods etc.) for
persistence or persistence
related operations
A “gateway” to the
persistence classes
A “gateway” to the
persistence classes
Allow access to persistent
objects, transaction context,
query language etc.
Allow access to persistent
objects, transaction context,
query language etc.
 Hibernate Annotations includes
◦ Standardized Java Persistence and EJB 3.0 (JSR 220)
object/relational mapping annotations
◦ Hibernate-specific extension annotations for performance
optimization and special mappings
 Hibernate EntityManager includes
◦ The standard Java Persistence management API
◦ The standard Java Persistence Query Language
◦ The standard Java Persistence object lifecycle rules
◦ The standard Java Persistence configuration and
packaging
1. Hibernate Core
2. Hibernate EntityManager
3. Hibernate Annotations
http://www.hibernate.org/
1. Hibernate Core
2. Hibernate EntityManager
3. Hibernate Annotations
http://www.hibernate.org/
MySQL JDBC Driver
http://tinyurl.com/ymt6rb
MySQL JDBC Driver
http://tinyurl.com/ymt6rb
 Attached to a class
 Signify that a class is persistent
 Example:
 An entity must follow the Java Bean convention
for its attributes to be persistent
◦ Having getters and setters
 Each entity must have an identity
 An identity of an entity could simply be a class
variable annotated with @Id
 Example
 Id can be auto generated
@Id(generate=GeneratorType.AUTO)
 There are other strategies such as
◦ GeneratorType.SEQUENCE
◦ GeneratorType.IDENTITY
 AUTO is best for portability between database
vendors
 @Column, is put on getter of a class variable
 Has several functionalities
◦ Updatable (boolean)
◦ Nullable (updatable)
◦ Length (int)
◦ Example:
 There are 4 types of links
◦ @OneToOne
◦ @OneToMany
◦ @ManyToOne
◦ @ManyToMany
 In most cases, putting the annotation on a getter
of a class variable would be enough
 In some cases, we need to identify a few
parameters to the annotations
 Two cases
◦ Two entities share the same primary key value
 Entity objects have two distinct modes
◦ Attached
 The object is in the database
◦ Detached
 The object is in memory acting as a DTO
 Modification on detached object would not be persisted
automatically
 Developers need to persist detached objects using a
primitive
 Entity manager:
◦ Gateway to persistent classes
◦ Enable queries
◦ Outside of session beans, provides transaction facility
1. Hibernate Core
2. Hibernate EntityManager
3. Hibernate Annotations
http://www.hibernate.org/
1. Hibernate Core
2. Hibernate EntityManager
3. Hibernate Annotations
http://www.hibernate.org/
MySQL JDBC Driver
http://tinyurl.com/ymt6rb
MySQL JDBC Driver
http://tinyurl.com/ymt6rb
Entity classesEntity classes
JDBC
Driver
JDBC
Driver
JDBC
URL
JDBC
URL
User nameUser name
passwordpassword
EntityManagerFactory
Name
EntityManagerFactory
Name
1. Hibernate Core
2. Hibernate EntityManager
3. Hibernate Annotations
http://www.hibernate.org/
1. Hibernate Core
2. Hibernate EntityManager
3. Hibernate Annotations
http://www.hibernate.org/
MySQL JDBC Driver
http://tinyurl.com/ymt6rb
MySQL JDBC Driver
http://tinyurl.com/ymt6rb
CreateCreate
CreateCreate
OperatesOperates
Persistence.xmlPersistence.xml
Java persistence api
Java persistence api
Java persistence api
Java persistence api

Java persistence api

  • 1.
  • 2.
     What isJava Persistence API  Primary Features  Five Steps to Implement JPA ◦ Download Hibernate Components ◦ Prepare Database, and Download JDBC Driver ◦ Implemented POJO entities and add annotations ◦ Persistence.xml ◦ Implemented client side code via EntityManager
  • 3.
     The JavaPersistence API is the standard object/relational mapping and persistence management interface of the Java EE 5.0 platform. As part of the EJB 3.0 specification effort, it is supported by all major vendors of the Java industry.
  • 4.
    Inheritance, polymorphism, etc.Inheritance,polymorphism, etc. Using annotations and/or XMLUsing annotations and/or XML
  • 5.
    Java Application JavaPersistence API Hibernate TopLink Kodo (OpenJPA) Everyone can use their own favorite persistence technology
  • 6.
    Hibernate CoreHibernate Core HibernateAnnotation (JPA) Hibernate EntityManager Hibernate Annotation (JPA) Hibernate EntityManager Hibernate Annotation (Hibernate) Hibernate XML Mapping File Hibernate Annotation (Hibernate) Hibernate XML Mapping File Java Persistence APIJava Persistence API Hibernate APIHibernate API
  • 7.
    1. Hibernate Core 2.Hibernate EntityManager 3. Hibernate Annotations http://www.hibernate.org/ MySQL JDBC Driver http://tinyurl.com/ymt6rb
  • 8.
    1. Hibernate Core 2.Hibernate EntityManager 3. Hibernate Annotations http://www.hibernate.org/ 1. Hibernate Core 2. Hibernate EntityManager 3. Hibernate Annotations http://www.hibernate.org/ MySQL JDBC Driver http://tinyurl.com/ymt6rb MySQL JDBC Driver http://tinyurl.com/ymt6rb
  • 9.
    To label artifacts (classes,methods etc.) for persistence or persistence related operations To label artifacts (classes, methods etc.) for persistence or persistence related operations A “gateway” to the persistence classes A “gateway” to the persistence classes Allow access to persistent objects, transaction context, query language etc. Allow access to persistent objects, transaction context, query language etc.
  • 10.
     Hibernate Annotationsincludes ◦ Standardized Java Persistence and EJB 3.0 (JSR 220) object/relational mapping annotations ◦ Hibernate-specific extension annotations for performance optimization and special mappings  Hibernate EntityManager includes ◦ The standard Java Persistence management API ◦ The standard Java Persistence Query Language ◦ The standard Java Persistence object lifecycle rules ◦ The standard Java Persistence configuration and packaging
  • 11.
    1. Hibernate Core 2.Hibernate EntityManager 3. Hibernate Annotations http://www.hibernate.org/ 1. Hibernate Core 2. Hibernate EntityManager 3. Hibernate Annotations http://www.hibernate.org/ MySQL JDBC Driver http://tinyurl.com/ymt6rb MySQL JDBC Driver http://tinyurl.com/ymt6rb
  • 14.
     Attached toa class  Signify that a class is persistent  Example:  An entity must follow the Java Bean convention for its attributes to be persistent ◦ Having getters and setters
  • 15.
     Each entitymust have an identity  An identity of an entity could simply be a class variable annotated with @Id  Example
  • 16.
     Id canbe auto generated @Id(generate=GeneratorType.AUTO)  There are other strategies such as ◦ GeneratorType.SEQUENCE ◦ GeneratorType.IDENTITY  AUTO is best for portability between database vendors
  • 17.
     @Column, isput on getter of a class variable  Has several functionalities ◦ Updatable (boolean) ◦ Nullable (updatable) ◦ Length (int) ◦ Example:
  • 18.
     There are4 types of links ◦ @OneToOne ◦ @OneToMany ◦ @ManyToOne ◦ @ManyToMany  In most cases, putting the annotation on a getter of a class variable would be enough  In some cases, we need to identify a few parameters to the annotations
  • 19.
     Two cases ◦Two entities share the same primary key value
  • 20.
     Entity objectshave two distinct modes ◦ Attached  The object is in the database ◦ Detached  The object is in memory acting as a DTO  Modification on detached object would not be persisted automatically  Developers need to persist detached objects using a primitive
  • 21.
     Entity manager: ◦Gateway to persistent classes ◦ Enable queries ◦ Outside of session beans, provides transaction facility
  • 22.
    1. Hibernate Core 2.Hibernate EntityManager 3. Hibernate Annotations http://www.hibernate.org/ 1. Hibernate Core 2. Hibernate EntityManager 3. Hibernate Annotations http://www.hibernate.org/ MySQL JDBC Driver http://tinyurl.com/ymt6rb MySQL JDBC Driver http://tinyurl.com/ymt6rb
  • 23.
    Entity classesEntity classes JDBC Driver JDBC Driver JDBC URL JDBC URL UsernameUser name passwordpassword EntityManagerFactory Name EntityManagerFactory Name
  • 24.
    1. Hibernate Core 2.Hibernate EntityManager 3. Hibernate Annotations http://www.hibernate.org/ 1. Hibernate Core 2. Hibernate EntityManager 3. Hibernate Annotations http://www.hibernate.org/ MySQL JDBC Driver http://tinyurl.com/ymt6rb MySQL JDBC Driver http://tinyurl.com/ymt6rb
  • 25.