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();
}

}

Struts2 Hibernate Integration java programmer can use it in simplified manner


 Create A Model Class  A class:

package Model;

public class Emp
{

String name,job;
int salary;
int id;
public Emp() {
super();
// TODO Auto-generated constructor stub
}
public Emp(String name, String job, int salary) {
super();
this.name = name;
this.job = job;
this.salary = salary;
}
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;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

}

Create Hibernate Session Factory

package HibernateUtile;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class MySesssionFactory
{
static SessionFactory factory;
static
{
Configuration cfg=new Configuration().configure();
factory=cfg.buildSessionFactory();
}
public static Session getSession()
{
return factory.openSession();
}
}


Create Dao Class

package Dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import HibernateUtile.MySesssionFactory;
import Model.Emp;

public class EmpDao
{
public void insert(Emp emp)
{
Session session=MySesssionFactory.getSession();
Transaction tr=session.beginTransaction();
session.save(emp);
tr.commit();
session.close();
}
}


Create Hibernate Configuration


 <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="connection.username">system</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
        <property name="connection.password">oracle</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="show_sql">true</property>
    <mapping resource="Hibernate.hbm.xml"/>
    </session-factory>

</hibernate-configuration>



Create Hibernate Mapping File

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-mapping>
 <class name="Model.Emp">
    <id name="id" type="int">
    <generator class="increment"/>
    </id>
    <property name="name"/>
    <property name="job"/>
    <property name="salary" type="int"/>

</class>
</hibernate-mapping>



Create Controller Page

package View;

import Dao.EMpDao;
import Model.Emp;

public class ActionAaAview
{
private Emp e;

public Emp getE() {
return e;
}

public void setE(Emp e) {
this.e = e;
}
public String execute()
{
EmpDao ctr=new EmpDao();
ctr.insert(e);
return "success";
}
}



Create struts.xml file

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="demo" extends="struts-default">
<action name="add" class="View.ActionAaAview">
<result name="success">/Welcome.jsp</result>
<!--<result name="failure">/Retrive.jsp</result>-->
</action>
</package>
</struts>



Copy bellow text and paste in web.xml file


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


Create index.jsp

<%@taglib prefix="s" uri="/struts-tags"%>

<s:form action="add" >

<s:textfield name="Emp.name" label="Name"/>
<s:textfield name="Emp.job" label="Job"/>
<s:textfield name="Emp.salary" label="Salary"/>
<s:token/>
<s:submit name="Register"/>
<s:reset name="Reset"/>


</s:form>



Create welcome.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
Welcome<s:property value="Emp.name"/>

</s:form>


Create retrive.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
<s:form action="add" method="post">
<s:textfield name="name" label="Name"/>
<s:textfield name="job" label="Job"/>
<s:textfield name="salary" label="Salary"/>
<s:submit name="Register"/>
<s:reset name="Reset"/>
<s:textfield />

</s:form>