Welcome Guest   Login   Mar 28, 2024
Registered Users
Login
Sign-up

 

 


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.*;

@Entity
public class Person implements Serializable {

    @Id
    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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd" version="1.0">
  <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
   <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <class>com.deepak.repository.Person</class>
    <properties>
     <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=""/>
   </properties>
  </persistence-unit>
</persistence>

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();
        entityManager.getTransaction().begin();
        Person person = new Person();
        person.setId(30);
        person.setFirstName("Lata Mangeshkar");
        entityManager.persist(person);
        entityManager.getTransaction().commit();
        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.
http://www.screaming-penguin.com/node/4441



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

Suggestions/Comments/Views
Name:
E-Mail:
(Only admin can see)
Comments:

 

 

My New Blog

My Award winning Whitepaper

Contradicting Light
My Personal Blog


Trace Mobile Location



My Book Reviews
 




Tech Jokes Worth Reading

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