Package org.objectweb.speedo.tutorial.appli.additional.detach

Source Code of org.objectweb.speedo.tutorial.appli.additional.detach.TutorialStep6

/**
* 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.additional.detach;

import java.io.IOException;

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

import org.objectweb.speedo.tutorial.TutorialHelper;
import org.objectweb.speedo.tutorial.pobjects.additional.detach.Address;
import org.objectweb.speedo.tutorial.pobjects.additional.detach.Author;
import org.objectweb.speedo.tutorial.pobjects.additional.detach.Book;
import org.objectweb.speedo.tutorial.pobjects.additional.detach.Editor;

/**
* @author Y.Bersihand
*/
public class TutorialStep6 {
 
  private static PersistenceManagerFactory pmf = null;
 
  /**This steps describes how to:
   * detach persistent objects
   * re-attach persistent objects
   */
  public static void detach() {
    System.out.println( "***************attach/detach*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    //ignore the following lines: detailed in the following step : step7
      //get the fetch plan of the pm
      FetchPlan fp = pm.getFetchPlan();
      fp.addGroup("all");
    //stop ignoring
    detachObject(pm);
    attachObject(pm);
        pm.close();
  }
 
  /**
   * detach persistent objects
   */
  public static void detachObject(PersistenceManager pm){
    Author author = new Author("Henry Miller");
    Address address = new Address("Coronation Street", "811XBA", "Manchester");
    Editor editor = new Editor("Grasset", address);
    Book book = new Book("Tropic of Capricorn", author, editor, 1939);
   
    //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
    Book copyOfBook = (Book) pm.detachCopy(book);
    System.out.println("The detached version of the book is as follows:\n " + copyOfBook.toString());   
  }
 
  /**
   * detach persistent object
   * modify it
   * then re-attach it
   */
  public static void attachObject(PersistenceManager pm){
    Author author = new Author("Hubert Selby Jr");
    Address address = new Address("Sharrow Street", "911ZB2", "Sheffield");
    Editor editor = new Editor("Folio", address);
    Book book = new Book("The willow tree", author, editor, 1999);
   
    //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
    Book copyOfBook = (Book) pm.detachCopy(book);
    System.out.println("The detached version of the book is as follows:\n " + copyOfBook.toString());
   
    //modify the book
    copyOfBook.setYear(2012);
    copyOfBook.getEditor().getAddress().setCity("New York");
    System.out.println( "Copy of the book " + copyOfBook.toString());
   
    //attach the book
    pm.currentTransaction().begin();
    Book attachedBook = (Book) pm.makePersistent(copyOfBook);
    pm.currentTransaction().commit();
    System.out.println("The attached version of the book is as follows:\n " + attachedBook.toString());
  }
 
  public static void main(String[] args){
    TutorialHelper th = null;
    try {
      th = new TutorialHelper(args[0]);
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(-1);
    }
    TutorialStep6.pmf = th.getPMF();
    TutorialStep6.detach();
  }

}
TOP

Related Classes of org.objectweb.speedo.tutorial.appli.additional.detach.TutorialStep6

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.