001: package example;
002:
003: import java.io.PrintWriter;
004: import java.io.IOException;
005:
006: import java.util.List;
007:
008: import javax.servlet.http.HttpServlet;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011:
012: import javax.servlet.ServletException;
013:
014: import javax.persistence.*;
015:
016: /**
017: * A client to illustrate the query.
018: */
019: public class QueryServlet extends HttpServlet {
020: @PersistenceContext(name="example")
021: private EntityManager _entityManager;
022:
023: public void init() {
024: House house = null;
025:
026: try {
027: house = _entityManager.find(House.class, new Long(1));
028: } catch (Throwable e) {
029: }
030:
031: if (house == null) {
032: _entityManager.getTransaction().begin();
033:
034: try {
035: House gryffindor = new House("Gryffindor");
036: _entityManager.persist(gryffindor);
037:
038: House slytherin = new House("Slytherin");
039: _entityManager.persist(slytherin);
040:
041: House ravenclaw = new House("Ravenclaw");
042: _entityManager.persist(ravenclaw);
043:
044: House hufflepuff = new House("Hufflepuff");
045: _entityManager.persist(hufflepuff);
046:
047: Student student;
048:
049: student = new Student("Harry Potter", "M", gryffindor);
050: _entityManager.persist(student);
051:
052: student = new Student("Ron Weasley", "M", gryffindor);
053: _entityManager.persist(student);
054:
055: student = new Student("Hermione Granger", "F",
056: gryffindor);
057: _entityManager.persist(student);
058:
059: student = new Student("Draco Malfoy", "M", slytherin);
060: _entityManager.persist(student);
061:
062: student = new Student("Millicent Bulstrode", "F",
063: slytherin);
064: _entityManager.persist(student);
065:
066: student = new Student("Penelope Clearwater", "F",
067: ravenclaw);
068: _entityManager.persist(student);
069: } finally {
070: _entityManager.getTransaction().commit();
071: }
072: }
073: }
074:
075: public void service(HttpServletRequest req, HttpServletResponse res)
076: throws java.io.IOException, ServletException {
077: PrintWriter out = res.getWriter();
078:
079: res.setContentType("text/html");
080:
081: Query allHouse = _entityManager
082: .createQuery("SELECT o FROM House o");
083:
084: String sql = ("SELECT s" + " FROM House h, IN(h.studentList) s"
085: + " WHERE h.id=?1 AND s.gender='M'");
086: Query boysInHouse = _entityManager.createQuery(sql);
087:
088: List houses = allHouse.getResultList();
089:
090: for (int i = 0; i < houses.size(); i++) {
091: House house = (House) houses.get(i);
092:
093: out.println("<H3>Boys living in " + house.getName()
094: + ":</H3>");
095:
096: boysInHouse.setParameter(1, new Long(house.getId()));
097: List boys = boysInHouse.getResultList();
098:
099: if (boys.size() == 0)
100: out.println("No boys are living in " + house.getName());
101:
102: for (int j = 0; j < boys.size(); j++) {
103: Student boy = (Student) boys.get(j);
104:
105: out.println(boy.getName() + "<br>");
106: }
107: }
108: }
109: }
|