Package org.objectweb.speedo.tutorial.appli.basics

Source Code of org.objectweb.speedo.tutorial.appli.basics.TutorialStep2

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

package org.objectweb.speedo.tutorial.appli.basics;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import org.objectweb.speedo.tutorial.TutorialHelper;
import org.objectweb.speedo.tutorial.pobjects.basics.Address;
import org.objectweb.speedo.tutorial.pobjects.basics.Country;

/**
* @author Y.Bersihand
*/
public class TutorialStep2 {
 
  private static PersistenceManagerFactory pmf = null;
 
  /**This steps describes how to:
   * Create a persistent object
   * Update a persistent object
   * Delete a persistent object
   */
  public static void manageObject() {
    System.out.println( "***************managingObject*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    //1- make persistent
    Object id = createAddress(pm);
        //2- update
    updateAddress(pm, id);
        //3- delete
    deleteAddress(pm, id);
        pm.close();
  }
 
  /**
   * Create a persistent object
   * @return the id of the object created
   */
  public static Object createAddress(PersistenceManager pm){
    //create a country and an address
    Country uk = new Country("uk", "United Kingdom");
    Address address = new Address("Sharrow Street", "Sheffield", uk);
    //make persistent
    //all the references reachable from this object will be made persistent
    pm.currentTransaction().begin();
    System.out.println( "make persistent the address " + address.toString());
        pm.makePersistent(address);
        //get the object id
        Object id = pm.getObjectId(address);
        pm.currentTransaction().commit();
        return id;
  }
 
  /**
   * Update a persistent object
   */
  public static void updateAddress(PersistenceManager pm, Object id){
    pm.currentTransaction().begin();
        //get the object using its id
    Address address = (Address) pm.getObjectById(id, true);
        System.out.println( "update the address " + address.toString());
        //update
        address.setCity("New York");
        pm.currentTransaction().commit();
  }
 
  /**
   * Delete a persistent object
   */
  public static void deleteAddress(PersistenceManager pm, Object id){
    pm.currentTransaction().begin();
        //get the object using its id
    Address address = (Address) pm.getObjectById(id, true);
        System.out.println( "delete the address " + address.toString());
        //delete
        pm.deletePersistent(address);
        pm.currentTransaction().commit();
  }
 
  public static void main(String[] args){
    TutorialHelper th = null;
    try {
      th = new TutorialHelper(args[0]);
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(-1);
    }
    TutorialStep2.pmf = th.getPMF();
    TutorialStep2.manageObject();
  }

}
TOP

Related Classes of org.objectweb.speedo.tutorial.appli.basics.TutorialStep2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.