pricedao.java

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

JAVA
136
字号
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.Price;
import book.example.photoprint.po.Shop;
import book.example.photoprint.util.HibernateSessionFactory;

public class PriceDAO implements IPriceDAO {
	public void addPrice(Price price) throws DBException {
		Session session;
		Transaction tx = null;
		try {
			session = HibernateSessionFactory.currentSession();
			tx = session.beginTransaction();
			session.save(price);
			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 updatePrice(Price price) throws DBException {
		Session session;
		Transaction tx = null;
		try {
			session = HibernateSessionFactory.currentSession();
			tx = session.beginTransaction();
			session.update(price);
			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 deletePrice(String priceid) throws DBException {
		Session session;
		try {
			session = HibernateSessionFactory.currentSession();

			Transaction tx = session.beginTransaction();
			Price price = (Price) session.load(Price.class, priceid);
			session.delete(price);
			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.Price")
					.list();

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

	public List listByShopId(Shop shop) throws DBException {
		Session session;
		List list = null;
		try {
			session = HibernateSessionFactory.currentSession();
			list = session
					.createQuery(
							"from book.example.photoprint.po.Price price where price.shop=?")
					.setEntity(0, shop).list();

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

⌨️ 快捷键说明

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