Welcome Guest   Login   Jul 25, 2017
Registered Users



Fedora & Vista Blog

Simplest JPA Example    

Step 1:
Create PERSON table in MySQL test database with two fields.
ID with int type
FIRSTNAME with varchar type.
Step 2: Create the project in Eclipse or Netbeans and add these jar files as libraries.
  • ejb3-persistence.jar
  • mysql-connector-java-5.1.6-bin.jar
  • toplink-essentials-agent.jar
  • toplink-essentials.jar

Step 3: Write this POJO class with the name Person.Java.

package com.deepak.repository;

import java.io.Serializable;
import javax.persistence.*;

public class Person implements Serializable {

    private int id;
    private String firstName;

    public String getFirstName() {
        return firstName;

    public int getId() {
        return id;

    public void setFirstName(String firstname) {
        this.firstName = firstname;

    public void setId(int id) {
        this.id = id;

Step 4: Create JPAResourceBean for getting EntityManagerFactory object.

package com.deepak.repository;

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

public class JPAResourceBean {

    protected EntityManagerFactory emf;
     public EntityManagerFactory getEMF (){
        if (emf == null){
            emf = Persistence.createEntityManagerFactory("default", new java.util.HashMap());
        return emf;

Step 5: Write Persistence.xml inside the META-INF folder.
META-INF folder will be at classes source level.
Name of my database is test and I logged on with root userid without any password. You can configure this xml as per your MySQL configurations.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd" version="1.0">
  <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
     <property name="toplink.logging.level" value="FINEST"/>
     <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
     <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/test"/>
     <property name="toplink.jdbc.user" value="root"/>
     <property name="toplink.jdbc.password" value=""/>

Step 6: Create main class to add the record to table.

package com.deepak.repository;

import javax.persistence.EntityManagerFactory;
import oracle.toplink.essentials.ejb.cmp3.EntityManager;
public class Driver
    public static void main(String[] args)
        EntityManagerFactory emf = new JPAResourceBean().getEMF();
        EntityManager entityManager = (EntityManager) emf.createEntityManager();
        Person person = new Person();
        person.setFirstName("Lata Mangeshkar");
        System.out.println("All Saved...");

Step 7: Run the Driver class.
Hopefully everything goes well and you see one record added to Person Table in MySQL.

Hope you enjoyed creating independent JPA program which can work in Java Standard Edition 1.5 above. You don’t need any JEE container to try this example.

You can download required software freely from these locations.
MySQL  -  http://dev.mysql.com/downloads/
SQLYog - http://www.webyog.com/en/downloads.php
NetBeans - http://netbeans.org/downloads/index.html

Also check JPA example by Charlie on this URL.

Posted by: scbcd

Old Articles
Mastering Enterprise JavaBean 3.0 book for SCBCD Certification
Simplest JPA Example
EJB 2.1 example from Mastering Enterprise JavaBean 3.0 book.
No new edition for Head First EJB - SCBCD 5 book

(Only admin can see)



My New Blog

My Award winning Whitepaper

Contradicting Light
My Personal Blog

Trace Mobile Location

My Book Reviews

Tech Jokes Worth Reading

www.deepjava.com 1999-2014 (C) Developed and maintained by D-Kay Consultancy