1 25 26 package org.objectweb.speedo.tutorial.appli.additional.detach; 27 28 import java.io.IOException ; 29 30 import javax.jdo.PersistenceManager; 31 import javax.jdo.PersistenceManagerFactory; 32 import javax.jdo.FetchPlan; 33 import javax.jdo.JDODetachedFieldAccessException; 34 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 import org.objectweb.speedo.tutorial.TutorialHelper; 40 41 44 public class TutorialStep7 { 45 46 private static PersistenceManagerFactory pmf = null; 47 48 51 public static void fetchPlan() { 52 System.out.println( "***************fetchPlan*****************"); 53 PersistenceManager pm = pmf.getPersistenceManager(); 54 usePredefinedFetchGroups(pm); 55 useDefinedFetchGroups(pm); 56 pm.close(); 57 } 58 59 64 public static void usePredefinedFetchGroups(PersistenceManager pm){ 65 FetchPlan fp = pm.getFetchPlan(); 67 68 Author author = new Author("William S. Burroughs"); 70 Address address = new Address("Fenton Street", "931ZR2", "Leeds"); 71 Editor editor = new Editor("Mille et Une Nuits", address); 72 Book book = new Book("The Yage Letters", author, editor, 1955); 73 74 pm.currentTransaction().begin(); 76 System.out.println( "make persistent the book " + book.toString()); 77 pm.makePersistent(book); 78 pm.currentTransaction().commit(); 79 80 Book defaultBook = (Book) pm.detachCopy(book); 84 System.out.println( "With the default fetchgroup:"); 85 try{ 86 System.out.println( "Title can be accessed: " + defaultBook.getTitle()); 87 System.out.println( "Year can be accessed: " + defaultBook.getYear()); 88 System.out.println( "Author should not be accessed: " + defaultBook.getAuthor().toString()); 89 System.out.println( "Editor should not be accessed: " + defaultBook.getEditor().toString()); 90 } 91 catch(Exception e){ 92 if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("author") != -1) 93 System.out.println( "Correct exception caught: " + e.getMessage()); 94 else 95 System.out.println( "Error: " + e); 96 } 97 98 fp.addGroup("all").removeGroup("default"); 101 Book allBook = (Book) pm.detachCopy(book); 102 System.out.println( "With the all fetchgroup:"); 103 try{ 104 System.out.println( "Title can be accessed: " + allBook.getTitle()); 105 System.out.println( "Year can be accessed: " + allBook.getYear()); 106 System.out.println( "Author can be accessed: " + allBook.getAuthor().toString()); 107 System.out.println( "Editor can be accessed: " + allBook.getEditor().toString()); 108 } 109 catch(Exception e){ 110 System.out.println( "Error: " + e); 111 } 112 } 113 114 117 public static void useDefinedFetchGroups(PersistenceManager pm){ 118 FetchPlan fp = pm.getFetchPlan(); 120 121 Author author = new Author("John Fante"); 123 Address address = new Address("South Street", "211ZL2", "York"); 124 Editor editor = new Editor("Plon", address); 125 Book book = new Book("Bandini", author, editor, 1938); 126 127 pm.currentTransaction().begin(); 129 System.out.println( "make persistent the book " + book.toString()); 130 pm.makePersistent(book); 131 pm.currentTransaction().commit(); 132 fp.addGroup("onlyAuthor").removeGroup("default"); 136 Book onlyAuthorBook = (Book) pm.detachCopy(book); 137 System.out.println( "With the onlyAuthor fetchgroup:"); 138 try{ 139 System.out.println( "Title can be accessed: " + onlyAuthorBook.getTitle()); 140 System.out.println( "Year can be accessed: " + onlyAuthorBook.getYear()); 141 System.out.println( "Author can be accessed: " + onlyAuthorBook.getAuthor().toString()); 142 System.out.println( "Editor should not be accessed: " + onlyAuthorBook.getEditor().toString()); 143 } 144 catch(Exception e){ 145 if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("editor") != -1) 146 System.out.println( "Correct exception caught: " + e.getMessage()); 147 else 148 System.out.println( "Error: " + e); 149 } 150 151 fp.addGroup("editorName").removeGroup("onlyAuthor"); 155 Book editorNameBook = (Book) pm.detachCopy(book); 156 System.out.println( "With the editorName fetchgroup:"); 157 try{ 158 System.out.println( "Title can be accessed: " + editorNameBook.getTitle()); 159 System.out.println( "Year can be accessed: " + editorNameBook.getYear()); 160 System.out.println( "Editor name can be accessed: " + editorNameBook.getEditor().getName()); 161 System.out.println( "Editor address should not be accessed: " + editorNameBook.getEditor().getAddress().toString()); 162 System.out.println( "Author should not be accessed: " + editorNameBook.getAuthor().toString()); 163 } 164 catch(Exception e){ 165 if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("address") != -1) 166 System.out.println( "Correct exception caught: " + e.getMessage()); 167 else 168 System.out.println( "Error: " + e); 169 } 170 } 171 172 public static void main(String [] args){ 173 TutorialHelper th = null; 174 try { 175 th = new TutorialHelper(args[0]); 176 } catch (IOException e) { 177 e.printStackTrace(); 178 System.exit(-1); 179 } 180 TutorialStep7.pmf = th.getPMF(); 181 TutorialStep7.fetchPlan(); 182 } 183 184 } 185 | Popular Tags |