JPA 

This tutorial will show you how to use JPA(Java Persistence API) for selecting, inserting, updating and deleting data into/from database.

The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database.

The JPA is used to reduce the burden of writing codes for relational object management, a programmer or developer follows the ‘JPA Provider’ framework, which allows easy interaction with database instance. The JPA being a Java framework a programmer also needs not to worry about the SQL part of the code, he/she may need to hardly concentrate on SQL coding.

In EJB 3.0 the persistence layer was separated and defined as a replacement of EJB 2 CMP Entity Beans specification.

Being JPA an open source API, various enterprise vendors such as Oracle, Redhat, Eclipse, etc. provide new products by adding the JPA persistence flavor in them. Some of these products include Hibernate, Eclipselink, Toplink, Spring Data JPA.


The following example shows you how to use JPA to write a CRUD application.

For this tutorial we will create a standalone maven project in Eclipse. If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

Prerequisites

The following configurations are required in order to run the application

Eclipse Mars
JDK 1.8
Have maven installed and configured
JPA dependencies in pom.xml

Now we will see the below steps how to create a maven based spring project in Eclipse.

The final project structure is shown below

jpa maven crudPA

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

maven-arctype-quickstart

Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : jpa

Step 2. Modify the pom.xml file as shown below.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>jpa</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>jpa</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
		<hibernate.version>4.3.10.Final</hibernate.version>
		<mysqlconnector.version>5.1.34</mysqlconnector.version>
	</properties>

	<dependencies>
		<!-- JPA -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

		<!-- mysql java connector -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysqlconnector.version}</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Step 3. If you see JRE System Library[J2SE-1.5] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.5], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Step 4. Create src/main/resources folder for putting the resource files.

Do right-click on the project and go New -> Source Folder. Give Folder name: as src/main/resources and click on Finish button.

Then create META-INF folder under src/main/resources source directory.

Step 5. Create an XML file persistence.xml under src/main/resources/META-INF.

Do right-click on META-INF in the project and go New -> file. Give File name: as persistence.xml and click on Finish button.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
	version="2.1">
	<persistence-unit name="userPersistanceUnit"
		transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
		<class>com.roytuts.jpa.entity.UserDetails</class>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cdcol" />
			<property name="javax.persistence.jdbc.user" value="root" />
			<property name="javax.persistence.jdbc.password" value="" />

			<property name="dialect" value="org.hibernate.dialect.MySQLDialect" />
			<property name="hibernate.transaction.flush_before_completion"
				value="true" />
			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.format_sql" value="true" />
		</properties>
	</persistence-unit>
</persistence>

In the above XML file, we have transaction-type as RESOURCE_LOCAL. There are two types of Transaction management types supported in JPA.

RESOURCE LOCAL Transactions
JTA or GLOBAL Transactions

Resource local transactions refer to the native transactions of the JDBC Driver whereas JTA transactions refer to the transactions of the JEE server. A Resource Local transaction involves a single transactional resource, for example a JDBC Connection. Whenever you need two or more resources( for example a JMS Connection and a JDBC Connection )  within a single transaction, you use  JTA Transaction. Container Managed Entity Managers always use JTA transactions as the container takes care of transaction life cycle management and spawning the transaction across multiple transactional resources. Application Managed Entity Managers can use either Resource Local Transactions or JTA transactions.

Normally in JTA or global transaction, a third party transaction monitor enlists the different transactional resources within a transaction, prepares them for a commit and finally commits the transaction. This process of first preparing the resources for transaction(by doing a dry run) and then committing(or rolling back) is called a 2 phase commit.

Step 6. Create below entity class UserDetails under src/main/java folder

package com.roytuts.jpa.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "user_details")
public class UserDetails {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	private int id;

	@Column(name = "first_name")
	private String firstName;

	@Column(name = "last_name")
	private String lastName;

	@Column(name = "email")
	private String email;

	@Column(name = "dob")
	private String dob;

	public UserDetails() {
	}

	public UserDetails(int id, String firstName, String lastName, String email, String dob) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
		this.dob = dob;
	}

	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 String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDob() {
		return dob;
	}

	public void setDob(String dob) {
		this.dob = dob;
	}

	@Override
	public String toString() {
		return "UserDetails [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email
				+ ", dob=" + dob + "]";
	}

}

Step 7. Create below custom exception class JpaException under src/main/java folder

package com.roytuts.jpa.exception;

public class JpaException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	public JpaException(String msg) {
		super(msg);
	}

	public JpaException(Throwable t) {
		super(t);
	}

	public JpaException(String msg, Throwable t) {
		super(msg, t);
	}

}

Step 8. Now create an enum PersistenceManager for creating EntityManagerFactory and EntityManager

package com.roytuts.jpa.manager;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public enum PersistenceManager {

	_INSTANCE;

	private EntityManagerFactory emf;

	private PersistenceManager() {
		emf = Persistence.createEntityManagerFactory("userPersistanceUnit");
	}

	public EntityManager getEntityManager() {
		return emf.createEntityManager();
	}

	public void close() {
		emf.close();
	}

}

Step 9. Now create a class UserDetailsCrud for database operation

package com.roytuts.jpa.crud;

import java.util.List;

import javax.persistence.EntityManager;

import com.roytuts.jpa.entity.UserDetails;
import com.roytuts.jpa.exception.JpaException;

public class UserDeatilsCrud {

	private EntityManager em;

	public UserDeatilsCrud(EntityManager em) {
		this.em = em;
	}

	public UserDetails getUserDetails(int id) {
		try {
			UserDetails userDetails = em.find(UserDetails.class, id);
			return userDetails;
		} catch (JpaException ex) {
			throw new JpaException(ex.getMessage(), ex);
		}
	}

	public List<UserDetails> getUsersDetails() {
		try {
			String sql = "select u from UserDetails u";
			List<UserDetails> userDetails = em.createQuery(sql, UserDetails.class).getResultList();
			return userDetails;
		} catch (JpaException ex) {
			throw new JpaException(ex.getMessage(), ex);
		}
	}

	public void addUserDetails(UserDetails userDetails) {
		try {
			em.getTransaction().begin();
			em.persist(userDetails);
			em.getTransaction().commit();
		} catch (JpaException e) {
			em.getTransaction().rollback();
			throw new JpaException(e.getMessage(), e);
		}
	}

	public void updateUserDetails(UserDetails userDetails) {
		try {
			em.getTransaction().begin();
			UserDetails details = getUserDetails(userDetails.getId());
			details.setFirstName(userDetails.getFirstName());
			details.setLastName(userDetails.getLastName());
			details.setEmail(userDetails.getEmail());
			details.setDob(userDetails.getDob());
			em.getTransaction().commit();
		} catch (JpaException e) {
			em.getTransaction().rollback();
			throw new JpaException(e.getMessage(), e);
		}
	}

	public void deleteUserDetails(int id) {
		try {
			em.getTransaction().begin();
			UserDetails details = getUserDetails(id);
			em.remove(details);
			em.getTransaction().commit();
		} catch (JpaException e) {
			em.getTransaction().rollback();
			throw new JpaException(e.getMessage(), e);
		}
	}

}

Step 10. Create a main class which will test CRUD application.

package com.roytuts.jpa.crud.test;

import java.util.List;

import javax.persistence.EntityManager;

import com.roytuts.jpa.crud.UserDeatilsCrud;
import com.roytuts.jpa.entity.UserDetails;
import com.roytuts.jpa.manager.PersistenceManager;

public class JpaCrudTest {

	public static void main(String[] args) {
		EntityManager em = PersistenceManager._INSTANCE.getEntityManager();

		UserDeatilsCrud userDeatilsCrud = new UserDeatilsCrud(em);

		//user details for user id 7
		try {
			UserDetails userDetails = userDeatilsCrud.getUserDetails(7);
			System.out.println(userDetails);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//get all user details
		try {
			List<UserDetails> detailsList = userDeatilsCrud.getUsersDetails();
			for (UserDetails details : detailsList) {
				System.out.println(details);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		//add new user
		try {
			UserDetails user = new UserDetails();
			user.setFirstName("Liton");
			user.setLastName("Sarkar");
			user.setEmail("liton.sarkar@email.com");
			user.setDob("20-07-1990");
			userDeatilsCrud.addUserDetails(user);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//get all users after addition
		try {
			List<UserDetails> detailsList = userDeatilsCrud.getUsersDetails();
			for (UserDetails details : detailsList) {
				System.out.println(details);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		//update user for id 11
		try {
			UserDetails user = new UserDetails();
			user.setId(13);
			user.setFirstName("Liton");
			user.setLastName("Sarkar");
			user.setEmail("liton.sarkar@email.com");
			user.setDob("14-07-1990");
			userDeatilsCrud.updateUserDetails(user);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//get all users after update
		try {
			List<UserDetails> detailsList = userDeatilsCrud.getUsersDetails();
			for (UserDetails details : detailsList) {
				System.out.println(details);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		//delete user for id 9
		try {
			userDeatilsCrud.deleteUserDetails(9);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//get all users after deletion
		try {
			List<UserDetails> detailsList = userDeatilsCrud.getUsersDetails();
			for (UserDetails details : detailsList) {
				System.out.println(details);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		em.close();
		PersistenceManager._INSTANCE.close();
	}

}

Step 11. Create table user_details in MySQL database and dump some data into it

USE `cdcol`;

/*Table structure for table `user_details` */

DROP TABLE IF EXISTS `user_details`;

CREATE TABLE `user_details` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `first_name` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `last_name` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `dob` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
insert  into 
`user_details`(`id`,`first_name`,`last_name`,`email`,`dob`) 
values 
(7,'Soumitra','Roy','contact@roytuts.com','30-08-2016'),
(8,'Souvik','Sanyal','souvik.sanyal@email.com','30-09-1991'),
(9,'Abc','Xyz','abc@xyz.com','12-01-1998');

Step 12. Run the above main class, you will see below output in the console.

Hibernate: 
    select
        userdetail0_.id as id1_0_0_,
        userdetail0_.dob as dob2_0_0_,
        userdetail0_.email as email3_0_0_,
        userdetail0_.first_name as first_na4_0_0_,
        userdetail0_.last_name as last_nam5_0_0_ 
    from
        user_details userdetail0_ 
    where
        userdetail0_.id=?
UserDetails [id=7, firstName=Soumitra, lastName=Roy, email=contact@roytuts.com, dob=30-08-2016]
Hibernate: 
    select
        userdetail0_.id as id1_0_,
        userdetail0_.dob as dob2_0_,
        userdetail0_.email as email3_0_,
        userdetail0_.first_name as first_na4_0_,
        userdetail0_.last_name as last_nam5_0_ 
    from
        user_details userdetail0_
UserDetails [id=7, firstName=Soumitra, lastName=Roy, email=contact@roytuts.com, dob=30-08-2016]
UserDetails [id=8, firstName=Souvik, lastName=Sanyal, email=souvik.sanyal@email.com, dob=30-09-1991]
UserDetails [id=9, firstName=Abc, lastName=Xyz, email=abc@xyz.com, dob=12-01-1998]
Hibernate: 
    insert 
    into
        user_details
        (dob, email, first_name, last_name) 
    values
        (?, ?, ?, ?)
Hibernate: 
    select
        userdetail0_.id as id1_0_,
        userdetail0_.dob as dob2_0_,
        userdetail0_.email as email3_0_,
        userdetail0_.first_name as first_na4_0_,
        userdetail0_.last_name as last_nam5_0_ 
    from
        user_details userdetail0_
UserDetails [id=7, firstName=Soumitra, lastName=Roy, email=contact@roytuts.com, dob=30-08-2016]
UserDetails [id=8, firstName=Souvik, lastName=Sanyal, email=souvik.sanyal@email.com, dob=30-09-1991]
UserDetails [id=9, firstName=Abc, lastName=Xyz, email=abc@xyz.com, dob=12-01-1998]
UserDetails [id=10, firstName=Liton, lastName=Sarkar, email=liton.sarkar@email.com, dob=20-07-1990]
Hibernate: 
    update
        user_details 
    set
        dob=?,
        email=?,
        first_name=?,
        last_name=? 
    where
        id=?
Hibernate: 
    select
        userdetail0_.id as id1_0_,
        userdetail0_.dob as dob2_0_,
        userdetail0_.email as email3_0_,
        userdetail0_.first_name as first_na4_0_,
        userdetail0_.last_name as last_nam5_0_ 
    from
        user_details userdetail0_
UserDetails [id=7, firstName=Soumitra, lastName=Roy, email=contact@roytuts.com, dob=30-08-2016]
UserDetails [id=8, firstName=Souvik, lastName=Sanyal, email=souvik.sanyal@email.com, dob=30-09-1991]
UserDetails [id=9, firstName=Abc, lastName=Xyz, email=abc@xyz.com, dob=12-01-1998]
UserDetails [id=10, firstName=Liton, lastName=Sarkar, email=liton.sarkar@email.com, dob=14-07-1990]
Hibernate: 
    delete 
    from
        user_details 
    where
        id=?
Hibernate: 
    select
        userdetail0_.id as id1_0_,
        userdetail0_.dob as dob2_0_,
        userdetail0_.email as email3_0_,
        userdetail0_.first_name as first_na4_0_,
        userdetail0_.last_name as last_nam5_0_ 
    from
        user_details userdetail0_
UserDetails [id=7, firstName=Soumitra, lastName=Roy, email=contact@roytuts.com, dob=30-08-2016]
UserDetails [id=8, firstName=Souvik, lastName=Sanyal, email=souvik.sanyal@email.com, dob=30-09-1991]
UserDetails [id=10, firstName=Liton, lastName=Sarkar, email=liton.sarkar@email.com, dob=14-07-1990]

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *