1 package example; 2 3 import java.io.PrintWriter ; 4 import java.io.IOException ; 5 6 import java.util.List ; 7 import java.util.Collection ; 8 import java.util.Iterator ; 9 10 import javax.servlet.http.HttpServlet ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 13 14 import javax.servlet.ServletException ; 15 16 import javax.persistence.*; 17 18 21 public class ManyToManyServlet extends HttpServlet { 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 (1)); 31 } catch (Throwable 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 req, HttpServletResponse res) 85 throws java.io.IOException , ServletException 86 { 87 PrintWriter out = res.getWriter(); 88 89 res.setContentType("text/html"); 90 91 Query allStudent = _entityManager.createQuery("SELECT o FROM Student o"); 92 93 List 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 courses = student.getCourses(); 101 102 out.println("<ul>"); 103 Iterator 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 |