KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > QueryServlet


1 package example;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.io.IOException JavaDoc;
5
6 import java.util.List JavaDoc;
7
8 import javax.servlet.http.HttpServlet JavaDoc;
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11
12 import javax.servlet.ServletException JavaDoc;
13
14 import javax.persistence.*;
15
16 /**
17  * A client to illustrate the query.
18  */

19 public class QueryServlet extends HttpServlet JavaDoc {
20   @PersistenceContext(name="example")
21   private EntityManager _entityManager;
22
23   public void init()
24   {
25     House house = null;
26       
27     try {
28       house = _entityManager.find(House.class, new Long JavaDoc(1));
29     } catch (Throwable JavaDoc e) {
30     }
31
32     if (house == null) {
33       _entityManager.getTransaction().begin();
34       
35       try {
36     House gryffindor = new House("Gryffindor");
37     _entityManager.persist(gryffindor);
38     
39     House slytherin = new House("Slytherin");
40     _entityManager.persist(slytherin);
41     
42     House ravenclaw = new House("Ravenclaw");
43     _entityManager.persist(ravenclaw);
44     
45     House hufflepuff = new House("Hufflepuff");
46     _entityManager.persist(hufflepuff);
47
48     Student student;
49
50     student = new Student("Harry Potter", "M", gryffindor);
51     _entityManager.persist(student);
52
53     student = new Student("Ron Weasley", "M", gryffindor);
54     _entityManager.persist(student);
55
56     student = new Student("Hermione Granger", "F", gryffindor);
57     _entityManager.persist(student);
58
59     student = new Student("Draco Malfoy", "M", slytherin);
60     _entityManager.persist(student);
61
62     student = new Student("Millicent Bulstrode", "F", slytherin);
63     _entityManager.persist(student);
64
65     student = new Student("Penelope Clearwater", "F", ravenclaw);
66     _entityManager.persist(student);
67       } finally {
68     _entityManager.getTransaction().commit();
69       }
70     }
71   }
72
73   public void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
74     throws java.io.IOException JavaDoc, ServletException JavaDoc
75   {
76     PrintWriter JavaDoc out = res.getWriter();
77
78     res.setContentType("text/html");
79
80     Query allHouse = _entityManager.createQuery("SELECT o FROM House o");
81     
82     String JavaDoc sql = ("SELECT s" +
83           " FROM House h, IN(h.studentList) s" +
84           " WHERE h.id=?1 AND s.gender='M'");
85     Query boysInHouse = _entityManager.createQuery(sql);
86     
87     List JavaDoc houses = allHouse.getResultList();
88
89     for (int i = 0; i < houses.size(); i++) {
90       House house = (House) houses.get(i);
91       
92       out.println("<H3>Boys living in " + house.getName() + ":</H3>");
93
94       boysInHouse.setParameter(1, new Long JavaDoc(house.getId()));
95       List JavaDoc boys = boysInHouse.getResultList();
96
97       if (boys.size() == 0)
98     out.println("No boys are living in " + house.getName());
99
100       for (int j = 0; j < boys.size(); j++) {
101     Student boy = (Student) boys.get(j);
102
103     out.println(boy.getName() + "<br>");
104       }
105     }
106   }
107 }
108
Popular Tags