📄 messagedaoimpl.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 + -