assettypeutil.java

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

JAVA
170
字号
/*
 * AssettypeUtil.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 net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

/**
 *
 * @author Administrator
 */
public class AssettypeUtil {
    public static boolean insert(Assettype type) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            type.setAssets(new HashSet());
            session.save(type);
            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, Assettype type) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Assettype at = new Assettype();
            session.load(at, Byte.valueOf(id));
            at.setRemarks(type.getRemarks());
            at.setName(type.getName());
            session.update(at);
            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 Assettype");
        return list;
    }
    
    public static Assettype find(String id) {
        Assettype type = null;
        Transaction transaction = null;
        Session session = null;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            type = new Assettype();
            session.load(type, Byte.valueOf(id));
            transaction.commit();
        } 
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            type = null;
        } 
        catch (Exception e) {
            e.printStackTrace();
            type = null;
        }
        finally {
            HibernateService.closeSession(session);
            return type;
        }
    }
    
    public static boolean delete(String id) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Assettype type = new Assettype();
            session.load(type, Byte.valueOf(id));
            type.getAssets().clear();
            session.delete(type);
            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 addAssets(String id, Asset asset) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Assettype type = new Assettype();
            session.load(type, Byte.valueOf(id));
            asset.setAssettype(type);
            session.update(type);
            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 + -
显示快捷键?