![]() |
![]() |
Back to the Speedo tutorial home page
![]() |
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>
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:
//create a country and 3 addressesNote: If you do not use a transaction, the write operation on the datastore is done when the PersistenceManager instance is closed.
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();
![]() |
|
![]() |
/**
* 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();
}
/**To launch the step 2:
* 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();
}