Hibernate JPA CRUD Operations

Hi friends,

Create, Read, Update and Delete are the four basic operations of persistence storage. We can say these operations collectively as an acronym CRUD. Here we are going to see how these operations are implemented in JPA. Here we can see the Hibernate implementation of JPA API.

What is JPA ?

  • JPA is a JAVA API Specification.
  • It’s an Entity persistence model for EJB 3.0.
  • It’s a POJO based framework for Java Persistence.
  • It Manages Relational Data in JAVA applications.

Benefits of JPA

JPA provides complete ORM solution for J2SE and J2EE applications. It’s easy to use, no need to implement any framework interfaces or classes. It facilitates test driven development and It’s annotation driven.

The major benefits of JPA are given below.

  • 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.

JPA Architecture

jpa_architecture

JPA Vendors

These are the major JPA vendors.

jpa_vendors

What is an ORM ?

ORM stands for Object Relational Mapping. It converts data between incompatible type systems in object-oriented programming languages. It creates a “virtual object DB“. It makes RDBMS looks like ODBMS.

orm

What is Hibernate ?

  • It’s a well known ORM tool.
  • It’s the most popular JPA API implementation.
  • It maps the Relational Model in the database to the Object Model in Java.

Hibernate Architecture

hibernate_architecture

CRUD Operations in JPA

These are the four methods used for CRUD operations in JPA API.

  • Persist()
  • Find()
  • Merge()
  • Remove()

Okay, now let’s move on to the example

log4j.properties

log4j.rootLogger = INFO, FILE
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=C://log.out
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=#[%d{dd/MMM/yyyy HH:mm:ss}] - File[%F] - Method[%M] - Line[%L] - Priority[%p] - Message[%m]%n

Crud.java

package com.ddkr.jpahibernate.biz;

import com.ddkr.jpahibernate.entity.Employee;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Crud {

private EntityManagerFactory emf = null;
private EntityManager em = null;

public static void main(String[] args) throws Exception {
try {
Crud.class.newInstance().create();
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}

public Crud() {
try {
emf = Persistence.createEntityManagerFactory("JPA-Hibernate-PU");
em = emf.createEntityManager();
} catch (Exception e) {
System.out.println(e.getCause());
}
}

private void create() throws Exception {
try {
em.getTransaction().begin();
Employee employee = new Employee("Angelina", "Jolie", 'F', 39);
em.persist(employee);
em.getTransaction().commit();
} catch (Exception e) {
em.getTransaction().rollback();
System.out.println(e.getLocalizedMessage());
throw new Exception(e);
} finally {
em.close();
}
}

private void read() throws Exception {
try {
em.getTransaction().begin();
Object empObj = em.find(Employee.class, 1L);
System.out.println(empObj);
em.getTransaction().commit();
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
throw new Exception(e);
} finally {
em.close();
}
}

private void update() throws Exception {
try {
em.getTransaction().begin();
Employee employee = em.find(Employee.class, 1L);
employee.setFirstName("Jennifer");
employee.setLastName("Lawrence");
employee.setAge(23);
em.merge(employee);
em.getTransaction().commit();
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
throw new Exception(e);
} finally {
em.flush();
em.close();
}
}

private void delete() throws Exception {
try {
em.getTransaction().begin();
Employee employee = em.find(Employee.class, 1L);
em.remove(employee);
em.getTransaction().commit();
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
throw new Exception(e);
} finally {
em.flush();
em.close();
}
}
}