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

📄 historyutil.java

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

package model.library.hibernate;
import dbservice.hibernate.HibernateService;
import java.util.List;
import java.util.ListIterator;
import model.hr.hibernate.*;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
/**
 *
 * @author Administrator
 */
public class HistoryUtil {
    public static boolean insert(String documentId, String employeeId) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            deleteSameHistory(documentId, employeeId);
            int row = HibernateService.getRows("select count(*) from History history where history.employee.id=" + employeeId);
            if (row == 8) {
                deleteAll(employeeId);
            }
            History history = new History();
            Document doc = new Document();
            Employee emp = new Employee();
            session.load(doc, Integer.valueOf(documentId));
            session.load(emp, employeeId);
            
            history.setEmployee(emp);
            history.setDocument(doc);
            
            session.saveOrUpdate(history);
            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;
        }
    }
    
    private static void deleteSameHistory(String documentId, String employeeId) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            session.delete("from History history where history.employee.id=" + employeeId + 
                    " and history.document.id=" + documentId);
            transaction.commit();
        }
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            HibernateService.closeSession(session);
        }
    }
    
    private static void deleteAll( String employeeId) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            session.delete("from History history where history.employee.id=" + employeeId);
            transaction.commit();
        }
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            HibernateService.closeSession(session);
        }        
    }
    
    public static List findAll(String employeeId) {
        List list = null;
        list = HibernateService.execQuery("from History history where history.employee.id=" + employeeId);
        return list;        
    }
}

⌨️ 快捷键说明

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