Showing posts with label JPA2 Without Toplink Implemation. Show all posts
Showing posts with label JPA2 Without Toplink Implemation. Show all posts

Thursday, 16 October 2014

JPA2 Without Toplink Implemation


Create Model Class


package myPack;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity

public class Emp
{
@Id
int id;
String name,job;
int salary;

public Emp() {
super();
}


public Emp(int id, String name, String job, int salary) {
super();
this.id = id;
this.name = name;
this.job = job;
this.salary = salary;
}


public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}

}




Create Factory Class

package myPack;


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

public class MyFactory 
{
static EntityManagerFactory EMF;

static 
{
EMF=Persistence.createEntityManagerFactory("JPA1withTopLinkPU");
}
public static EntityManager getManager()
{
return EMF.createEntityManager();
}
}


Create Persist Class

package myPack;

import javax.persistence.EntityManager;
import javax.persistence.*;

public class PersistDemo {

public static void main(String[] args)
{
EntityManager m=MyFactory.getManager();
Emp e1=new Emp(8,"Bharat","Developer",15000);
Emp e2=new Emp(9,"Ankit","Developer",17000);
System.out.println("Persisting Entities...........");
EntityTransaction t=m.getTransaction();
t.begin();
m.persist(e1);
m.persist(e2);
t.commit();
m.close();
System.out.println("SUCCESSFULLY PERISTED");
}

}

Create Persistence Configuration Xml File


<?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
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    
<persistence-unit name="JPA1withTopLinkPU" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
  <class>myPack.Emp</class>
  <properties>
<property name = "toplink.jdbc.driver" value = "oracle.jdbc.driver.OracleDriver"/>
<property name = "toplink.jdbc.url" value = "jdbc:oracle:thin:@localhost:1521:xe"/>
<property name = "toplink.jdbc.user" value = "system"/>
<property name = "toplink.jdbc.password" value = "oracle"/>
  </properties>
</persistence-unit>
  
</persistence>


Create FetchTest Class

package myPack;

import java.util.Scanner;

import javax.persistence.EntityManager;

public class FetchTest {

public static void main(String[] args) {
EntityManager m=MyFactory.getManager();
Scanner in=new Scanner(System.in);
System.out.println("Enter your id:");
int id=in.nextInt();in.nextLine();
System.out.println("Find method used........");
Emp e1=m.find(Emp.class, id);
System.out.println("Details of fetchd entity using data memeber:");
System.out.println(e1.name+"\t"+e1.job+"\t"+e1.salary);
System.out.println("Details of fetched entity getter Methods");
System.out.println(e1.getName()+"\t"+e1.getJob()+"\t"+e1.getSalary());
System.out.println("GetReference method used......");
Emp e=m.getReference(Emp.class,id);
System.out.println("Details of fetchd entity using data memeber:");
System.out.println(e.name+"\t"+e.job+"\t"+e.salary);
System.out.println("Details of fetched entity getter Methods");
System.out.println(e.getName()+"\t"+e.getJob()+"\t"+e.getSalary());
m.close();
}

}