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

📄 articleutil.java

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

package model.bbs.hibernate;
import dbservice.hibernate.HibernateService;
import java.util.*;
import model.hr.hibernate.*;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
/**
 *
 * @author Administrator
 */
public class ArticleUtil {
    public static boolean insert(String employeeId, String topicId, Article article) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Date date = new Date();
            article.setPresstime(new Date().toString());
            article.setArticleresponses(new HashSet());
            
//            Employee employee = new Employee();
//            Topic topic = new Topic();
//            
//            session.load(employee, employeeId);
//            session.load(topic, Integer.parseInt(topicId));
//            
//            employee.getArticles().add(article);
//            article.setEmployee(employee);
//            
//            topic.getArticles().add(article);
//            article.setTopic(topic);
            EmployeeUtil.addArticles(employeeId, article);
            TopicUtil.addArticles(topicId, article);
            session.save(article);
            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 Article find(String id) {
        Article article = null;
        Transaction transaction = null;
        Session session = null;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            article = new Article();
            session.load(article, Integer.valueOf(id));
            transaction.commit();
        }
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            article = null;
        }
        catch (Exception e) {
            e.printStackTrace();
            article = null;
        }
        finally {
            HibernateService.closeSession(session);
            return article;
        }
    }
    
    public static List findAll() {
        List list = null;
        Transaction transaction = null;
        Session session = null;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            list = HibernateService.execQuery("from Article");
            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 boolean delete(String id) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Article article = new Article();
            session.load(article, Integer.valueOf(id));
            session.delete(article);
            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 addArticleresponses(String id, Articleresponse response) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Article article = new Article();
            session.load(article, Integer.parseInt(id));
            response.setArticle(article);
            session.update(article);
            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 + -