KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > dao > ItemDAO


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 JavaDoc;
10
11 /**
12  * A typical DAO for auction items using Hibernate.
13  *
14  * @author Christian Bauer <christian@hibernate.org>
15  */

16 public class ItemDAO {
17
18     public ItemDAO() {
19         HibernateUtil.beginTransaction();
20     }
21
22     // ********************************************************** //
23

24     public Item getItemById(Long JavaDoc 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     // ********************************************************** //
42

43     public Bid getMaxBid(Long JavaDoc itemId)
44             throws InfrastructureException {
45
46         Bid maxBidAmount = null;
47         try {
48             // Note the creative where-clause subselect expression...
49
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     // ********************************************************** //
60

61     public Bid getMinBid(Long JavaDoc itemId)
62             throws InfrastructureException {
63
64         Bid maxBidAmount = null;
65         try {
66             // Note the creative where-clause subselect expression..
67
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     // ********************************************************** //
78

79     public Collection JavaDoc findAll()
80             throws InfrastructureException {
81
82         Collection JavaDoc 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     // ********************************************************** //
92

93     public Collection JavaDoc findByExample(Item exampleItem)
94             throws InfrastructureException {
95
96         Collection JavaDoc 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     // ********************************************************** //
107

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     // ********************************************************** //
119

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