DataNucleus - Tutorial for JDO, persisting to ODF
Background

You saw in the DataNucleus JDO Tutorial how to configure a sample application for persisting to RDBMS. We took sample classes of Book and its parent Product and showed

  1. how to configure them for persistence, namely which classes and which fields are to be persisted, and into which datastore "tables"
  2. how to actually perform the persistence and how to access objects from the datastore

In that tutorial we used an RDBMS datastore (HSQLDB for the downloadable sample). And we ended up with a database schema as

This tutorial shows how easy it is to change that to persist that very same data to a different datastore. In this case we choose ODF, but could have chosen any of the other datastores supported by DataNucleus





Persistence to ODF

What if we want to persist that same data to a spreadsheet like ODF (OpenOffice) ? Well the changes necessary are minimal. In this particular case we defined an ORM definition for storing the column names etc for HSQLDB. In this case here we want to configure the names of the columns and also the positions in the spreadsheet. So let's define a file package-odf.orm

<orm>
    <package name="org.datanucleus.samples.jdo.tutorial">
        <class name="Product" identity-type="datastore" table="Products">
            <extension vendor-name="datanucleus" key="include-column-headers" value="true"/>
            <inheritance strategy="complete-table"/>
            <datastore-identity>
                <column name="Id" position="0"/>
            </datastore-identity>
            <field name="name">
                <column name="Name" position="1"/>
            </field>
            <field name="description">
                <column name="Description" position="2"/>
            </field>
            <field name="price">
                <column name="Price" position="3"/>
            </field>
        </class>

        <class name="Book" identity-type="datastore" table="Books">
            <extension vendor-name="datanucleus" key="include-column-headers" value="true"/>
            <inheritance strategy="complete-table"/>
            <datastore-identity>
                <column name="Id" position="0"/>
            </datastore-identity>
            <field name="author">
                <column name="Author" position="4"/>
            </field>
            <field name="isbn">
                <column name="ISBN" position="5"/>
            </field>
            <field name="publisher">
                <column name="Publisher" position="6"/>
            </field>
        </class>
    </package>
</orm>

With ODF the only inheritance strategy supported currently is complete-table so we use that, and we will have 2 worksheets in our spreadsheet, one called Products and one called Books . We have defined the column names to be with capital letter etc, so they will look nice in the spreadsheet, and that both worksheets will have column headers included. We also set the relative positions of the columns. The only remaining thing to do is to set the persistence properties to persist to ODF instead of RDBMS, like this

javax.jdo.PersistenceManagerFactoryClass=org.datanucleus.jdo.JDOPersistenceManagerFactory
javax.jdo.option.ConnectionURL=odf:file:tutorial.ods
javax.jdo.option.Mapping=odf

If we now put datanucleus-odf and ODFDOM in the CLASSPATH and run it we get a spreadsheet like this





Summary

As you can see, with very minimal changes, we can make our tutorial persist the data to an ODF spreadsheet. In fact we have not changed any model code, nor any persistence code and simply defined how it will be mapped to the spreadsheet. This flexibility and portability is not available with any other persistence tool .