Package dao_impl

Source Code of dao_impl.PriceDAOImpl

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao_impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jboss.logging.Logger;

import dao.PriceDAO;
import dao.UserDAO;
import pojo.Product;
import pojo.Price;
import pojo.User;
import util.HibernateUtil;

/**
*
* @author phuc
*/
public class PriceDAOImpl implements PriceDAO {

  private static final Logger logger = Logger.getLogger(PriceDAOImpl.class);

  // get all range price from databae
  public List<Price> getListPrice() {
    logger.debug("getListPrice start");
    List<Price> ds = null;

    Session session = HibernateUtil.getSessionFactory().openSession();

    try {
      String hql = "from Price";
      Query query = session.createQuery(hql);
      ds = query.list();
      logger.debug("getListPrice success");
    } catch (HibernateException ex) {
      System.err.println(ex);
      logger.error("getListPrice error : " + ex);
    } finally {
      session.close();
    }
    logger.debug("getListPrice end");
    return ds;
  }

  // get infor of a price by id
  public Price getInfoPrice(int id) {
    logger.debug("getInfoPrice start");
    Price sp = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
      sp = (Price) session.get(Price.class, id);
      logger.debug("getInfoPrice success");
    } catch (HibernateException ex) {
      System.err.println(ex);
      logger.error("getInfoPrice error : " + ex);
    } finally {
      session.close();
    }
    logger.debug("getInfoPrice end");
    return sp;
  }

  // add price to database
  public boolean addPrice(Price sp) {
    logger.debug("addPrice start");
    Session session = HibernateUtil.getSessionFactory().openSession();

    Transaction transaction = null;
    try {
      transaction = session.beginTransaction();
      session.save(sp);
      transaction.commit();
      logger.debug("addPrice success");
    } catch (HibernateException ex) {
      transaction.rollback();
      logger.error("addPrice error : " + ex);
    } finally {
      session.close();
    }
    logger.debug("addPrice end");
    return true;
  }

  // del a price by id
  public boolean delPrice(int id) {
    logger.debug("delPrice start");
    Session session = HibernateUtil.getSessionFactory().openSession();
    Price u = (Price) session.get(Price.class, id);

    Transaction transaction = null;
    try {
      transaction = session.beginTransaction();
      session.delete(u);
      transaction.commit();
      logger.debug("delPrice success");
    } catch (HibernateException ex) {
      transaction.rollback();
      System.err.println(ex);
      logger.error("delPrice error : " + ex);
    } finally {
      session.close();
    }
    logger.debug("delPrice end");

    return true;
  }
}
TOP

Related Classes of dao_impl.PriceDAOImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.