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

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.


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