KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > tutorial > appli > additional > detach > TutorialStep6


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  */

25
26 package org.objectweb.speedo.tutorial.appli.additional.detach;
27
28 import java.io.IOException JavaDoc;
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 /**
41  * @author Y.Bersihand
42  */

43 public class TutorialStep6 {
44     
45     private static PersistenceManagerFactory pmf = null;
46     
47     /**This steps describes how to:
48      * detach persistent objects
49      * re-attach persistent objects
50      */

51     public static void detach() {
52         System.out.println( "***************attach/detach*****************");
53         PersistenceManager pm = pmf.getPersistenceManager();
54         //ignore the following lines: detailed in the following step : step7
55
//get the fetch plan of the pm
56
FetchPlan fp = pm.getFetchPlan();
57             fp.addGroup("all");
58         //stop ignoring
59
detachObject(pm);
60         attachObject(pm);
61         pm.close();
62     }
63     
64     /**
65      * detach persistent objects
66      */

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         //make the book persistent
74
pm.currentTransaction().begin();
75         System.out.println( "make persistent the book " + book.toString());
76         pm.makePersistent(book);
77         pm.currentTransaction().commit();
78         
79         //detach the book
80
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     /**
85      * detach persistent object
86      * modify it
87      * then re-attach it
88      */

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         //make the book persistent
96
pm.currentTransaction().begin();
97         System.out.println( "make persistent the book " + book.toString());
98         pm.makePersistent(book);
99         pm.currentTransaction().commit();
100         
101         //detach the book
102
Book copyOfBook = (Book) pm.detachCopy(book);
103         System.out.println("The detached version of the book is as follows:\n " + copyOfBook.toString());
104         
105         //modify the book
106
copyOfBook.setYear(2012);
107         copyOfBook.getEditor().getAddress().setCity("New York");
108         System.out.println( "Copy of the book " + copyOfBook.toString());
109         
110         //attach the book
111
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 JavaDoc[] args){
118         TutorialHelper th = null;
119         try {
120             th = new TutorialHelper(args[0]);
121         } catch (IOException JavaDoc e) {
122             e.printStackTrace();
123             System.exit(-1);
124         }
125         TutorialStep6.pmf = th.getPMF();
126         TutorialStep6.detach();
127     }
128
129 }
130
Popular Tags