KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > examples > quickstart > TestServlet


1 package org.hibernate.examples.quickstart;
2
3 import java.io.IOException JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import javax.servlet.ServletException JavaDoc;
8 import javax.servlet.http.HttpServlet JavaDoc;
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11
12 import org.hibernate.HibernateException;
13 import org.hibernate.Query;
14 import org.hibernate.Session;
15 import org.hibernate.SessionFactory;
16 import org.hibernate.Transaction;
17 import org.hibernate.cfg.Configuration;
18
19 /**
20  * Simple Servlet used to show how using Hibernate from a Servlet
21  */

22 public class TestServlet extends HttpServlet JavaDoc {
23
24     /**
25      * Hibernate SessionFactory instance
26      */

27     private SessionFactory sessionFactory;
28
29     /**
30      * Hibernate Session instance
31      */

32     private Session session;
33
34     /**
35      * Hibernate Transaction
36      */

37     private Transaction transaction;
38
39     /**
40      * execute GET action
41      */

42     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
43         throws ServletException JavaDoc, IOException JavaDoc {
44         try {
45             // Initialize Hibernate (Configuration and SessionFactory)
46
initHibernate();
47
48             // Prepare out
49
response.setContentType("text/html");
50
51             PrintWriter JavaDoc out = response.getWriter();
52
53             // Create some Cats
54
beginTransaction();
55             createCats(out);
56             endTransaction(true);
57
58             // Select all Cats
59
beginTransaction();
60             selectAllCats(out);
61             endTransaction(false);
62
63             // Select female Cats
64
beginTransaction();
65             selectFemaleCats(out);
66             endTransaction(false);
67
68             // print "Servlet OK" at the end (everything went OK)
69
out.println("Servlet is OK");
70         } catch (HibernateException e) {
71             throw new ServletException JavaDoc(e);
72         }
73     }
74
75     /**
76      * Creates and Persists Cats in DB
77      * @param out PrintWriter
78      * @throws HibernateException if
79      */

80     public void createCats(PrintWriter JavaDoc out) throws HibernateException {
81         out.print("<h3>Creating Cats:</h3>");
82         out.println("CREATING 'Princess'...<br/>");
83
84         Cat princess = new Cat();
85         princess.setName("Princess");
86         princess.setSex('F');
87         princess.setWeight(7.4f);
88         session.save(princess);
89
90         out.println("CREATING 'Max'...<br/>");
91
92         Cat max = new Cat();
93         max.setName("Max");
94         max.setSex('M');
95         max.setWeight(8.1f);
96         session.save(max);
97
98         out.println("CREATING 'Sophie'...<br/>");
99
100         Cat sophie = new Cat();
101         sophie.setName("Sophie");
102         sophie.setSex('F');
103         sophie.setWeight(4.1f);
104         session.save(sophie);
105     }
106
107     /**
108      * Select All the Cats
109      * @param out PrintWriter
110      * @throws HibernateException
111      */

112     public void selectAllCats(PrintWriter JavaDoc out) throws HibernateException {
113         out.print("<h3>Retrieving all Cats:</h3>");
114
115         String JavaDoc queryString = "select cat from Cat as cat";
116         Query query = session.createQuery(queryString);
117
118         for (Iterator JavaDoc it = query.iterate(); it.hasNext();) {
119             Cat cat = (Cat) it.next();
120             out.println("CAT: " + cat.getName() + " (" + cat.getSex() + ", " +
121                 cat.getWeight() + ")<br/>");
122         }
123     }
124
125     /**
126      * Select all Female Cats
127      * @param out PrintWriter
128      * @throws HibernateException
129      */

130     public void selectFemaleCats(PrintWriter JavaDoc out) throws HibernateException {
131         out.print("<h3>Retrieving female Cats:</h3>");
132
133         String JavaDoc queryString = "select cat from Cat as cat where cat.sex = :sex";
134         Query query = session.createQuery(queryString);
135         query.setCharacter("sex", 'F');
136
137         for (Iterator JavaDoc it = query.iterate(); it.hasNext();) {
138             Cat cat = (Cat) it.next();
139             out.println("CAT: " + cat.getName() + " (" + cat.getSex() + ", " +
140                 cat.getWeight() + ")<br/>");
141         }
142     }
143
144     // Helper Methods
145
private void initHibernate() throws HibernateException {
146         // Load Configuration and build SessionFactory
147
sessionFactory = new Configuration().configure().buildSessionFactory();
148     }
149
150     private void beginTransaction() throws HibernateException {
151         session = sessionFactory.openSession();
152         transaction = session.beginTransaction();
153     }
154
155     private void endTransaction(boolean commit) throws HibernateException {
156         if (commit) {
157             transaction.commit();
158         } else {
159             // Don't commit the transaction, can be faster for read-only operations
160
transaction.rollback();
161         }
162
163         session.close();
164     }
165 }
166
Popular Tags