JPA CRUD Example using eclipse
In this tutorial we will discuss how to create a simple CRUD example using JPA.
Note:-If you are new to JPA please read this article in which I discussed how to create JPA example in Eclipse
Tools Required:
Directory Structure of the Application
In this tutorial we will discuss how to create a simple CRUD example using JPA.
Note:-If you are new to JPA please read this article in which I discussed how to create JPA example in Eclipse
Tools Required:
Directory Structure of the Application
Steps to develop this example
Following is the steps involved for developing this application
Step1: Create tables to store data
Create a table EMP_RECORD with columns empid, firstname, lastname and email.
CREATE TABLE `EMP_RECORD` ( `empid` INT NOT NULL , `firstname` VARCHAR(45) NULL , `lastname` VARCHAR(45) NULL , `email` VARCHAR(45) NULL , PRIMARY KEY (`empid`) );
Step2: Create Java Project
Create a Java project "JPACRUDExample". After that create a folder "lib" inside the project and place the required JPA jars and MySQL Connector jar into this folder and add the jar files to classpath of the project.
Step3: Create Entity Bean
An Entity Bean is a light weight persistent domain object.The Entity Bean is a Java Object that has to follow a few rules Our Entity Bean will be called Employee.Java , and it is created inside the package com.javawebtutor.jpa.pojo .
Employee.java
package com.javawebtutor.jpa.pojo; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "EMP_RECORD") public class Employee implements Serializable { @Id private int empId; private String email; private String firstname; private String lastname; public Employee() { } public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } 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; } @Override public String toString() { return "Employee [empId=" + empId + ", email=" + email + ", firstname=" + firstname + ", lastname=" + lastname + "]"; } }
Step4: Create persistence.xml :
Create a directory "META-INF" in "src" folder and create the file "persistence.xml" inside it and add following code into this file.This file contains details about your database.
persistence.xml
<?xml version="1.0" encoding="UTF-8" ?> <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"> <persistence-unit name="JPACRUD" transaction-type="RESOURCE_LOCAL"> <class>com.javawebtutor.jpa.pojo.Employee</class> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="mukesh" /> </properties> </persistence-unit> </persistence>
Step5 : Test Program for CRUD Operation :
Create a test program Test.java which will perform CRUD operation.
Test.java
package com.javawebtutor.jpa.test; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import com.javawebtutor.jpa.pojo.Employee; public class Test { public static void main(String[] args) { /* Create EntityManagerFactory */ EntityManagerFactory emf = Persistence .createEntityManagerFactory("JPACRUD"); /* Create Entity */ Employee employee = new Employee(); employee.setFirstname("Mukesh"); employee.setLastname("Kumar"); employee.setEmail("m@gmail.com"); employee.setEmpId(12); /* Create EntityManager */ EntityManager em = emf.createEntityManager(); /* Persist entity */ em.getTransaction().begin(); em.persist(employee); em.getTransaction().commit(); /* Retrieve entity */ employee = em.find(Employee.class, 12); System.out.println(employee); /* Update entity */ em.getTransaction().begin(); employee.setFirstname("Ravi"); System.out.println("Update Employee Name is :- " + employee); em.getTransaction().commit(); /* Remove entity */ em.getTransaction().begin(); em.remove(employee); em.getTransaction().commit(); /* Check whether enittiy is removed or not */ employee = em.find(Employee.class, 12); System.out.println("Employee after removal :- " + employee); } }
Output
Now run the Test.java class and you will see following output in the Eclipse console.
Ravi [EL Info]: 2014-11-18 23:19:00.139--ServerSession(3656231)--EclipseLink, version: Eclipse Persistence Services - 1.2.0.v20091016-r5565 [EL Info]: 2014-11-18 23:19:01.155--ServerSession(3656231)--file:/D:/WORK_SPACE/JPA/JPACRUDExample/bin/-JPACRUD login successful Employee [empId=12, email=m@gmail.com, firstname=Mukesh, lastname=Kumar] Update Employee Name is :- Employee [empId=12, email=m@gmail.com, firstname=Ravi, lastname=Kumar] Employee after removal :- null
You can download the source code of the example by clicking on the Download link below.
Related Articles