1 package org.hibernate.examples.quickstart; 2 3 import java.io.IOException ; 4 import java.io.PrintWriter ; 5 import java.util.Iterator ; 6 7 import javax.servlet.ServletException ; 8 import javax.servlet.http.HttpServlet ; 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 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 22 public class TestServlet extends HttpServlet { 23 24 27 private SessionFactory sessionFactory; 28 29 32 private Session session; 33 34 37 private Transaction transaction; 38 39 42 public void doGet(HttpServletRequest request, HttpServletResponse response) 43 throws ServletException , IOException { 44 try { 45 initHibernate(); 47 48 response.setContentType("text/html"); 50 51 PrintWriter out = response.getWriter(); 52 53 beginTransaction(); 55 createCats(out); 56 endTransaction(true); 57 58 beginTransaction(); 60 selectAllCats(out); 61 endTransaction(false); 62 63 beginTransaction(); 65 selectFemaleCats(out); 66 endTransaction(false); 67 68 out.println("Servlet is OK"); 70 } catch (HibernateException e) { 71 throw new ServletException (e); 72 } 73 } 74 75 80 public void createCats(PrintWriter 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 112 public void selectAllCats(PrintWriter out) throws HibernateException { 113 out.print("<h3>Retrieving all Cats:</h3>"); 114 115 String queryString = "select cat from Cat as cat"; 116 Query query = session.createQuery(queryString); 117 118 for (Iterator 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 130 public void selectFemaleCats(PrintWriter out) throws HibernateException { 131 out.print("<h3>Retrieving female Cats:</h3>"); 132 133 String 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 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 private void initHibernate() throws HibernateException { 146 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 transaction.rollback(); 161 } 162 163 session.close(); 164 } 165 } 166 | Popular Tags |