productdaoimpl.java

来自「购物车系统」· Java 代码 · 共 103 行

JAVA
103
字号
package cn.com.tarena.ecport.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import cn.com.tarena.ecport.common.util.HibernateUtil;
import cn.com.tarena.ecport.dao.IProductDAO;
import cn.com.tarena.ecport.exception.ECPortException;
import cn.com.tarena.ecport.pojo.Product;

public class ProductDAOImpl implements IProductDAO {
    public void save(Product transientInstance) {
        try {
            HibernateUtil.getSessionFactory().getCurrentSession().save(transientInstance);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
	public void delete(Product persistentInstance) {
        try {
            HibernateUtil.getSessionFactory().getCurrentSession().delete(persistentInstance);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    public Product findById( java.lang.Long id) {
        try {
            Product instance = (Product) HibernateUtil.getSessionFactory().getCurrentSession()
                    .get(Product.class, id);
            return instance;
        } catch (RuntimeException re) {
            
            throw re;
        }
    }
    
    
    public List findByExample(Product instance) {
        try {
            List results = HibernateUtil.getSessionFactory().getCurrentSession()
                    .createCriteria("Product")
                    .add(Example.create(instance))
            .list();
            return results;
        } catch (RuntimeException re) {
            
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      try {
         String queryString = "from Product as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = HibernateUtil.getSessionFactory().getCurrentSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         
         throw re;
      }
	}

	
    public Product merge(Product detachedInstance) {   
        try {
            Product result = (Product) HibernateUtil.getSessionFactory().getCurrentSession()
                    .merge(detachedInstance);
            
            return result;
        } catch (RuntimeException re) {
            
            throw re;
        }
    }

    public void saveOrUpdate(Product instance) {

    }
    
    public void lock(Product instance) {

    }

	public List findAllProducts() throws ECPortException {
		try {
			Query queryObj = HibernateUtil.getSessionFactory().getCurrentSession().createQuery("from Product");
			List list = queryObj.list();
			
			return list;
		} catch (RuntimeException re) {
			
			throw new ECPortException(re);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?