albumdao.java
来自「《JSP网站开发典型模块与实例精讲》一书光盘源码」· Java 代码 · 共 153 行
JAVA
153 行
package book.example.photoprint.dao;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import book.example.photoprint.exception.DBException;
import book.example.photoprint.po.Album;
import book.example.photoprint.util.HibernateSessionFactory;
public class AlbumDAO implements IAlbumDAO {
public void addAlbum(Album album) throws DBException {
Session session;
Transaction tx = null;
try {
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.saveOrUpdate(album);
tx.commit();
} catch (HibernateException e) {
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
e.printStackTrace();
throw new DBException("保存对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
public void updateAlbum(Album album) throws DBException {
Session session;
Transaction tx = null;
try {
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.update(album);
tx.commit();
} catch (HibernateException e) {
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
e.printStackTrace();
throw new DBException("更新对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
public void deleteAlbum(String albumid) throws DBException {
Session session;
try {
session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
Album album = (Album) session.load(Album.class, albumid);
session.delete(album);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("删除对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("关闭session失败!");
}
}
}
public List list() throws DBException {
Session session;
List list = null;
try {
session = HibernateSessionFactory.currentSession();
list = session.createQuery("from book.example.photoprint.po.Album")
.list();
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("获取对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return list;
}
public List listByUserId(String userid) throws DBException {
Session session;
List list = null;
try {
session = HibernateSessionFactory.currentSession();
list = session
.createQuery(
"from book.example.photoprint.po.Album album where album.userid=?")
.setString(0, userid).list();
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("获取对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return list;
}
public Album getAlbum(String albumid) throws DBException {
Session session;
Album album = null;
try {
session = HibernateSessionFactory.currentSession();
album = (Album) session.get(Album.class, albumid);
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("获取对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return album;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?