Basic For Hibernate EJB application

·         Create Hibernate Config
·         Create Hibernate Map on war project and move into bean project

<?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">
<hibernate-mapping>
   <class name="entities.Customer" table="CUSTOMER">
      <id column="customer_id" name="customerId">
         <generator class="increment"/>
      </id>
      <property column="name" name="name"/>
   </class>
</hibernate-mapping>


·         Create Util Class and add

package util;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

public class HibernateUtil {

   private static final SessionFactory sessionFactory;

   static {
      try {
         sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
      } catch (Throwable ex) {
         System.err.println("Initial SessionFactory creation failed." + ex);
         throw new ExceptionInInitializerError(ex);
      }
   }
   public static SessionFactory getSessionFactory() {
      return sessionFactory;
   }
   public static Session openSession() {
      return sessionFactory.openSession();
   }
   public static Session getCurrentSession() {
      return sessionFactory.getCurrentSession();
   }
   public static void close() {
      if (sessionFactory != null) {
         sessionFactory.close();
      }
   }
}


·         Customer Entity Class

package entities;
import java.io.Serializable;
public class Customer implements Serializable {

   private Integer customerId;
   private String name;
   public Customer() {
   }
   public Customer(Integer customerId) {
      this.customerId = customerId;
   }
   public Integer getCustomerId() {
      return customerId;
   }
   public void setCustomerId(Integer customerId) {
      this.customerId = customerId;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   @Override
   public String toString() {
      // Customer CST001 - Nimal
      return "Customer " + customerId + " - " + name;
   }  
}


·         Customer manager Class

package util;
import java.util.List;
import javax.ejb.Stateless;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
@Stateless
public class CustomerManager {
   public List listCustomer() {
      Transaction tx = null;
      Session session = HibernateUtil.getCurrentSession();
      try {
         tx = session.beginTransaction();
         List customers = session.createQuery("select a from Customer as a").list();
         tx.commit();
         
         return customers;
      } catch (RuntimeException e) {
         if (tx != null && tx.isActive()) {
            try {
               tx.rollback();
            } catch (HibernateException e1) {
               System.out.println("Error rolling back transaction");
            }
            throw e;
         }
      }     
      return null;
   }
}


·         Servlet

package servlets;

import entities.Customer;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.CustomerManager;
public class ListCustomers extends HttpServlet {

   @EJB
   private CustomerManager customerManager;
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      try (PrintWriter out = response.getWriter()) {
         /* TODO output your page here. You may use following sample code. */
         out.println("<!DOCTYPE html>");
         out.println("<html>");
         out.println("<head>");
         out.println("<title>Servlet ListCustomers</title>");
         out.println("</head>");
         out.println("<body>");
         out.println("<h1>List of Customers</h1>");

         List result = customerManager.listCustomer();
         for (Iterator iter = result.iterator(); iter.hasNext();) {
            Customer c = (Customer)iter.next();
            out.println(c.toString() + "<br>");
         }

         out.println("</body>");
         out.println("</html>");
      }

   }

Comments

Popular posts from this blog

Encrypt and Decrypt text using alphabet shifting method

Gridview to Datatable and Viewstate

You think Creating Circuler Graph is a difficult task ...........!!!