1 25 26 package org.objectweb.speedo.tutorial.appli.basics; 27 28 import java.io.IOException ; 29 30 import javax.jdo.PersistenceManager; 31 import javax.jdo.PersistenceManagerFactory; 32 33 import org.objectweb.speedo.tutorial.TutorialHelper; 34 import org.objectweb.speedo.tutorial.pobjects.basics.Address; 35 import org.objectweb.speedo.tutorial.pobjects.basics.Country; 36 37 40 public class TutorialStep2 { 41 42 private static PersistenceManagerFactory pmf = null; 43 44 49 public static void manageObject() { 50 System.out.println( "***************managingObject*****************"); 51 PersistenceManager pm = pmf.getPersistenceManager(); 52 Object id = createAddress(pm); 54 updateAddress(pm, id); 56 deleteAddress(pm, id); 58 pm.close(); 59 } 60 61 65 public static Object createAddress(PersistenceManager pm){ 66 Country uk = new Country("uk", "United Kingdom"); 68 Address address = new Address("Sharrow Street", "Sheffield", uk); 69 pm.currentTransaction().begin(); 72 System.out.println( "make persistent the address " + address.toString()); 73 pm.makePersistent(address); 74 Object id = pm.getObjectId(address); 76 pm.currentTransaction().commit(); 77 return id; 78 } 79 80 83 public static void updateAddress(PersistenceManager pm, Object id){ 84 pm.currentTransaction().begin(); 85 Address address = (Address) pm.getObjectById(id, true); 87 System.out.println( "update the address " + address.toString()); 88 address.setCity("New York"); 90 pm.currentTransaction().commit(); 91 } 92 93 96 public static void deleteAddress(PersistenceManager pm, Object id){ 97 pm.currentTransaction().begin(); 98 Address address = (Address) pm.getObjectById(id, true); 100 System.out.println( "delete the address " + address.toString()); 101 pm.deletePersistent(address); 103 pm.currentTransaction().commit(); 104 } 105 106 public static void main(String [] args){ 107 TutorialHelper th = null; 108 try { 109 th = new TutorialHelper(args[0]); 110 } catch (IOException e) { 111 e.printStackTrace(); 112 System.exit(-1); 113 } 114 TutorialStep2.pmf = th.getPMF(); 115 TutorialStep2.manageObject(); 116 } 117 118 } 119 | Popular Tags |