📄 orderdaoimpl.java
字号:
package com.estore.struts.daoimpl;import java.awt.Stroke;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import org.hibernate.Hibernate;import org.hibernate.Session;import org.hibernate.Transaction;import com.estore.struts.Globals;import com.estore.struts.dao.OrderDao;import com.estore.struts.entity.Order;import com.estore.struts.entity.OrderItem;import com.estore.struts.entity.Product;import com.estore.struts.entity.User;import com.estore.struts.service.UserService;import com.estore.struts.utils.HibernateSessionFactory;import com.estore.struts.utils.ServiceFactory;import com.estore.struts.utils.StoreException;public class OrderDaoImpl implements OrderDao { /* * (non-Javadoc) * * @see com.estore.struts.dao.OrderDao#saveOrder(com.estore.struts.entity.Order) * 向数据库中保存一个指定的order对象; */ public void saveOrder(Order order) throws StoreException { Transaction tx = null; Session session = null; try { session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); session.saveOrUpdate(order); tx.commit(); } catch (Exception ex) { tx.rollback(); throw new StoreException(ex); }finally { session.close(); } } /* * (non-Javadoc) * * @see com.estore.struts.dao.OrderDao#getOrdersByUser(com.estore.struts.entity.User) * 根据用户来获得属于用户的所有的order; */ public Collection<Order> getOrdersByUser(User user) throws StoreException { Session session = null; try { session = HibernateSessionFactory.getSession(); Collection<Order> orders = session.createQuery( "from Order o where o.user=:user").setEntity("user", user) .list(); return orders; } catch (Exception ex) { throw new StoreException(ex); } finally { session.close(); } } /* * (non-Javadoc) * * @see com.estore.struts.dao.OrderDao#getOrderById(java.lang.Integer) * 根据order的id号来从数据库中获得一个指定的order; */ public Order getOrderById(Integer id) throws StoreException { Session session = null; try { session = HibernateSessionFactory.getSession(); Order order = (Order) session.get(Order.class, id); Hibernate.initialize(order); Hibernate.initialize(order.getUserItem()); return order; } catch (Exception ex) { throw new StoreException(ex); }finally { session.close(); } } /* * (non-Javadoc) * * @see com.estore.struts.dao.OrderDao#getOrderItems(com.estore.struts.entity.Order) * 根据指定的order对象来获得属于该order的所有orderitem项; */ public Collection<OrderItem> getOrderItems(Order order) throws StoreException { Session session = null; Collection<OrderItem> orderitems = null; try { session = HibernateSessionFactory.getSession(); orderitems = session.createQuery( "from OrderItem oi where oi.order=:order").setEntity( "order", order).list();// // Hibernate.initialize(order.getOrderItems()); return orderitems; } catch (Exception ex) { throw new StoreException(ex); }finally { session.close(); } } /* * (non-Javadoc) * * @see com.estore.struts.dao.OrderDao#deleteOrder(com.estore.struts.entity.Order) * 删除指定的order对象; */ public void deleteOrder(Order order) throws StoreException { Transaction tx = null; Session session = null; try { session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); session.delete(order); tx.commit(); } catch (Exception ex) { tx.rollback(); throw new StoreException(ex); }finally { session.close(); } } public Collection findAllUser() throws StoreException { Transaction tx = null; Session session = null; HashSet hs=new HashSet(); try { session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); List orders = session.createQuery("from Order").list(); for(Object obj:orders){ Order order = (Order)obj;// User user = (User) session.createQuery(// "from User u where u.id = '" + .getUserid() + "'")// .uniqueResult(); hs.add(order.getUser()); } tx.commit(); } catch (Exception ex) { tx.rollback(); throw new StoreException(ex); }finally { session.close(); } return hs; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -