?? 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 + -