dao.ProductCategoryDAO.java Source code

Java tutorial

Introduction

Here is the source code for dao.ProductCategoryDAO.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 dao;

import java.util.List;
import javax.ejb.Stateless;
import model.ProductCategory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import utils.HibernateUtil;

/**
 *
 * @author yyun
 */
@Stateless
public class ProductCategoryDAO implements IProductCategoryDAO {

    private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    private Session session;

    @Override
    public void addProductCategory(ProductCategory productCategory) {
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds
            tx.setTimeout(3);
            tx.begin();

            sess.save(productCategory);

            sess.getTransaction().commit();
        } catch (Exception e) {
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }

    }

    @Override
    public void updateProductCategory(ProductCategory productCategory) {
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds
            tx.setTimeout(3);
            tx.begin();

            //            System.out.println("Hello " + productCategory.getIdProductCategory());

            ProductCategory c = (ProductCategory) sess.get(ProductCategory.class,
                    productCategory.getIdProductCategory());
            c.setDescription(productCategory.getDescription());
            c.setName(productCategory.getName());
            c.setParentCategory(productCategory.getParentCategory());

            sess.getTransaction().commit();
        } catch (Exception e) {
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }

    }

    @Override
    public void deleteProductCategory(Integer IdProductCategory) {
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds
            tx.setTimeout(3);
            tx.begin();
            ProductCategory c = (ProductCategory) sess.get(ProductCategory.class, IdProductCategory);
            sess.delete(c);

            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }

    }

    @Override
    public ProductCategory getProductCategory(Integer IdProductCategory) {
        ProductCategory cs;
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds
            tx.setTimeout(3);
            tx.begin();
            cs = (ProductCategory) sess.get(ProductCategory.class, IdProductCategory);

            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }

        return cs;
    }

    @Override
    public List findAll() {
        List<ProductCategory> cs;
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds

            tx.setTimeout(3);
            tx.begin();
            Query queryResult = sess.createQuery("from ProductCategory");
            cs = queryResult.list();

            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }
        return cs;
    }
}