☰ See All Chapters |
JPA Tutorial - Introduction to JPA and ORM
JPA (Java Persistence API) is an Object Relational Mapping Technology (ORM) used to store the data by mapping java class with the Database table, member variables with the columns and making java class objects representing rows of DB tables by having synchronization between them. JPA is a specification from Oracle/Sun, and we have to use the implementation from any of the providers like Hibernate, OpenJPA etc… JPA internally uses JDBC but it has greater advantages over JDBC. This tutorial provides you the complete in depth tutorial of JPA, and we will learn the understanding of JAVA Persistence API (JPA).
Introduction to JPA and ORM
Any storage system which stores the data permanently is called as persistence store. Ex: Files, Database’s etc… Insert Update, delete, and select operations on the DB table are called persistence operations. The logic used for this purpose is called as persistence logic. The technologies which are used to develop persistence logic are called as persistence technologies. Ex: JDBC, Hibernate, JPA, IBATIS.
Object Relational Mapping (ORM)
Mapping Java Class with the Database table, member variables with the columns and making that java class objects representing rows of DB tables by having synchronization between them is called Object Relational Mapping(ORM). Synchronization between objects and rows of tables is nothing but, modifications done in java objects reflect in rows of tables and vice versa. ORM technologies like JPA, hibernate, TopLink, Ibatis etc., are responsible for this Synchronization and to develop objects based ORM persistence logic. All ORM technologies internally use JDBC code to perform persistence operations on DB table rows. Hence these ORM technologies are providing abstraction layer to programmers on JDBC. So ORM technologies are also called as framework technologies.
Advantages of JPA
JPA Supports POJO-POJI model Programming
JPA is Light –Weight technology to develop DB independent persistence logic.
JPA Allows to work with any JAVA, JEE framework softwares based applications to make them interacting with DB software.
When we want to do distribute transactions in JDBC we have to implement JTA API (Java Transaction API). But JPA provides built in transactions management, connection pooling.
JPA supports two levels of caching (or) Buffering to reduce network round trips between client applications and Database.
JPA provides JQL (JPA Query Language) as Database independent language to perform persistence operations.
From Entity classes (we will read about entity classed in coming chapters) we can see all the relations between tables. Easy readability
JPA Supports automatic versioning of rows but JDBC Not.
JPA allows object level relationship in development of persistence logic, when tables are there in relationships like 1-1,1-n,n-n etc. JPA uses fetching strategy for retrieving associated objects if the application needs to navigate the association. You can navigate object relationships transparently. Related objects are automatically loaded as needed. For example if you load a PO (Purchase Order) and you want to access its Customer, you can simply access PO. The ORM will take care of loading the Customer data for you without any effort on your part.
When we are using Data source connection, in JDBC we have to implement JNDI API (Java Naming and Directory Interface API). But JPA provides built in support for Data source connection.
Can JPA be used alone without hibernate
No, JPA cannot be used alone. JPA is a specification from Oracle/Sun, and we have to use the implementation from any of the provider. Hibernate, OpenJPA and many providers have implemented this JPA specification. In our tutorials we used Hibernate implementation.
Hibernate has implemented JPA, and along with this, hibernate has its own ORM implementation.
Many organizations are using plain hibernate and hibernate came before JPA. After JPA is released, hibernate has implemented this JPA.
Please remember, if you are using JPA with hibernate implementation, then you should say that you are using JPA not hibernate. I have seen developers who are software architects don’t know the difference between JPA and hibernate. Now you should be proud, because you know the difference between JPA and Hibernate. JPA is a specification and Hibernate is an implementation, also remember Hibernate has its own ORM implementation, means before JPA, many developers and organizations were using hibernate own ORM implementation. So hibernate has two implementation, one is implementation for JPA and second is its own ORM implementation.
Also remember if you have learnt either Hibernate or JPA, learning other is just an hour work for you.
Which java package is used for working with hibernate and JPA?
javax.persistence – JPA (JPA with any implementation)
org.hibernate –- Hibernate (Hibernates own ORM implementation)
JPA Example Program
In the below example Student class is mapped to Student table of database. Classes which are mapped to database tables are called as persistence classes or entity classes.
Student.java
package com.java4coding;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table;
@Entity @Table(name = "STUDENT") public class Student { @Id @Column(name = "id") private int id;
@Column(name = "fistName") private String firstName;
@Column(name = "lastName") private String lastName;
@Column(name = "marks") private int marks;
public Student() {
}
public Student(int id, String firstName, String lastName, int marks) { super(); this.id = id; this.firstName = firstName; this.lastName = lastName; this.marks = marks; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public int getMarks() { return marks; }
public void setMarks(int marks) { this.marks = marks; } } |
Test.java
package com.java4coding;
import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence;
public class Test { private static EntityManager em;
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("StudentPU"); em = emf.createEntityManager();
createStudent(1, "Manu", "Manjunatha", 100); createStudent(2, "Likitha", "Manjunatha", 98); createStudent(3, "Advith", "Tyagraj", 99); }
private static void createStudent(int id, String firstName, String lastName, int marks) { em.getTransaction().begin(); Student student = new Student(id, firstName, lastName, marks); em.persist(student); em.getTransaction().commit(); } } |
All Chapters