KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > ManyToManyServlet


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

21 public class ManyToManyServlet extends HttpServlet JavaDoc {
22   @PersistenceContext(name="example")
23   private EntityManager _entityManager;
24
25   public void init()
26   {
27     Student student = null;
28       
29     try {
30       student = _entityManager.find(Student.class, new Long JavaDoc(1));
31     } catch (Throwable JavaDoc e) {
32     }
33
34     if (student == null) {
35       _entityManager.getTransaction().begin();
36
37       try {
38     Student harry = new Student("Harry Potter");
39     _entityManager.persist(harry);
40     
41     Student ron = new Student("Ron Weasley");
42     _entityManager.persist(ron);
43     
44     Student hermione = new Student("Hermione Granger");
45     _entityManager.persist(hermione);
46     
47     Course darkArts = new Course("Defense Against the Dark Arts");
48     _entityManager.persist(darkArts);
49     
50     Course potions = new Course("Potions");
51     _entityManager.persist(potions);
52     
53     Course divination = new Course("Divination");
54     _entityManager.persist(divination);
55     
56     Course arithmancy = new Course("Arithmancy");
57     _entityManager.persist(arithmancy);
58     
59     Course transfiguration = new Course("Transfiguration");
60     _entityManager.persist(transfiguration);
61     
62     Grade grade;
63
64     _entityManager.persist(new Grade(harry, darkArts, "A"));
65     _entityManager.persist(new Grade(harry, potions, "C-"));
66     _entityManager.persist(new Grade(harry, transfiguration, "B+"));
67     _entityManager.persist(new Grade(harry, divination, "B"));
68
69     _entityManager.persist(new Grade(ron, darkArts, "A-"));
70     _entityManager.persist(new Grade(ron, potions, "C+"));
71     _entityManager.persist(new Grade(ron, transfiguration, "B"));
72     _entityManager.persist(new Grade(ron, divination, "B+"));
73
74     _entityManager.persist(new Grade(hermione, darkArts, "A+"));
75     _entityManager.persist(new Grade(hermione, potions, "A-"));
76     _entityManager.persist(new Grade(hermione, transfiguration, "A+"));
77     _entityManager.persist(new Grade(hermione, arithmancy, "A+"));
78       } finally {
79     _entityManager.getTransaction().commit();
80       }
81     }
82   }
83
84   public void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
85     throws java.io.IOException JavaDoc, ServletException JavaDoc
86   {
87     PrintWriter JavaDoc out = res.getWriter();
88
89     res.setContentType("text/html");
90
91     Query allStudent = _entityManager.createQuery("SELECT o FROM Student o");
92     
93     List JavaDoc students = allStudent.getResultList();
94
95     for (int i = 0; i < students.size(); i++) {
96       Student student = (Student) students.get(i);
97
98       out.println("<h3>" + student.getName() + "</h3>");
99
100       Collection JavaDoc courses = student.getCourses();
101
102       out.println("<ul>");
103       Iterator JavaDoc iter = courses.iterator();
104       while (iter.hasNext()) {
105     Course course = (Course) iter.next();
106
107     out.println("<li>" + course.getName());
108       }
109       out.println("</ul>");
110     }
111   }
112 }
113
Popular Tags