com.hp.dao.StockDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.hp.dao.StockDAOImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.hp.dao;

import com.googlecode.s2hibernate.struts2.plugin.util.HibernateSessionFactory;
import static com.googlecode.s2hibernate.struts2.plugin.util.HibernateSessionFactory.getSessionFactory;
import com.hp.domain.Stock;
import com.hp.domain.TakeOrder;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author HP
 */
public class StockDAOImpl implements StockDAO {
    public boolean saveOrUpdate(Stock pStock) {
        Session session = HibernateSessionFactory.getSessionFactory().openSession();
        Transaction transaction;

        transaction = session.beginTransaction();
        try {
            //            if(getStock(pStock.getmStockID()) == null)
            //                session.save(pStock);
            //            else
            session.saveOrUpdate(pStock);
            //session.flush();
            session.getTransaction().commit();
        } catch (HibernateException e) {
            transaction.rollback();
            return false;
        } finally {
            session.close();
        }

        return true;
    }

    public Stock getStock(int pStock) {
        Session session = getSessionFactory().openSession();
        Transaction transaction;
        transaction = session.beginTransaction();

        Stock courses = null;
        try {
            courses = (Stock) session.get(TakeOrder.class, pStock);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

        return courses;
    }

    public Stock getStock(String pID) {
        Session session = getSessionFactory().openSession();
        Transaction transaction;
        transaction = session.beginTransaction();

        Stock courses = null;
        try {
            //Query q = session.createQuery("from TakeOrder where mID='"+pID+"'");
            Query q = session.createSQLQuery("select * from tb_kho where kho_ma_san_pham='" + pID + "'")
                    .addEntity(Stock.class);
            if (q.list().size() == 0)
                return null;

            courses = (Stock) q.list().get(0);
            //courses = (TakeOrder)session.get(TakeOrder.class, pID);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

        return courses;
    }

    @Override
    public boolean update(Stock pStock) {
        Session session = getSessionFactory().openSession();
        Transaction transaction;
        transaction = session.beginTransaction();
        try {

            session.update(pStock);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            transaction.rollback();
            return false;
        } finally {
            session.close();
        }

        return true;
    }

    @Override
    public boolean delete(Stock pStock) {
        Session session = getSessionFactory().openSession();
        Transaction transaction;
        transaction = session.beginTransaction();
        try {

            session.delete(pStock);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            transaction.rollback();
            return false;
        } finally {
            session.close();
        }

        return true;
    }
}