/**
* Detach objects with user-defined fetchgroups (see the detach.jdo file)
*/
public static void useDefinedFetchGroups(PersistenceManager pm){
//create a fetch plan
FetchPlan fp = pm.getFetchPlan();
//create a book
Author author = new Author("John Fante");
Address address = new Address("South Street", "211ZL2", "York");
Editor editor = new Editor("Plon", address);
Book book = new Book("Bandini", author, editor, 1938);
//make the book persistent
pm.currentTransaction().begin();
System.out.println( "make persistent the book " + book.toString());
pm.makePersistent(book);
pm.currentTransaction().commit();
//detach the book --> "onlyAuthor" fetch group
//fields title year and author are loaded
// fields editor is not loaded
fp.addGroup("onlyAuthor").removeGroup("default");
Book onlyAuthorBook = (Book) pm.detachCopy(book);
System.out.println( "With the onlyAuthor fetchgroup:");
try{
System.out.println( "Title can be accessed: " + onlyAuthorBook.getTitle());
System.out.println( "Year can be accessed: " + onlyAuthorBook.getYear());
System.out.println( "Author can be accessed: " + onlyAuthorBook.getAuthor().toString());
System.out.println( "Editor should not be accessed: " + onlyAuthorBook.getEditor().toString());
}
catch(Exception e){
if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("editor") != -1)
System.out.println( "Correct exception caught: " + e.getMessage());
else
System.out.println( "Error: " + e);
}
//detach the book --> "editorName" fetch group
//fields title, year, editor.name are loaded
//fields author, editor.address are not loaded
fp.addGroup("editorName").removeGroup("onlyAuthor");
Book editorNameBook = (Book) pm.detachCopy(book);
System.out.println( "With the editorName fetchgroup:");
try{
System.out.println( "Title can be accessed: " + editorNameBook.getTitle());
System.out.println( "Year can be accessed: " + editorNameBook.getYear());