⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 productutil.java

📁 J2EE电子商务系统开发从入门到精通---基于Struts和Hibernate技术实现
💻 JAVA
字号:
/*
 * ProductUtil.java
 *
 * Created on 2006年5月21日, 下午2:28
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package model.store.hibernate;

import dbservice.hibernate.HibernateService;
import java.util.HashSet;
import java.util.List;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.HibernateException;
/**
 *
 * @author Administrator
 */
public class ProductUtil {
    public static byte STATUS_INIT = (byte)0;
    public static byte STATUS_IN = (byte)1;
    public static byte STATUS_OUT = (byte)2;
    
    public static boolean insert(Product product) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            product.setStatus(STATUS_INIT);
            product.setStoreins(new HashSet());
            product.setStoreouts(new HashSet());
            session.save(product);
            transaction.commit();
            b = true;
        }
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            b = false;
        }
        catch (Exception e) {
            e.printStackTrace();
            b = false;
        }
        finally {
            HibernateService.closeSession(session);
            return b;
        }
    }
    
    public static boolean update(String id, Product p) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Product product = new Product();
            session.load(product, id);
            // 只能改变货物的3个属性:name、purpose、remarks
            // 其它属性保持不变
            product.setName(p.getName());
            product.setPurpose(p.getPurpose());
            product.setRemarks(p.getRemarks());
            transaction.commit();
            b = true;
        }
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            b = false;
        }
        catch (Exception e) {
            e.printStackTrace();
            b = false;
        }
        finally {
            HibernateService.closeSession(session);
            return b;
        }
    }
    
    public static List findAll() {
        List list = null;
        Transaction transaction = null;
        Session session = null;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            list = HibernateService.execQuery("from Product");
            transaction.commit();            
        }
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            list = null;
        }
        catch (Exception e) {
            e.printStackTrace();
            list = null;
        }
        finally {
            HibernateService.closeSession(session);
            return list;
        }
    }
    
    public static List findAll(String filter) {
        List list = null;
        Transaction transaction = null;
        Session session = null;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            list = HibernateService.execQuery("from Product where " + filter);
            transaction.commit();
            HibernateService.closeSession(session);
        }
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            list = null;
        }
        catch (Exception e) {
            e.printStackTrace();
            list = null;
        }
        finally {
            HibernateService.closeSession(session);
            return list;
        }
    }
    
    public static Product find(String id) {
        Product product = null;
        Transaction transaction = null;
        Session session = null;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            product = new Product();
            session.load(product, id);
            transaction.commit();
        }
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            product = null;
        }
        catch (Exception e) {
            e.printStackTrace();
            product = null;
        }
        finally {
            HibernateService.closeSession(session);
            return product;
        }
    }
    
    public static boolean delete(String id) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Product product = new Product();
            session.load(product, id);
            product.getStoreins().clear();
            session.delete(product);
            transaction.commit();
            b = true;
        }
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            b = false;
        }
        catch (Exception e) {
            e.printStackTrace();
            b = false;
        }
        finally {
            HibernateService.closeSession(session);
            return b;
        }
    }
    
    public static boolean addStoreouts(String id, Storeout storeout) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Product product = new Product();
            session.load(product, id);            
            product.setStatus(ProductUtil.STATUS_OUT);
            storeout.setProduct(product);
            session.update(product);
            transaction.commit();
            b = true;
        }
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            b = false;
        }
        catch (Exception e) {
            e.printStackTrace();
            b = false;
        }
        finally {
            HibernateService.closeSession(session);
            return b;
        }
    }
    
    public static boolean addStoreins(String id, Storein storein) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Product product = new Product();
            session.load(product, id);
            product.setStatus(ProductUtil.STATUS_IN);
            storein.setProduct(product);
            session.update(product);
            transaction.commit();
            b = true;
        }
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            b = false;
        }
        catch (Exception e) {
            e.printStackTrace();
            b = false;
        }
        finally {
            HibernateService.closeSession(session);
            return b;
        }
    }
    
}

⌨️ 快捷键说明

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