What is JPA (Java Persistence API)?

  • The Java Persistence API (JPA) provides an object/relational mapping facility to Java developers for managing relational data in Java applications.
  • JPA is a replacement for the much criticized EJB 2.0 and EJB 2.1
  • JPA is part of EJB 3.0 specification
  • One of the great benefits of JPA is that it is an independent API and can nicely integrate with J2EE as well as J2SE applications
  • Supports both annotations and xml based configuration
  • JPA is a specification and needs an ORM implementation to work and persist the Java Objects.
  • List of ORM frameworks are:
    • Hibernate
    • Toplink from oracle
    • iBatis
    • Open JPA
     
  • Future versions of JPA will be defined in a separate JSR/specification rather than in the EJB JSR/specification.


JPA Architecture

  • JPA is a lightweight, POJO-based Java framework to persist the Java Objects to the relational database.
  • JPA uses metadata (via annotations, via XML or both) to map the persistence objects with the database table
  • JPA supports SQL like query language which can be used to execute both static and dynamic queries.
  • Using JPA it's also very easy to switch to different ORM frameworks.
  • JPA architecture includes the following three components:
    • Entity
    • EntityManager
    • EntityManagerFactory
JPA Architecture
 Entity
  • An Entity is a class which should be persisted in a database, In other words it Signify that a class is persistent
  • Instance of entity represent one record in a table
  • All entity classes must define a primary key and must have a default constructor
  • By default, the table name corresponds (same) to the class name
  • Syntax:
           

EntityManager
  • The EntityManager interface is providing the API for interacting with the Entity, e.g. finds objects, persists them and remove objects from the database, etc.
  • In other words it provides the operations from and to the database.

EntityManagerFactory
  • The EntityManagerFactory is used to create an instance of EntityManager.

No comments:

Post a Comment