Implementation of JPA

To implement the JPA, first you have to configure it within your Eclipse. To configure you have to follow some steps which we earlier talk in Hibernate Configuration in Eclipse, Only one change you have to do here, that instead of hibernate.cfg.xml file you have to create persistence.xml file inside META-INF folder and either you can remove hibernate3.jar.

Example of JPA


Customer.java File
package com.ducat.jpa;

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

@Entity
public class Customer {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int cno;
	private String name;
	private String address;
	public int getCno() {
		return cno;
	}
	public void setCno(int cno) {
		this.cno = cno;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "Customer [cno=" + cno + ", name=" 
		+ name + ", address=" + address + "]";
	}
}	

CustomerMain.java File
package com.ducat.jpa;

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

public class CustomerMain {

	public static void main(String[] args) {
		 EntityManagerFactory emf = 
		 Persistence.createEntityManagerFactory("myunit");
	        EntityManager em = emf.createEntityManager();
	 
	        try {
	            em.getTransaction().begin();
	           
	            Customer customer = new Customer();
	            customer.setName("jtechies");
	            customer.setAddress("121 jtechies state");
	             
	            em.persist(customer);
	             
	            em.getTransaction().commit();
	            em.getTransaction().begin();
	             
	             customer = em.find(Customer.class, 1);

	            em.getTransaction().commit();
	            System.out.println("customer "+customer);
	        }
	        catch (Exception e) {
	            em.getTransaction().rollback();
	            e.printStackTrace();
	        }
	        finally{
	            emf.close();
	        }
	}
}

persistence.xml File
<?xml version="1.0" encoding="UTF-8"?>
 
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/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">
     
    <persistence-unit name="myunit"
    	transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence
        </provider>
         
    <properties>
        <property name="hibernate.show_sql"
        	value="true" />
        <property name="javax.persistence.jdbc.driver"
        value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url"
        value="jdbc:mysql://localhost:3306/emp_db"/>
        <property name="javax.persistence.jdbc.user"
        value="root" />
        <property name="javax.persistence.jdbc.password"
        value="root" />
        <property name="hibernate.dialect"
        value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.hbm2ddl.auto"
        value="create"/>
        </properties>
    </persistence-unit>
    
</persistence>

Output on Console
Hibernate: insert into Customer (address, name) values (?, ?)
customer Customer [cno=1, name=jtechies, address=121 jtechies state]

Table in Mysql

JPA output