JaiswalTraining

Get the online training


Warning: include(../../../content.php): failed to open stream: No such file or directory in /home/jaiswaltraining/public_html/jpa/rad/index.php on line 27

Warning: include(../../../content.php): failed to open stream: No such file or directory in /home/jaiswaltraining/public_html/jpa/rad/index.php on line 27

Warning: include(): Failed opening '../../../content.php' for inclusion (include_path='.:/opt/php54/lib/php') in /home/jaiswaltraining/public_html/jpa/rad/index.php on line 27
Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven









         JPA program on  RAD


  • Web JPA program

  • In this section we will make jpa program on  RAD
  • JPA is used to solve the complex progamming  in simple way 
  • It is easy to make ejb as well as web jpa program on the RAD.
  • JPA is used with servlet jap and ejb3 for better jdbc conectivity.
  • It provides the server side jdbc Connectivity.

  • Install the RAD7.5 on your system.
  • JPA is not supported on RAD7 which is
    having Websphere Application Server6.1
  • RAD7.5 is having Websphere Application Server7.0
    which supports EJB3.0 and JPA

  • Open the IBM RAD7.5
  • Fisrt take a Dynamic Web Project in the RAD.
  • Type the name of the project  as EmpDemo.
  • Check the Add project to an EAR
  • Give the EAR name as ejb
  • Select the server as websphere Application Server 7.0
  • Click on the Finish button.




  • This creates a Emp  project .

Adding JPA Facets
  • By default the dynamic project is not  having  JPA  properties.
  • So  user need to add the JPA  Facets to this project.
  • Right Click on the project.
  • Select the properties.
  • Here select the Project Facets.
  • Check the java Persistence.
  • Click on the Ok button.
 

  • This step will add the JPA facets to the project..
  • A foder JPA Conetent  gets created.
  • JPA Conetnt is having  persistence.xml




Creating Database and table in Db2 in Control Center.

  • Control Center is a gui tool to create database, table in DB2.
  • User can modify, insert, update  the records in it.
  • Open the Control Center  as given below.



  • Create the database as shown below.
  • Right click on the All Databases-->Create Database-->Standard




  • Type the database name as Emp.
  • Click on the Finish  button.
  • It will create the Emp database.






Creating table

  • Expand the EMP database node.
  • Right Click on the Table node.
  • Select the Create option



  • A wizard for table creation gets started.
  • Type  table name employee.
  • Select the schema


  • Click on the next Button.
  • Now add  the column name along with dada type.
  • Click on the next Button.




  • Adding Constraints

      • We have not defined any primary key or foreign key in the table.
      • Now we will add the primary key for the Column EID.
      • Click on the Add Primary Key Button.





  • Select the Column EID.
  • Click on the OK button.



  • It is added in the given  window.
  • Click on the finish button
  • A employee table will get created.





Adding JPA Facets
  • By default the dynamic project is not  JPA  properties.
  • So  user need to add the JPA  Facets to this project.
  • Right Click on the project.
  • Select the properties.
  • Here select the Project Facets.
  • Check the java Persistence.
  • Click on the Ok button.







Creating the Connection

  • In the given window Click on the Add Connection
    link below Connection.
  • This will open the window for connection creation.



      
  • Click  on the Add Connection  link to add a new
    connection for the specific database.
  • In the upcoming  window select  the required database softare.




  • We will create the connection for db2
  • So select the database manager for db2 as shown below
  • Add the database  name as emp.
  • In the User name type db2admin
  • In the password tye the require password.





  • Click on the Test Connection button to check   the proper setting.
  • If the setting is accurate it will display the Connection  succeeded message.
  • Click on the Ok button.
  • It will create a connection named emp.
  • It will get appended in the persistence.xml as 
    <jta-data-source>java:comp/env/emp</jta-data-source>



Entities Creation

  • Using the jpa tool we can create the Entity file.
  • Right click on the project.
  • Select the JPA tools-->Generate Entities from tables.





  • Select the connection name emp.
  • Select the schema in which your table is present.
  • This schema can be found from the control  cenre of db2.
  • Every table is present under some  schema



  • Click on the next button.
  • It will display the tables present in the given schema.
  • Select the Employee table.
  • Click on theFinish button.




  • It will create a Entity(POJO) file with the given code.
  • Its value will get added into the persistence.xml as
    <class>pack.Employee</class>
package pack;

import java.io.Serializable;

import javax.annotation.Generated;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Employee implements Serializable {
  @Id
  private int eid;

  private String ename;

  private int salary;

  private static final long serialVersionUID = 1L;

  public Employee() {
    super();
  }

  public int getEid() {
    return this.eid;
  }

  public void setEid(int eid) {
    this.eid = eid;
  }

  public String getEname() {
    return this.ename;
  }

  public void setEname(String ename) {
    this.ename = ename;
  }

  public int getSalary() {
    return this.salary;
  }

  public void setSalary(int salary) {
    this.salary = salary;
  }

}


Persistence.xml

  • Open the persistence.xml
  • You will find jata-data-source and class has been added
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd"
>
  <persistence-unit name="EmpDemo">

    <jta-data-source>java:comp/env/emp</jta-data-source>
    <class>pack.Employee</class>

    <properties>
      <property name="openjpa.jdbc.Schema" value="SHANKAR" />
    </properties>
  </persistence-unit>
</persistence>


Jsp Form

  • Create one jsp file in the project.
  • Save its as Form.jsp
  • Add the code as shown below.
  • Add the action attribute  with value testServlet.
<html>
<body>
<form action="testServlet">Eid <input name="eid" size=5>
<br>

<br>
Name <input size=name="name"><br>
<br>
Salary <input name="sal" size=5><br>
<br>
Select Option <select name="op1">
  <option value="add">Add
  <option value="delete">Delete
  <option value="find">Find
  <option value="showall">ShowAll
  <option value="update">update
</select> <br>
<br>

<input type="submit"></form>
</body>
</html>

  • Now create one servlet testServlet.
  • put the code belowto add, show all, find, update and  remove the records.

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pack.Employee;

public class testServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  protected void doGet(HttpServletRequest request,
      HttpServletResponse responsethrows ServletException, IOException {
    PrintWriter out = response.getWriter();
    response.setContentType("text/html");
    String op = request.getParameter("op1");

    EntityManagerFactory fac = Persistence.createEntityManagerFactory(
        "EmpDemo", System.getProperties());

    EntityManager em = fac.createEntityManager();

    Employee emp = new Employee();

    if (op.equals("add")) {
      int eid = Integer.parseInt(request.getParameter("eid"));
      String name = request.getParameter("name");
      int sal = Integer.parseInt(request.getParameter("sal"));

      emp.setEid(eid);
      emp.setEname(name);
      emp.setSalary(sal);

      em.getTransaction().begin();
      em.persist(emp);
      em.getTransaction().commit();
      out.print("record inserted");
    }

    else if (op.equals("delete")) {
      int eid = Integer.parseInt(request.getParameter("eid"));

      Employee e = em.find(Employee.class, eid);
      if (e == null)
        out.print("id is wrong");
      else {
        em.getTransaction().begin();
        em.remove(e);
        em.getTransaction().commit();
        out.print("record deleted");
      }
    else if (op.equals("find")) {
      int eid = Integer.parseInt(request.getParameter("eid"));

      em.getTransaction().begin();
      Employee e = em.find(Employee.class, eid);
      if (e == null)
        out.print("id is wrong");
      else {
        out.print("id is " + e.getEid());
        out.print("<br>name  is " + e.getEname());
        out.print("<br>Salary  is " + e.getSalary());
      }
    else if (op.equals("showall")) {
      List<Employee> l = em.createQuery(
          "select object(o) from Employee as o").getResultList();

      for (Employee emp1 : l) {
        out.print("<br><br>eid is " + emp1.getEid());
        out.print("<br>Name is " + emp1.getEname());
        out.print("<br>Salary is " + emp1.getSalary());
      }
    else if (op.equals("update")) {
      int eid = Integer.parseInt(request.getParameter("eid"));
      String name = request.getParameter("name");
      int sal = Integer.parseInt(request.getParameter("sal"));

      Employee e = em.find(Employee.class, eid);
      if (e == null)
        out.print("id is not present<br>cannot be updated");
      else {

        em.getTransaction().begin();
        e.setEid(eid);
        e.setEname(name);
        e.setSalary(sal);

        em.merge(e);
        em.getTransaction().commit();
        out.print(" record updated with eid " + eid);
      }
    }
  }
}


Running the Program.

  • Right Click on  the form.jsp.
  • Select the Run On server.
  • Select the Websphere Application Server 7.0.


    • Add the record as shown below.



    • Add the record as shown below.





    • Add the record as shown below.




    • Select the option show All.





  • It will display all the added records.




Download EmpDemo code

Download ejb EAR code