📄 productbusinessimpl.java
字号:
package cn.com.tarena.ecport.biz.impl;
import java.util.List;
import org.hibernate.HibernateException;
import cn.com.tarena.ecport.biz.IProductBusiness;
import cn.com.tarena.ecport.common.util.HibernateUtil;
import cn.com.tarena.ecport.dao.IProductDAO;
import cn.com.tarena.ecport.dao.factory.DAOFactory;
import cn.com.tarena.ecport.exception.ECPortException;
import cn.com.tarena.ecport.pojo.Product;
public class ProductBusinessImpl implements IProductBusiness {
public List<Product> findAllProducts() throws ECPortException {
try {
IProductDAO ipd = (IProductDAO) DAOFactory.getDAO(IProductDAO.class.getName());
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
List<Product> products = ipd.findAllProducts();
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction().commit();
return products;
} catch (HibernateException e) {
e.printStackTrace();
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction().rollback();
}
return null;
}
public Product getProductById(Long productid) throws ECPortException {
try {
IProductDAO ipd = (IProductDAO) DAOFactory.getDAO(IProductDAO.class.getName());
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
Product product = ipd.findById(productid);
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction().commit();
return product;
} catch (HibernateException e) {
e.printStackTrace();
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction().rollback();
}
return null;
}
public void addProduct(Product product) throws ECPortException {
try {
IProductDAO ipd = (IProductDAO) DAOFactory.getDAO(IProductDAO.class.getName());
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
ipd.save(product);
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction().rollback();
}
}
public void deleteProduct(Long productId) throws ECPortException {
try {
IProductDAO ipd = (IProductDAO) DAOFactory.getDAO(IProductDAO.class.getName());
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
Product product = ipd.findById(productId);
ipd.delete(product);
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction().rollback();
}
}
public boolean hasProduct(String productName) throws ECPortException {
try {
IProductDAO ipd = (IProductDAO) DAOFactory.getDAO(IProductDAO.class.getName());
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
List<Product> products = ipd.findByProperty("name", productName);
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction().commit();
if (products.size() == 0) {
return false;
} else {
return true;
}
} catch (HibernateException e) {
e.printStackTrace();
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction().rollback();
}
return false;
}
public void updateProduct(Product product) throws ECPortException {
try {
IProductDAO ipd = (IProductDAO) DAOFactory.getDAO(IProductDAO.class.getName());
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
ipd.saveOrUpdate(product);
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction().rollback();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -