Saturday, June 13, 2015

Simple Implementation of JPA ( Java Persistence API )

Implementing Simple JPA Application


Technology Stack

1.    Eclipse Java EE IDE for Web Developers - Version: Kepler Service Release 2
2.    Oracle Database 11g -
3.    Persistence Provider - EclipseLink JPA
4.    Architecture – Web Application (MVC)


Jars / Dependencies

     Eclipselink:   eclipselink-2.1.0.v20100614-r7608
     Javax Persistence: javax.persistence-2.1.0
     Oracle JDBC Driver: ojdbc14_g-10.2.0.4



To get started, follow these steps:

Create a Dynamic Web Project or a JPA Project in Eclipse:
·         If JPA project then you can notice the META-INF folder holding the persistence.xml (only if you have correctly set your build path)  if not you can configure build path by adding above dependencies.
·         If Web project you can create persistence.xml in your META-INF folder under webcontent.

We will need following files to complete implementation of Selecting / Creating a record in a Payment table:
a)    POJO class – representing an entity (Payment in our case)
b)    Persistence.xml – holding our database details, persistence provider, class for mapping with any table for data persist (Record insert in table)
c)    Service / DAO class – To create EntityManager instance and using which we can query database. (In JPA a database connection is represented by the EntityManager interface. In order to access and work with an ObjectDB database we need an EntityManager instance)


Sample Entity class

A Payment entity POJO class :




@Entity: To declare the class as a ORM framework’s object class
@Id: To declare the primary key for the Payment table in a Payment Entity class
@Column: To map the attribute in the Entity class with the actual column of the table.


Sample ORM.xml or persistence.xml

<?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="JPATestPaymentCreation">
  <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>payments.Payment</class>
       <properties>
          <property name="javax.persistence.jdbc.driver"
                    value="oracle.jdbc.driver.OracleDriver" />
          <property name="javax.persistence.jdbc.url"
                    value="jdbc:oracle:thin:@AAAA03:1521:AAAA3t" />
          <property name="javax.persistence.jdbc.user" value="XXXXXXXX" />
          <property name="javax.persistence.jdbc.password" value="YYYYYYYYYY" />
        </properties>
       </persistence-unit>
</persistence>

 Sample Service Implementation java file

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.*;

public class TestInsertEmployeeViaJPA {
 private static final String PERSISTENCE_UNIT_NAME = "JPATestEmployeeCreation";
 private static EntityManagerFactory factory;

/**
* @param args
*/
public static void main(String[] args) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        EntityManager em = factory.createEntityManager();
          
        // Read the existing entries and write to console
          Query q = em.createQuery("SELECT emp FROM Employee emp");
           
         List<Employee> empList = q.getResultList();
         for (Employee user : empList) {
              System.out.println(user.getTraceId());
         }
         System.out.println("Size: " + empList.size());
          
          // Create new Employee Record
          em.getTransaction().begin();
          
          Employee testEmployee = new Employee();
          testEmployee.setTraceId(57615061110567l);
          testEmployee.setId(9000);
          testEmployee.setTransactionId(3l);
          testEmployee.setStatusId(1);
          testEmployee.setObligationId(1571);
          testEmployee.setSentToDal(1l);
          
          em.persist(testEmployee);
          em.getTransaction().commit();
          em.close();    
}
}



No comments:

Post a Comment