KickJava   Java API By Example, From Geeks To Geeks.

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


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 comments using Hibernate
13  *
14  * @author Christian Bauer <christian@hibernate.org>
15  */

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

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

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

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

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

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