KickJava   Java API By Example, From Geeks To Geeks.

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


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.User;
7 import org.hibernate.ce.auction.persistence.HibernateUtil;
8
9 import java.util.Collection JavaDoc;
10
11 /**
12  * A typical DAO for users using Hibernate.
13  *
14  * @author Christian Bauer <christian@hibernate.org>
15  */

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

24     public User getUserById(Long JavaDoc userId, boolean lock)
25             throws InfrastructureException {
26
27         Session session = HibernateUtil.getSession();
28         User user = null;
29         try {
30             if (lock) {
31                 user = (User) session.load(User.class, userId, LockMode.UPGRADE);
32             } else {
33                 user = (User) session.load(User.class, userId);
34             }
35         } catch (HibernateException ex) {
36             throw new InfrastructureException(ex);
37         }
38         return user;
39     }
40
41     // ********************************************************** //
42

43     public Collection JavaDoc findAll()
44             throws InfrastructureException {
45
46         Collection JavaDoc users;
47         try {
48             users = HibernateUtil.getSession().createCriteria(User.class).list();
49         } catch (HibernateException ex) {
50             throw new InfrastructureException(ex);
51         }
52         return users;
53     }
54
55     // ********************************************************** //
56

57     public Collection JavaDoc findByExample(User exampleUser)
58             throws InfrastructureException {
59
60         Collection JavaDoc users;
61         try {
62             Criteria crit = HibernateUtil.getSession().createCriteria(User.class);
63             users = crit.add(Example.create(exampleUser)).list();
64         } catch (HibernateException ex) {
65             throw new InfrastructureException(ex);
66         }
67         return users;
68     }
69
70     // ********************************************************** //
71

72     public void makePersistent(User user)
73             throws InfrastructureException {
74
75         try {
76             HibernateUtil.getSession().saveOrUpdate(user);
77         } catch (HibernateException ex) {
78             throw new InfrastructureException(ex);
79         }
80     }
81
82     // ********************************************************** //
83

84     public void makeTransient(User user)
85             throws InfrastructureException {
86
87         try {
88             HibernateUtil.getSession().delete(user);
89         } catch (HibernateException ex) {
90             throw new InfrastructureException(ex);
91         }
92     }
93
94
95 }
96
Popular Tags