assetutil.java

来自「J2EE电子商务系统开发从入门到精通---基于Struts和Hibernate技」· Java 代码 · 共 195 行

JAVA
195
字号
/*
 * AssetUtil.java
 *
 * Created on 2006年6月10日, 上午10:52
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package model.asset.hibernate;
import dbservice.hibernate.HibernateService;
import java.util.HashSet;
import java.util.List;
import model.hr.hibernate.*;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

/**
 *
 * @author Administrator
 */
public class AssetUtil {
    private static byte STATUS_INIT = 0;
    private static byte STATUS_AUDIT = 1;
    
    public static boolean insert(String typeId, String depId, Asset asset) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            AssettypeUtil.addAssets(typeId, asset);
            DepartmentUtil.addAssets(depId, asset);
            asset.setStatus(STATUS_INIT);
            session.save(asset);
            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, String typeId, String depId, Asset asset) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Asset a = new Asset();
            session.load(a, id);
            a.setName(asset.getName());
            a.setPrice(asset.getPrice());
            a.setRemarks(asset.getRemarks());
            a.setStatus(STATUS_INIT);
            AssettypeUtil.addAssets(typeId, a);
            DepartmentUtil.addAssets(depId, a);
            session.update(a);
            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 audit(String id) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Asset a = new Asset();
            session.load(a, id);
            a.setStatus(STATUS_AUDIT);
            session.update(a);
            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;
        list = HibernateService.execQuery("from Asset");
        return list;
    }
    
    public static Asset find(String id) {
        Asset asset = null;
        Transaction transaction = null;
        Session session = null;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            asset = new Asset();
            session.load(asset, id);
            transaction.commit();
            HibernateService.closeSession(session);
        } 
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            asset = null;
        } 
        catch (Exception e) {
            e.printStackTrace();
            asset = null;
        } 
        finally {
            HibernateService.closeSession(session);
            return asset;
        }
    }
    
    public static List findAll(String column, String keyword) {
        String hql = null;
        if (keyword == null || keyword.equals("")) {
            keyword = "%";
        }
        if (column.equals("id") ) {
            hql = "from Asset asset where asset.id like '%" + keyword + "%'";
        }
        else if (column.equals("name") || column.equals("price")) {
            hql = "from Asset asset where asset." + column + " like '%" + keyword + "%'";
        }
        else if (column.equals("typeId")) {
            hql = "from Asset asset where asset.assettype.name like '%" + keyword + "%'";
        }
        else if (column.equals("departmentId")) {
            hql = "from Asset asset where asset.department.name like '%" + keyword + "%'";
        }
        else {
            hql = "from Asset";
        }
        List list = HibernateService.execQuery(hql);
        return list;
    }
    
    public static boolean delete(String id) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Asset asset = new Asset();
            session.load(asset, id);
            session.delete(asset);
            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 + =
减小字号Ctrl + -
显示快捷键?