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();
}
}
}

Published by

Deepesh Darshan K R

I am Me. In all the world, there is no one else exactly like me. Everything that comes out of me is authentically mine, because I alone chose it. I own everything about me: my body, my feelings, my mouth, my voice, all my actions, whether they be to others or myself. I own my fantasies, my dreams, my hopes, my fears. I own my triumphs and successes, all my failures and mistakes. Because I own all of me, I can become intimately acquainted with me. By so doing, I can love me and be friendly with all my parts. I know there are aspects about myself that puzzle me, and other aspects that I do not know -- but as long as I am friendly and loving to myself, I can courageously and hopefully look for solutions to the puzzles and ways to find out more about me. However I look and sound, whatever I say and do, and whatever I think and feel at a given moment in time is authentically me. If later some parts of how I looked, sounded, thought, and felt turn out to be unfitting, I can discard that which is unfitting, keep the rest, and invent something new for that which I discarded. I can see, hear, feel, think, say, and do. I have the tools to survive, to be close to others, to be productive, and to make sense and order out of the world of people and things outside of me. I own me, and therefore, I can engineer me. I am me, and . . . . . . . . . . I am Okay. Haha.. Let it go.. I am a software engineer working in a MNC here at Kochi itself, specialized on J2EE platform, dribbling with frameworks like struts, hibernate etc.

Leave a comment