Speedo Tutorial - Basics

 FinalLogo.jpg
 FinalLogo.jpg
  1. Object model
  2. Step 1 : make objects persistent
  3. Step 2: update & delete persistent objects

Back to the Speedo tutorial home page




Object model

   The first two steps are based on the following object model:
   
An Address is identified via its street (primary key) and has a reference to
a Country which primary key is its code.

The source code of the java classes is located in the
org.objectweb.speedo.tutorial.pobjects.basics package.

For each jdo application, a jdo file must be defined: the jdo file enables the mapping between the java classes and the persistent objects stored in a database.
The basics.jdo jdo descriptor file of the object model is as follows (much more details in the mapping chapter):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jdo PUBLIC "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN" "jdo.dtd">
<jdo>

<!-- the name of the package -->
<package name="org.objectweb.speedo.pobjects.tutorial.basics">

<!-- the Country class -->
<class name="Country" identity-type="application">
<!-- the name of the table in the database associated with the Country java class -->
<extension vendor-name="speedo" key="sql-name" value="T_BASICS_COUNTRY"/>
<!-- the field code -->
<field name="code" primary-key="true">
<extension vendor-name="speedo" key="sql-name" value="CODE"/>
</field>
<!-- the field name -->
<field name="name">
<extension vendor-name="speedo" key="sql-name" value="NAME"/>
</field>
</class>

<!-- the Address class -->
<class name="Address" identity-type="application">
<!-- the name of the table in the database associated with the Address java class -->
<extension vendor-name="speedo" key="sql-name" value="T_BASICS_ADDRESS"/>
<!-- the field name -->
<field name="street" primary-key="true">
<extension vendor-name="speedo" key="sql-name" value="STREET"/>
</field>
<!-- the field city -->
<field name="city">
<extension vendor-name="speedo" key="sql-name" value="CITY"/>
</field>
<!-- the field country (reference) -->
<field name="country">
<!-- the reference to the country class value="CODE:COUNTRY" where
CODE is the PK of the T_BASICS_COUNTRY table and
COUNTRY is the FK of the T_BASICS_ADDRESS table.-->
<extension vendor-name="speedo" key="target-foreign-keys" value="CODE:COUNTRY"/>
</field>
</class>

</package>
</jdo>


Top
 

Step 1 : make objects persistent

    In the first step, we are going to make java objects become persitent objects. We firstly create a country and 3 addresses as normal java objects. Then the 3 addresses are made persistent by:
    Making persistent the addresses, the country is also made persistent (assuming the Country class has been defined as a persistent class, as it is the case in the basics.jdo file).

//create a country and 3 addresses
Country france = new Country("fr", "France");
Address address1 = new Address("rue de Mons", "Avignon", france);
Address address2 = new Address("impasse St Jacques", "Clermont", france);
Address address3 = new Address("rue Laffiteau", "Paris", france);

//get the persistence manager from the persistence manager factory
PersistenceManager pm = pmf.getPersistenceManager();
//begin a transaction
pm.currentTransaction().begin();
//make persistent the 3 addresses:
// all the references reachable from these objects will be made persistent
System.out.println("make persistent the address1 " + address1.toString());
pm.makePersistent(address1);
System.out.println("make persistent the address2 " + address2.toString());
pm.makePersistent(address2);
System.out.println("make persistent the address3 " + address3.toString());
pm.makePersistent(address3);
//commit the transaction
pm.currentTransaction().commit();
//close the persistence manager
pm.close();
    Note: If you do not use a transaction, the write operation on the datastore is done when the PersistenceManager instance is closed.

    To launch the step 1:
    You can view the results of the makePersistent method in your database via the DatabaseManager (see Tutorial Environment): tables T_BASICS_COUNTRY and T_BASICS_ADDRESS     now contain new rows.


                


   
    Top


Step 2: update and delete persistent objects

    In the step 2, we are going to update and delete objects made persistent.

Make Persistent

    Firstly, we create a country and an address that we make persistent as described in the step 1. In addition, we store the id of the object made persistent. We get this id via     the getObjectId(Object o) method called on the PersistenceManager. After, this id can be useful to retrieve the object calling the getObjectById(Object id) method of the        PersistenceManager.
      

/**
* 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

   Once made persistent, you can update the objects as normal java objects (using the setter methods of a bean for instance). The only condition is to perform this update        inside a transaction.
   In the updateAddress method we:
/**
* 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

    We then decide to delete the object we created and updated before. To perform such deletion, we:
/**
* 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();
}
  To launch the step 2:
    You can view the results of the manageObject method in your database via the DatabaseManager (see Tutorial Environment): tables T_BASICS_COUNTRY and T_BASICS_ADDRESS.

    Top

Step BackFollowing step