Friday, February 18, 2011

Introduction to JPA


Introduction
  • Saving : During database transactions, Java Objects are saved into database as table rows.
  • Retrieving : Table rows are returned and converted to Java objects.
  • Impedance mismatch
    • a) Database tables, rows, and columns are relational and have database types. 
    • b)Java objects can be object oriented and have Java types and behaviors.
    • It is vital that a Java programmer must spend a lot of time in SQL and JDBC programming.
  • Advantage : Java Persistence API (JPA) helps developers to avoid the task of SQL and JDBC programming. 
  • OOP : Java classes and their fields are mapped to database tables and columns. This mapping will help the programmers to just concentrate on Object Oriented principles.
Two Main Tasks of JPA

1. Object Relation Mapping (ORM)

  • Mapping : Entity classes and their properties (fields) are mapped to relational database tables and columns.
  • Relationship : Relationships (one-to-one, one-to-many, many-to-one, one-to-many, hierarchical) between the entities are mapped to primary key and foreign key columns in the database tables.
2. Operations on data

  • CRUD : Four main operations in any application are create, read, update and delete.
  • Translation : JPA translates Java operations into database operations.
  • Persistence : Translation of operations persist Java objects into database tables with JPA generating JDBC calls.
  • SQL statements : During Persistence SQL statements are automatically created behind the scene by JPA.

Philosophy of JPA
  • Two Sides of the Same Coin : Java objects and database rows represent same piece of data.
  • Intermediator : JPA is the intermediator among Java Objects and database rows.
  • Single  Operation : Operations on Java objects are now equivalent to database operations.  
  • Perfect Marriage : Java objects and database rows are no more two, but one. Developer deals with Java objects and JPA translates them into database rows. 

No comments:

Post a Comment