001: package example;
002:
003: import java.io.PrintWriter;
004: import java.io.IOException;
005:
006: import java.util.List;
007: import java.util.Collection;
008: import java.util.Iterator;
009:
010: import javax.servlet.http.HttpServlet;
011: import javax.servlet.http.HttpServletRequest;
012: import javax.servlet.http.HttpServletResponse;
013:
014: import javax.servlet.ServletException;
015:
016: import javax.persistence.*;
017:
018: /**
019: * A client to illustrate the many-to-many relation.
020: */
021: public class ManyToManyServlet extends HttpServlet {
022: @PersistenceContext(name="example")
023: private EntityManager _entityManager;
024:
025: public void init() {
026: Student student = null;
027:
028: try {
029: student = _entityManager.find(Student.class, new Long(1));
030: } catch (Throwable e) {
031: }
032:
033: if (student == null) {
034: _entityManager.getTransaction().begin();
035:
036: try {
037: Student harry = new Student("Harry Potter");
038: _entityManager.persist(harry);
039:
040: Student ron = new Student("Ron Weasley");
041: _entityManager.persist(ron);
042:
043: Student hermione = new Student("Hermione Granger");
044: _entityManager.persist(hermione);
045:
046: Course darkArts = new Course(
047: "Defense Against the Dark Arts");
048: _entityManager.persist(darkArts);
049:
050: Course potions = new Course("Potions");
051: _entityManager.persist(potions);
052:
053: Course divination = new Course("Divination");
054: _entityManager.persist(divination);
055:
056: Course arithmancy = new Course("Arithmancy");
057: _entityManager.persist(arithmancy);
058:
059: Course transfiguration = new Course("Transfiguration");
060: _entityManager.persist(transfiguration);
061:
062: Grade grade;
063:
064: _entityManager.persist(new Grade(harry, darkArts, "A"));
065: _entityManager.persist(new Grade(harry, potions, "C-"));
066: _entityManager.persist(new Grade(harry,
067: transfiguration, "B+"));
068: _entityManager
069: .persist(new Grade(harry, divination, "B"));
070:
071: _entityManager.persist(new Grade(ron, darkArts, "A-"));
072: _entityManager.persist(new Grade(ron, potions, "C+"));
073: _entityManager.persist(new Grade(ron, transfiguration,
074: "B"));
075: _entityManager
076: .persist(new Grade(ron, divination, "B+"));
077:
078: _entityManager.persist(new Grade(hermione, darkArts,
079: "A+"));
080: _entityManager.persist(new Grade(hermione, potions,
081: "A-"));
082: _entityManager.persist(new Grade(hermione,
083: transfiguration, "A+"));
084: _entityManager.persist(new Grade(hermione, arithmancy,
085: "A+"));
086: } finally {
087: _entityManager.getTransaction().commit();
088: }
089: }
090: }
091:
092: public void service(HttpServletRequest req, HttpServletResponse res)
093: throws java.io.IOException, ServletException {
094: PrintWriter out = res.getWriter();
095:
096: res.setContentType("text/html");
097:
098: try {
099: _entityManager.getTransaction().begin();
100:
101: Query allStudent = _entityManager
102: .createQuery("SELECT o FROM Student o");
103:
104: List students = allStudent.getResultList();
105:
106: for (int i = 0; i < students.size(); i++) {
107: Student student = (Student) students.get(i);
108:
109: out.println("<h3>" + student.getName() + "</h3>");
110:
111: Collection courses = student.getCourses();
112:
113: out.println("<ul>");
114: Iterator iter = courses.iterator();
115: while (iter.hasNext()) {
116: Course course = (Course) iter.next();
117:
118: out.println("<li>" + course.getName());
119: }
120: out.println("</ul>");
121: }
122: } finally {
123: _entityManager.getTransaction().commit();
124: }
125: }
126: }
|