1 package org.hibernate.ce.auction.dao; 2 3 import org.hibernate.*; 4 import org.hibernate.criterion.Example; 5 import org.hibernate.ce.auction.exceptions.InfrastructureException; 6 import org.hibernate.ce.auction.model.*; 7 import org.hibernate.ce.auction.persistence.HibernateUtil; 8 9 import java.util.Collection ; 10 11 16 public class ItemDAO { 17 18 public ItemDAO() { 19 HibernateUtil.beginTransaction(); 20 } 21 22 24 public Item getItemById(Long itemId, boolean lock) 25 throws InfrastructureException { 26 27 Session session = HibernateUtil.getSession(); 28 Item item = null; 29 try { 30 if (lock) { 31 item = (Item) session.load(Item.class, itemId, LockMode.UPGRADE); 32 } else { 33 item = (Item) session.load(Item.class, itemId); 34 } 35 } catch (HibernateException ex) { 36 throw new InfrastructureException(ex); 37 } 38 return item; 39 } 40 41 43 public Bid getMaxBid(Long itemId) 44 throws InfrastructureException { 45 46 Bid maxBidAmount = null; 47 try { 48 Query q = HibernateUtil.getSession().getNamedQuery("maxBid"); 50 q.setLong("itemid", itemId.longValue()); 51 maxBidAmount = (Bid) q.uniqueResult(); 52 } 53 catch (HibernateException ex) { 54 throw new InfrastructureException(ex); 55 } 56 return maxBidAmount; 57 } 58 59 61 public Bid getMinBid(Long itemId) 62 throws InfrastructureException { 63 64 Bid maxBidAmount = null; 65 try { 66 Query q = HibernateUtil.getSession().getNamedQuery("minBid"); 68 q.setLong("itemid", itemId.longValue()); 69 maxBidAmount = (Bid) q.uniqueResult(); 70 } 71 catch (HibernateException ex) { 72 throw new InfrastructureException(ex); 73 } 74 return maxBidAmount; 75 } 76 77 79 public Collection findAll() 80 throws InfrastructureException { 81 82 Collection items; 83 try { 84 items = HibernateUtil.getSession().createCriteria(Item.class).list(); 85 } catch (HibernateException ex) { 86 throw new InfrastructureException(ex); 87 } 88 return items; 89 } 90 91 93 public Collection findByExample(Item exampleItem) 94 throws InfrastructureException { 95 96 Collection items; 97 try { 98 Criteria crit = HibernateUtil.getSession().createCriteria(Item.class); 99 items = crit.add(Example.create(exampleItem)).list(); 100 } catch (HibernateException ex) { 101 throw new InfrastructureException(ex); 102 } 103 return items; 104 } 105 106 108 public void makePersistent(Item item) 109 throws InfrastructureException { 110 111 try { 112 HibernateUtil.getSession().saveOrUpdate(item); 113 } catch (HibernateException ex) { 114 throw new InfrastructureException(ex); 115 } 116 } 117 118 120 public void makeTransient(Item item) 121 throws InfrastructureException { 122 123 try { 124 HibernateUtil.getSession().delete(item); 125 } catch (HibernateException ex) { 126 throw new InfrastructureException(ex); 127 } 128 } 129 130 } 131 | Popular Tags |