KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > tutorial > appli > basics > TutorialStep2


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.basics;
27
28 import java.io.IOException JavaDoc;
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 /**
38  * @author Y.Bersihand
39  */

40 public class TutorialStep2 {
41     
42     private static PersistenceManagerFactory pmf = null;
43     
44     /**This steps describes how to:
45      * Create a persistent object
46      * Update a persistent object
47      * Delete a persistent object
48      */

49     public static void manageObject() {
50         System.out.println( "***************managingObject*****************");
51         PersistenceManager pm = pmf.getPersistenceManager();
52         //1- make persistent
53
Object JavaDoc id = createAddress(pm);
54         //2- update
55
updateAddress(pm, id);
56         //3- delete
57
deleteAddress(pm, id);
58         pm.close();
59     }
60     
61     /**
62      * Create a persistent object
63      * @return the id of the object created
64      */

65     public static Object JavaDoc createAddress(PersistenceManager pm){
66         //create a country and an address
67
Country uk = new Country("uk", "United Kingdom");
68         Address address = new Address("Sharrow Street", "Sheffield", uk);
69         //make persistent
70
//all the references reachable from this object will be made persistent
71
pm.currentTransaction().begin();
72         System.out.println( "make persistent the address " + address.toString());
73         pm.makePersistent(address);
74         //get the object id
75
Object JavaDoc id = pm.getObjectId(address);
76         pm.currentTransaction().commit();
77         return id;
78     }
79     
80     /**
81      * Update a persistent object
82      */

83     public static void updateAddress(PersistenceManager pm, Object JavaDoc id){
84         pm.currentTransaction().begin();
85         //get the object using its id
86
Address address = (Address) pm.getObjectById(id, true);
87         System.out.println( "update the address " + address.toString());
88         //update
89
address.setCity("New York");
90         pm.currentTransaction().commit();
91     }
92     
93     /**
94      * Delete a persistent object
95      */

96     public static void deleteAddress(PersistenceManager pm, Object JavaDoc id){
97         pm.currentTransaction().begin();
98         //get the object using its id
99
Address address = (Address) pm.getObjectById(id, true);
100         System.out.println( "delete the address " + address.toString());
101         //delete
102
pm.deletePersistent(address);
103         pm.currentTransaction().commit();
104     }
105     
106     public static void main(String JavaDoc[] args){
107         TutorialHelper th = null;
108         try {
109             th = new TutorialHelper(args[0]);
110         } catch (IOException JavaDoc e) {
111             e.printStackTrace();
112             System.exit(-1);
113         }
114         TutorialStep2.pmf = th.getPMF();
115         TutorialStep2.manageObject();
116     }
117
118 }
119
Popular Tags