ObjectDB - Object Database for Java Home | About Us | Customers | Free Download | Purchase
 
  <Back to Main Menu>   
 
 
JDO Directory Demo
Demo Home

Source Code
directory.Utilities
directory.pc.*
directory.step1.*
directory.step2.*
directory.step3.*
directory.step4.*

J2SDK Instructions
J2SDK on Windows
J2SDK on Unix

IDE Instructions
Borland JBuilder
IBM Eclipse
NetBeans / Forte /
Sun One Studio
JCreator
 



 

ObjectDB for Java/JDO Demo
package directory.step2

     JDO Directory

Step 2 demonstrates using JDO to create, update and delete objects.

directory/step2/eMain.java
 // ObjectDB for Java Demo - JDO Directory - A Brief JDO Tutorial
 // Copyright (C) 2001-2003, ObjectDB Software. All rights reserved.
 
 package directory.step2;
 
 /**
  * A main class for Step 2 of the Directory Demo.
  *
  * This class ensures that all the persistence capable classes will be enhanced
  * at runtime just before they are being loaded into the JVM.
  * To make it work - NO PERSISTENCE CAPABLE CLASS IS MENTIONED IN THIS CLASS!
  * (otherwise non enhanced versions of the classes might be loaded into JVM).
  */
 public class eMain {
     
     public static void main(String[] args) {
         
         // Enhance the persistence capable classes when necessary:
         com.objectdb.Enhancer.enhance("directory.pc.*");
 
         // Now run the example:
         new Update().run();
     }
 }
 

directory/step2/Update.java
 // ObjectDB for Java Demo - JDO Directory - A Brief JDO Tutorial
 // Copyright (C) 2001-2003, ObjectDB Software. All rights reserved.
 
 package directory.step2;
 
 import java.util.*;
 import javax.jdo.*;
 import directory.pc.*;
 
 /**
  * The Update class shows how to update and delete persistent objects, and
  * how to retrieve objects using queries and object IDs.
  */
 public class Update {
     
     /**
      * Runs the example.
      */
     public void run() {
         
         // Obtain a PersistenceManager instance:
         PersistenceManager pm =
             directory.Utilities.getPersistenceManager(false);
         
         // Do some operations:
         try {
             Object oid = addNewBook(pm);
             updateBook(pm, oid);
             deleteBook(pm, oid);
             System.out.println("Operations have been completed...");
         }
         catch (RuntimeException x) {
             System.out.println("Error: " + x.getMessage());
         }
 
         // Close the PersistenceManager instance:
         finally {
             if (pm.currentTransaction().isActive())
                 pm.currentTransaction().rollback();
             if (!pm.isClosed())
                 pm.close();
         }
     }
     
     /**
      * Adds a new book.
      *
      * @return the persistent object ID.
      */
     private static Object addNewBook(PersistenceManager pm) {
 
         // Retrieve the Books category:
         Query query = pm.newQuery(Category.class, "this.name == name");
         query.declareParameters("String name");
         Iterator itr = ((Collection)query.execute("Books")).iterator();
         if (!itr.hasNext())
             throw new RuntimeException("Category 'Books' cannot be found.");
         Category books = (Category)itr.next();
         query.closeAll();
 
         // Add a new book:
         pm.currentTransaction().begin();
         Book newBook = new Book("JDO in a Nutshell", 992,
             "http://www.oreilly.com/catalog/javanut4", "David Flanagan",
             "0596002831", "O'Reilly & Associates", 2002, 3);
         books.add(newBook);
         pm.currentTransaction().commit();
         
         // Return the new object ID:
         return JDOHelper.getObjectId(newBook);
     }
 
     /**
      * Updates the name field in a specified persistent object.
      */
     private static void updateBook(PersistenceManager pm, Object oid) {
         pm.currentTransaction().begin();
         Book book = (Book)pm.getObjectById(oid, true);
         book.setName("Java in a Nutshell");
         pm.currentTransaction().commit();
     }
 
     /**
      * Deletes a specified persistent object.
      */
     private static void deleteBook(PersistenceManager pm, Object oid) {
         pm.currentTransaction().begin();
         Book book = (Book)pm.getObjectById(oid, true);
         pm.deletePersistent(book);
         pm.currentTransaction().commit();
     }
 }
 




Copyright 2001-2007 ObjectDB Software. All rights reserved.