购物车系统
源代码在线查看: shoppingcartimpl.java
软件大小: |
440 K |
上传用户: |
pc1667pc1667 |
|
|
关键词: |
|
下载地址: |
免注册下载 普通下载
|
|
package com.estore.dao.impl;
import java.util.*;
import org.hibernate.*;
import com.estore.dao.ShoppingCartDao;
import com.estore.entity.*;
import com.estore.util.*;
public class ShoppingCartImpl implements ShoppingCartDao{
public Product findProductById(Integer id){
Session s = null;
Transaction tran = null;
Product product=new Product();
try{
s = HibernateUtil.getSession();
tran =s.beginTransaction();
String hql="from Product where id=?";
product=(Product)s.createQuery(hql).setInteger(0, id).uniqueResult();
tran.commit();
}catch(Exception e){
product=null;
tran.rollback();
throw new RuntimeException(e.getMessage());
}finally {
HibernateUtil.releaseSession(s);
}
return product;
}
public List listProducts() {
List pros = new ArrayList();
Session s = null;
Transaction tran = null;
try {
s = HibernateUtil.getSession();
tran = s.beginTransaction();
String hql = "from Product p";
Query q = s.createQuery(hql);
pros =(List) q.list();
tran.commit();
} catch (Exception e) {
e.printStackTrace();
if (tran != null)
tran.rollback();
}finally {
HibernateUtil.releaseSession(s);
}
return pros;
}
public Order findOrderByOrderId(Integer id){
Session s = null;
Transaction tran =null;
Order order=new Order();
try{
s = HibernateUtil.getSession();
tran =s.beginTransaction();
String hql="from Order where id=?";
order=(Order)s.createQuery(hql).setInteger(0,id).uniqueResult();
tran.commit();
return order;
}catch(Exception e){
tran.rollback();
throw new RuntimeException(e.getMessage());
}finally {
HibernateUtil.releaseSession(s);
}
}
public User findUserByUserId(Integer id){
Session s = null;
Transaction tran = null;
User user=new User();
try{
s = HibernateUtil.getSession();
tran = s.beginTransaction();
String hql="from User where id=?";
user=(User)s.createQuery(hql).setInteger(0,id).uniqueResult();
tran.commit();
return user;
}catch(Exception e){
tran.rollback();
throw new RuntimeException(e.getMessage());
}finally {
HibernateUtil.releaseSession(s);
}
}
public int generateOrder(Order order) {
Session s = null;
Transaction tran=null;
try{
s = HibernateUtil.getSession();
tran=s.beginTransaction();
s.save(order);
tran.commit();
return order.getOid();
}catch(Exception e){
tran.rollback();
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally {
HibernateUtil.releaseSession(s);
}
}
}