CRUD using JPA on Hibernate, MySQL & Maven

This tutorial explains how to perform a simple CRUD using JPA Annotations on Hibernate, Maven and MySQL as Database.

Java Persistence API (JPA) provides POJO (Plain Old Java Object) standard and object relational mapping (OR mapping) for data persistence among applications. Persistence, which deals with storing and retrieving of application data, can now be programmed with Java Persistence API starting from EJB 3.0 as a result of JSR 220. This API has borrowed many of the concepts and standards from leading persistence frameworks like Toplink (from Oracle) and Hibernate (from JBoss). One of the great benefits of JPA is that it is an independent API and can nicely integrate with J2EE as well as J2SE applications.

Lets see how to perform a CRUD Operation i.e. is Create, Read, Update & Delete using JPA on Hibernate, Maven and MySQL.

1. Project Setup

Lets create a simple Maven project with the below pom.xml

In the above pom.xml we added dependencies for Hibernate 3.6.10, MySQL Connector 5.1.25, javassist and sl4j for logging.

2. Database Table Creation

The above script creates a table EMPLOYEE which looks as below

Employee Table

Employee Table

3. JPA Entity Class

Lets us create a JPA Entity  Class Employee.java which is basically a Object Map of the above EMPLOYEE Table and it also contains the mapping specified using annotations.

4. Configuring Hibernate

Now let us create the JPA  Configuration file persistence.xml. This is the master JPA Configuration File wherein we specify the Database URL, Credentials and specify the various mapping files.

 6. Project Structure

The project after the above configuration would look like

JPA Basic Project Structure

JPA Basic Project Structure

7. CRUD Operation

1. CREATE

2. READ

3. UPDATE

4. DELETE

Download JPA MySQL Maven Example Project

I hope this has been useful for you and I’d like to thank you for reading. If you like this article, please leave a helpful comment and share it with your friends.

Leave a Reply

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