1 25 26 package org.objectweb.speedo.tutorial.appli.additional.inheritance; 27 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 33 import javax.jdo.Extent; 34 import javax.jdo.PersistenceManager; 35 import javax.jdo.PersistenceManagerFactory; 36 37 import org.objectweb.speedo.tutorial.pobjects.additional.inheritance.Employee; 38 import org.objectweb.speedo.tutorial.pobjects.additional.inheritance.Manager; 39 import org.objectweb.speedo.tutorial.pobjects.additional.inheritance.Worker; 40 import org.objectweb.speedo.tutorial.TutorialHelper; 41 42 45 public class TutorialStep5 { 46 47 private static PersistenceManagerFactory pmf = null; 48 49 53 public static void inheritance() { 54 System.out.println( "***************Inheritance*****************"); 55 PersistenceManager pm = pmf.getPersistenceManager(); 56 createInheritedObjects(pm); 58 iterateExtent(pm, Employee.class); 60 iterateExtent(pm, Worker.class); 62 iterateExtent(pm, Manager.class); 64 pm.close(); 65 } 66 67 70 public static void createInheritedObjects(PersistenceManager pm){ 71 Employee employee1 = new Employee("Herve Landry", 42, "Tetra Pack"); 73 Employee employee2 = new Employee("Vincent Racado", 30, "Tetra Pack"); 74 Worker worker1 = new Worker("Caroline Bret", 33, "Tetra Pack", true); 76 Worker worker2 = new Worker("Evelyne Jain", 54, "Tetra Pack", false); 77 Worker worker3 = new Worker("Tim Jorge", 28, "Tetra Pack", false); 78 Manager manager1 = new Manager("Jean Duverge", 52, "Tetra Pack", "Sales"); 80 Manager manager2 = new Manager("Eric Mento", 49, "Tetra Pack", "Marketing"); 81 82 worker1.addManager(manager1); 83 worker2.addManager(manager1); 84 worker2.addManager(manager2); 85 worker3.addManager(manager2); 86 87 Collection employees = new ArrayList (); 88 employees.add(employee1); 89 employees.add(employee2); 90 employees.add(worker1); 91 employees.add(worker2); 92 employees.add(worker3); 93 employees.add(manager1); 94 employees.add(manager2); 95 96 pm.currentTransaction().begin(); 98 pm.makePersistentAll(employees); 99 pm.currentTransaction().commit(); 100 } 101 102 public static void iterateExtent(PersistenceManager pm, Class cl){ 104 Extent extent = pm.getExtent(cl, true); 105 Iterator it = extent.iterator(); 106 System.out.println( "All " + cl.getName() + " instances:"); 107 while(it.hasNext()){ 108 Employee e = (Employee) it.next(); 109 System.out.println( e.toString()); 110 } 111 extent.close(it); 112 } 113 114 public static void main(String [] args){ 115 TutorialHelper th = null; 116 try { 117 th = new TutorialHelper(args[0]); 118 } catch (IOException e) { 119 e.printStackTrace(); 120 System.exit(-1); 121 } 122 TutorialStep5.pmf = th.getPMF(); 123 TutorialStep5.inheritance(); 124 } 125 126 } 127 | Popular Tags |