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

📄 messagedaoimpl.java

📁 java学习的必要的资料,servlet的说明很好
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -