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 ManyToOneServlet 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 allStudent = _entityManager
082: .createQuery("SELECT o FROM Student o");
083:
084: List students = allStudent.getResultList();
085:
086: for (int i = 0; i < students.size(); i++) {
087: Student student = (Student) students.get(i);
088:
089: out.println(student.getName() + " lives in "
090: + student.getHouse().getName() + "<br>");
091: }
092:
093: String sql = "SELECT s FROM Student s WHERE s.house.name=?1";
094:
095: Query houseStudents = _entityManager.createQuery(sql);
096: houseStudents.setParameter(1, "Gryffindor");
097:
098: students = houseStudents.getResultList();
099:
100: out.println("<h3>Gryffindor Students</h3>");
101:
102: for (int i = 0; i < students.size(); i++) {
103: Student student = (Student) students.get(i);
104:
105: out.println(student.getName() + "<br>");
106: }
107: }
108: }
|