photodao.java

来自「《JSP网站开发典型模块与实例精讲》一书光盘源码」· Java 代码 · 共 88 行

JAVA
88
字号
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.Photo;
import book.example.photoprint.util.HibernateSessionFactory;

public class PhotoDAO implements IPhotoDAO {

	public List listByOrderId(String id) throws DBException {
		Session session;
		List list = null;
		try {
			session = HibernateSessionFactory.currentSession();
			list = session
					.createQuery(
							"from book.example.photoprint.po.Photo photo where photo.orderid=:id")
					.setString("id", id).list();

		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DBException("获取对象失败!");
		} finally {
			try {
				HibernateSessionFactory.closeSession();
			} catch (HibernateException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public List listByAlbumId(String id) throws DBException{
		Session session;
		List list = null;
		try {
			session = HibernateSessionFactory.currentSession();
			list = session
					.createQuery(
							"from book.example.photoprint.po.Photo photo where photo.albumid=:id")
					.setString("id", id).list();

		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DBException("获取对象失败!");
		} finally {
			try {
				HibernateSessionFactory.closeSession();
			} catch (HibernateException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	public void addPhoto(Photo photo) throws DBException {
		Session session;
		Transaction tx = null;
		try {
			session = HibernateSessionFactory.currentSession();
			tx = session.beginTransaction();
			session.save(photo);
			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();
			}
		}
	}
}

⌨️ 快捷键说明

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