001: package org.hibernate.examples.quickstart;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.Iterator;
006:
007: import javax.servlet.ServletException;
008: import javax.servlet.http.HttpServlet;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011:
012: import org.hibernate.HibernateException;
013: import org.hibernate.Query;
014: import org.hibernate.Session;
015: import org.hibernate.SessionFactory;
016: import org.hibernate.Transaction;
017: import org.hibernate.cfg.Configuration;
018:
019: /**
020: * Simple Servlet used to show how using Hibernate from a Servlet
021: */
022: public class TestServlet extends HttpServlet {
023:
024: /**
025: * Hibernate SessionFactory instance
026: */
027: private SessionFactory sessionFactory;
028:
029: /**
030: * Hibernate Session instance
031: */
032: private Session session;
033:
034: /**
035: * Hibernate Transaction
036: */
037: private Transaction transaction;
038:
039: /**
040: * execute GET action
041: */
042: public void doGet(HttpServletRequest request,
043: HttpServletResponse response) throws ServletException,
044: IOException {
045: try {
046: // Initialize Hibernate (Configuration and SessionFactory)
047: initHibernate();
048:
049: // Prepare out
050: response.setContentType("text/html");
051:
052: PrintWriter out = response.getWriter();
053:
054: // Create some Cats
055: beginTransaction();
056: createCats(out);
057: endTransaction(true);
058:
059: // Select all Cats
060: beginTransaction();
061: selectAllCats(out);
062: endTransaction(false);
063:
064: // Select female Cats
065: beginTransaction();
066: selectFemaleCats(out);
067: endTransaction(false);
068:
069: // print "Servlet OK" at the end (everything went OK)
070: out.println("Servlet is OK");
071: } catch (HibernateException e) {
072: throw new ServletException(e);
073: }
074: }
075:
076: /**
077: * Creates and Persists Cats in DB
078: * @param out PrintWriter
079: * @throws HibernateException if
080: */
081: public void createCats(PrintWriter out) throws HibernateException {
082: out.print("<h3>Creating Cats:</h3>");
083: out.println("CREATING 'Princess'...<br/>");
084:
085: Cat princess = new Cat();
086: princess.setName("Princess");
087: princess.setSex('F');
088: princess.setWeight(7.4f);
089: session.save(princess);
090:
091: out.println("CREATING 'Max'...<br/>");
092:
093: Cat max = new Cat();
094: max.setName("Max");
095: max.setSex('M');
096: max.setWeight(8.1f);
097: session.save(max);
098:
099: out.println("CREATING 'Sophie'...<br/>");
100:
101: Cat sophie = new Cat();
102: sophie.setName("Sophie");
103: sophie.setSex('F');
104: sophie.setWeight(4.1f);
105: session.save(sophie);
106: }
107:
108: /**
109: * Select All the Cats
110: * @param out PrintWriter
111: * @throws HibernateException
112: */
113: public void selectAllCats(PrintWriter out)
114: throws HibernateException {
115: out.print("<h3>Retrieving all Cats:</h3>");
116:
117: String queryString = "select cat from Cat as cat";
118: Query query = session.createQuery(queryString);
119:
120: for (Iterator it = query.iterate(); it.hasNext();) {
121: Cat cat = (Cat) it.next();
122: out.println("CAT: " + cat.getName() + " (" + cat.getSex()
123: + ", " + cat.getWeight() + ")<br/>");
124: }
125: }
126:
127: /**
128: * Select all Female Cats
129: * @param out PrintWriter
130: * @throws HibernateException
131: */
132: public void selectFemaleCats(PrintWriter out)
133: throws HibernateException {
134: out.print("<h3>Retrieving female Cats:</h3>");
135:
136: String queryString = "select cat from Cat as cat where cat.sex = :sex";
137: Query query = session.createQuery(queryString);
138: query.setCharacter("sex", 'F');
139:
140: for (Iterator it = query.iterate(); it.hasNext();) {
141: Cat cat = (Cat) it.next();
142: out.println("CAT: " + cat.getName() + " (" + cat.getSex()
143: + ", " + cat.getWeight() + ")<br/>");
144: }
145: }
146:
147: // Helper Methods
148: private void initHibernate() throws HibernateException {
149: // Load Configuration and build SessionFactory
150: sessionFactory = new Configuration().configure()
151: .buildSessionFactory();
152: }
153:
154: private void beginTransaction() throws HibernateException {
155: session = sessionFactory.openSession();
156: transaction = session.beginTransaction();
157: }
158:
159: private void endTransaction(boolean commit)
160: throws HibernateException {
161: if (commit) {
162: transaction.commit();
163: } else {
164: // Don't commit the transaction, can be faster for read-only operations
165: transaction.rollback();
166: }
167:
168: session.close();
169: }
170: }
|