Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Persist data with Java Data Objects, Part 1

Grasp the qualities behind an ideal persistence layer

  • Print
  • Feedback

Page 3 of 6

PersistenceManager pm =PMFactory.initialize(..);
Company co = new Company("MyCompany");
Location l1  = new Location1 ("Boston");
Location l2 = new Location("New York");
// Create users.
User u1 = new User("Mark");
User u2 = new User("Tom");
User u3 = new User("Mary");
// Add users. A user can only "belong" to one location.
L1.addUser(u1);
L1.addUser(u2);
L2.addUser(u3);
// Add locations to the company.
co.addLocation(l1);
co.addLocation(l2);
// And finally, store the whole tree to the database.
pm.persist(c);


In another session, you can look up companies employing the user Tom:

PersistenceManager pm =PMFactory.initialize(...)
Collection companiesEmployingToms = pm.find("company.location.user.name = 'Tom'");


For relational data stores, you must create an additional mapping file. It might look like this:

<!DOCTYPE mapping PUBLIC ... >
<mapping>
<class name="com.numatica.example.Company" identity="companyID" key-generator="SEQUENCE">
<cache-type type="count-limited" capacity="5"/>
      
      <description>Company</description>
<map-to table="Companies"/>
      <field name="companyID"type="long">
 <sql name="companyID" type="numeric"/>
</field>
      <field name="name" type="string">
<sql name="name" type="varchar"/>
</field>
      <field name="locations" type="com.numatica.example.Location" collection="arraylist">
</field>
</class>
<class name="com.numatica.example.Location "identity="locationID"
           key-generator="SEQUENCE">
<cache-type type="unlimited"/>
        
        <description>Locations</description>
<map-to table="Locations"/>
        <field name="locationID" type="long">
<sql name="locationID" type="numeric"/>
</field>
        <field name="name" type="string">
<sql name="name" type="varchar"/>
        </field>
        
      <field name="company" type="com.numatica.example.Company"required="true">
            <sql name="companyID"/>
</field>
</class>
<class name="com.numatica.example.User" identity="userID"
depends="com.numatica.example.Location" >
<cache-type type="count-limited" capacity="200"/>
        
        <description>User</description>
<map-to table="Users"/>
       
 <field name="userID" type="integer">
<sql name="userID" type="numeric"/>
</field>
        <field name="location" type="com.numatica.example.Location"required="true">
            <sql name="locationID"/>
</field>
        <field name="name" type="string">
<sql name="username" type="varchar"/>
        </field>
</class>
</mapping>


The persistence layer takes care of the rest, which encompasses the following:

  • Finding dependent object groups
  • Managing application object identity
  • Managing persistent object identities (primary keys)
  • Persisting each object in the appropriate order
  • Providing cache management
  • Providing the proper transactional context (we don't want only a portion of the object tree persisted, do we?)
  • Providing user-selectable locking modes


Available solutions

The available persistence layer solutions divide into the following groups:

  1. Roll your own, using perhaps the JDBC API
  2. Proprietary object-relational mapping tools or object databases (ODBMS)
  3. J2EE/entity bean CMP (container-managed persistence) solutions
  4. Java Data Objects (JDO)-based solutions


None of the solutions currently available satisfy all the criteria set forth for the ideal persistence layer. Below I review the first three solutions, before focusing on the JDO-based approach.

  • Print
  • Feedback

Resources