Friday, March 11, 2011

Hibernate and DAO

UNDER CONSTRUCTION


package com.dao;

public interface DAOIntf<T> {
public void update(T obj);
public void save(T obj);
public void delete(T obj);
}





package com.dao;

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

import com.util.HibernateUtil;



public class AbstractDAOImpl<T> implements DAOIntf<T>{

  @Override
  public void update(T obj) {
    save(obj);
  }

  @Override
  public void save(T obj) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
      transaction = session.beginTransaction();
      session.save(obj);
      transaction.commit();
    } catch (HibernateException e) {
      transaction.rollback();
      e.printStackTrace();
    } finally {
      session.close();
    }
  }

  @Override
  public void delete(T obj) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
      transaction = session.beginTransaction();
      session.delete(obj);
      transaction.commit();
    } catch (HibernateException e) {
      transaction.rollback();
      e.printStackTrace();
    } finally {
      session.close();
    }
  
   }

}



ProductDAOIntf.java
package com.dao;

import java.util.List;

public interface ProductDAOIntf<T> extends DAOIntf<T> {
  public T getProductByID(int id);
  public List<T> getAllProducts();
}


ProductDAOImpl.java
package com.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.hbm.Product;
import com.util.HibernateUtil;



public class ProductDAOImpl<T> extends AbstractDAOImpl<T> implements ProductDAOIntf<T> {

  @Override
  public T getProductByID(int id) {
  Session session = HibernateUtil.getSessionFactory().openSession();
    T product=null;
    try {
      product =(T) session.get(Product.class,id);  
    } catch (HibernateException e) {
      e.printStackTrace();
    } finally {
      session.close();
    }
    return product;
  }
 
  @Override
    public List<T> getAllProducts(){
    Session session = HibernateUtil.getSessionFactory().openSession();
    List<T> list = null;
    try {
      list = session.createQuery("from Product").list();
    } catch (HibernateException e) {
      e.printStackTrace();
    } finally {
      session.close();
    }
    return list;
  }

}


Product.java
package com.hbm;

import java.io.Serializable;

public class Product implements Serializable {
 
 
  public Integer getProduct_Id() {
    return Product_Id;
  }

  public void setProduct_Id(Integer product_Id) {
    Product_Id = product_Id;
  }

  public String getProduct_Name() {
    return Product_Name;
  }

  public void setProduct_Name(String product_Name) {
    Product_Name = product_Name;
  }

  public Double getProduct_Price() {
    return Product_Price;
  }

  public void setProduct_Price(Double product_Price) {
    Product_Price = product_Price;
  }

  public Integer getProduct_QOH() {
    return Product_QOH;
  }

  public void setProduct_QOH(Integer product_QOH) {
    Product_QOH = product_QOH;
  }

  Integer Product_Id;
  String Product_Name;
  Double Product_Price;
  Integer Product_QOH;

  @Override
    public String toString() {
      return "Id : " + Product_Id + "\t Name " + Product_Name + "\tQOH " + Product_QOH;
    }
}

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

  <class name="com.hbm.Product" table="product" >
   <id name="Product_Id" type="int"> <generator class="identity"></generator></id>
   <property name="Product_Name" type="string" length="50" />
   <property name="Product_Price" type="double" lazy="true" />
   <property name="Product_QOH" type="int"/>
   
  </class>
</hibernate-mapping>