Java tutorial
/* * 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 control.dao; import control.dco.SupplierDCO; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import model.entity.Product; import model.entity.Supplier; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.criterion.Restrictions; import prj2.lw.bean.SupplierBean; /** * * @author LapTop */ public class SupplierDAO { SupplierDCO dco = new SupplierDCO(); public List<SupplierBean> getSupplier(String key, int type) { List<Supplier> res = null; Session session = utils.HibernateUtils.getSessionFactory().getCurrentSession(); try { session.beginTransaction(); Criteria crit = session.createCriteria(Supplier.class); switch (type) { case 0: break; case 1: crit.add(Restrictions.ilike("name", "%" + key + "%")); break; case 2: res = new ArrayList<Supplier>(); List<Product> plist = null; Criteria pcrit = session.createCriteria(Product.class); pcrit.add(Restrictions.eq("barcode", key)); plist = pcrit.list(); if (plist == null || plist.isEmpty()) { return null; } else { session.getTransaction().commit(); for (Product product : plist) { res.add(product.getSId()); System.out.println(res.size()); return res.isEmpty() ? null : dco.toSupplierBeanCollect(res); } } break; case 3: crit.add(Restrictions.ilike("code", "%" + key + "%")); break; case 4: crit.add(Restrictions.ilike("email", "%" + key + "%")); break; case 5: crit.add(Restrictions.ilike("adress", "%" + key + "%")); break; case 6: crit.add(Restrictions.ilike("fax", "%" + key + "%")); break; case 7: crit.add(Restrictions.ilike("phone", "%" + key + "%")); break; } res = crit.list(); return res == null ? null : dco.toSupplierBeanCollect(res); } catch (Exception e) { System.out.println("Error at" + this.getClass()); return null; } finally { if (session.getTransaction().isActive()) { session.getTransaction().commit(); } } } public boolean createOrUpdateSupplier(int staffid, SupplierBean bean, boolean iscreate) { Supplier entity = null; entity = dco.toEntity(bean); System.out.println("entity id:\t" + entity.getId()); Session session = utils.HibernateUtils.getSessionFactory().getCurrentSession(); try { session.beginTransaction(); if (iscreate) { session.save(entity); } else { session.update(entity); } return true; } catch (HibernateException e) { session.getTransaction().rollback(); System.out.println("Error at " + this.getClass()); return false; } finally { if (session.getTransaction().isActive()) { session.getTransaction().commit(); } } } }