messagedaoimpl.java

来自「java学习的必要的资料,servlet的说明很好」· Java 代码 · 共 106 行

JAVA
106
字号
package com.estore.struts.daoimpl;

import java.util.Collection;
import java.util.LinkedHashSet;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.estore.struts.dao.MessageDao;
import com.estore.struts.entity.Message;
import com.estore.struts.utils.HibernateSessionFactory;
import com.estore.struts.utils.StoreException;

public class MessageDaoImpl implements MessageDao {

	public void addMessage(Message message) throws StoreException {
		Transaction tx = null;
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			session.save(message);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			throw new StoreException(ex);
		}finally {
			session.close();
		}

	}

	public void deleteMessage(Message message) throws StoreException {
		Transaction tx = null;
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			session.delete(message);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			throw new StoreException(ex);
		}finally {
			session.close();
		}

	}

	public Collection findAll() throws StoreException {
		Transaction tx = null;
		Session session = null;
		Collection admins=null;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			admins=session.createQuery("from Message ").list();
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			throw new StoreException(ex);
		}finally {
			session.close();
		}
		
		return new LinkedHashSet(admins);
	}

	public Message findById(Integer id) throws StoreException {
		Transaction tx = null;
		Session session = null;
		Message admin=null;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			admin=(Message)session.createQuery("from Message m where m.messageId=:adminId ").setInteger("adminId", id).uniqueResult();
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			throw new StoreException(ex);
		}finally {
			session.close();
		}
		
		return admin;
	}

	public void modify(Message message) throws StoreException {
		Transaction tx = null;
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			session.saveOrUpdate(message);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			ex.printStackTrace();
		}finally {
			session.close();
		}

	}

}

⌨️ 快捷键说明

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