1 25 26 package org.objectweb.speedo.tutorial.appli.additional.detach; 27 28 import java.io.IOException ; 29 30 import javax.jdo.FetchPlan; 31 import javax.jdo.PersistenceManager; 32 import javax.jdo.PersistenceManagerFactory; 33 34 import org.objectweb.speedo.tutorial.TutorialHelper; 35 import org.objectweb.speedo.tutorial.pobjects.additional.detach.Address; 36 import org.objectweb.speedo.tutorial.pobjects.additional.detach.Author; 37 import org.objectweb.speedo.tutorial.pobjects.additional.detach.Book; 38 import org.objectweb.speedo.tutorial.pobjects.additional.detach.Editor; 39 40 43 public class TutorialStep6 { 44 45 private static PersistenceManagerFactory pmf = null; 46 47 51 public static void detach() { 52 System.out.println( "***************attach/detach*****************"); 53 PersistenceManager pm = pmf.getPersistenceManager(); 54 FetchPlan fp = pm.getFetchPlan(); 57 fp.addGroup("all"); 58 detachObject(pm); 60 attachObject(pm); 61 pm.close(); 62 } 63 64 67 public static void detachObject(PersistenceManager pm){ 68 Author author = new Author("Henry Miller"); 69 Address address = new Address("Coronation Street", "811XBA", "Manchester"); 70 Editor editor = new Editor("Grasset", address); 71 Book book = new Book("Tropic of Capricorn", author, editor, 1939); 72 73 pm.currentTransaction().begin(); 75 System.out.println( "make persistent the book " + book.toString()); 76 pm.makePersistent(book); 77 pm.currentTransaction().commit(); 78 79 Book copyOfBook = (Book) pm.detachCopy(book); 81 System.out.println("The detached version of the book is as follows:\n " + copyOfBook.toString()); 82 } 83 84 89 public static void attachObject(PersistenceManager pm){ 90 Author author = new Author("Hubert Selby Jr"); 91 Address address = new Address("Sharrow Street", "911ZB2", "Sheffield"); 92 Editor editor = new Editor("Folio", address); 93 Book book = new Book("The willow tree", author, editor, 1999); 94 95 pm.currentTransaction().begin(); 97 System.out.println( "make persistent the book " + book.toString()); 98 pm.makePersistent(book); 99 pm.currentTransaction().commit(); 100 101 Book copyOfBook = (Book) pm.detachCopy(book); 103 System.out.println("The detached version of the book is as follows:\n " + copyOfBook.toString()); 104 105 copyOfBook.setYear(2012); 107 copyOfBook.getEditor().getAddress().setCity("New York"); 108 System.out.println( "Copy of the book " + copyOfBook.toString()); 109 110 pm.currentTransaction().begin(); 112 Book attachedBook = (Book) pm.attachCopy(copyOfBook,false); 113 pm.currentTransaction().commit(); 114 System.out.println("The attached version of the book is as follows:\n " + attachedBook.toString()); 115 } 116 117 public static void main(String [] args){ 118 TutorialHelper th = null; 119 try { 120 th = new TutorialHelper(args[0]); 121 } catch (IOException e) { 122 e.printStackTrace(); 123 System.exit(-1); 124 } 125 TutorialStep6.pmf = th.getPMF(); 126 TutorialStep6.detach(); 127 } 128 129 } 130 | Popular Tags |