Example of Find, insert, Update, Delete in JPA

SQL

create table emp(
	eid int,
	ename varchar(40),
	elanguage  varchar(40)
);
insert into emp values (1,'Tyson','Java');
insert into emp values (2,'Justin','Python');
insert into emp values (3,'Martin','C++');

commit;

persistence.xml (/src/main/resources/META-INF/persistence.xml)

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
             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">
    <persistence-unit name="pu" transaction-type="RESOURCE_LOCAL">
    	<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    	<class>com.demo.Emp</class>
        <properties>
           <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
           <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mysql"/>
           <property name="javax.persistence.jdbc.user" value="tyson"/>
           <property name="javax.persistence.jdbc.password" value="tyson"/>
           
           <property name = "hibernate.show_sql" value = "true" />
           <property name = "hibernate.format_sql" value = "true" />
           <property name = "hibernate.hbm2ddl.auto" value = "none" />
           <property name = "hibernate.dilect" value = "org.hibernate.dialect.MySQLDialect" />
        </properties>
        
    </persistence-unit>

</persistence>

Emp.java

package com.demo;

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

@Entity
@Table(name="emp")
public class Emp {
	@Id
	@Column(name="eid")
	private int eid;
	
	@Column(name="ename",nullable=false,length=40)
	private String ename;
	
	@Column(name="elanguage",nullable=false,length=40)
	private String elanguage;
	
	public int getEid() {
		return eid;
	}
	public void setEid(int eid) {
		this.eid = eid;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getElanguage() {
		return elanguage;
	}
	public void setElanguage(String elanguage) {
		this.elanguage = elanguage;
	}
	@Override
	public String toString() {
		return "Emp [eid=" + eid + ", ename=" + ename + ", elanguage=" + elanguage + "]";
	}
}

App.java

package com.demo;

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

public class App {
	public static void main(String[] args) {
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");
		EntityManager em = emf.createEntityManager();
		
		//findExample - select a specific row
		printEmployee(em);
		
		//insert a single row - persist
		createEmployee(em,4,"Fade","C#");
		
		//update a single row 
		updateEmployee(em,"Jade");
		
		//only single delete
		deleteEmployee(em,3);
		
		em.close();
		emf.close();
		
	}
	
	private static void printEmployee(EntityManager em) {
		Emp e = em.find(Emp.class, 3);
		System.out.println(e);
	}
	
	private static void createEmployee(EntityManager em,int id,String name,String language) {
		Emp e = new Emp();
		e.setEid(id);
		e.setEname(name);
		e.setElanguage(language);
		
		em.getTransaction().begin();
		em.persist(e);
		em.getTransaction().commit();
		
	}
	
	private static void updateEmployee(EntityManager em, String name) {
		Emp e = em.find(Emp.class, 3);
		System.out.println(e.toString());
		em.getTransaction().begin();
		e.setEname(name);
		em.getTransaction().commit();
		System.out.println(e.toString());
	}
	
	private static void deleteEmployee(EntityManager em, int id) {
		em.getTransaction().begin(); 
		Emp e = em.find(Emp.class, id);
		em.remove(e);
		em.getTransaction().commit();  
	}
}

Leave a Comment