ObJectRelationalBridge
BRIDGING JAVA OBJECTS AND RELATIONAL DATABASES


the ojb sequence manager

Author: Thomas Mahler, september 2001

automatical assignment of unique values

As mentioned in Tutorial1 OJB provides a mechanism to assign unique values for primary key attributes. You just have to enable it with a declaration in the respective FieldDescriptor of the XML repository as follows:

  <FieldDescriptor id="1">
    <field.name>_id</field.name>
    <column.name>ID</column.name>
    <jdbc_type>INT</jdbc_type>
    <PrimaryKey>true</PrimaryKey>
    <autoincrement>true</autoincrement>
  </FieldDescriptor>

This definitions contains the following information: The attribute _id is mapped on the table's column ID. The JDBC Type of this column is INTEGER. This is a primary key column. OJB shall automatically assign unique values to this attribute.

This mechanism works for columns of type INTEGER, CHAR and VARCHAR. This mechanism helps you to keep your business logic free from code that computes unique Ids for primary key attributes.

force computation of unique values

By default OJB triggers the computation of unique ids during calls to PersistenceBroker.store(...). Sometimes it will be necessary to have the ids computed in advance. This can be done by simply obtaining the Identity of the respective object as follows:

Identity oid = new Identity(object);

See the Tutorial2 for an example.

Alternative SequenceManagers

OJB internally uses a sequence manager for the generation of unique ids. This SequenceManager is not very sophisticated and provides only a minimal feature set. It's implemented in ojb.broker.util.SequenceManagerDefaultImpl.

Another implementation is ojb.broker.util.SequenceManagerHighLowImpl, which implements a more sophisticated aproach, by using a High/Low algorithm, as described in Mapping Objects To Relational Databases. It is able to generate IDs unique to a given Table as well IDs as unique over all objects that use the same property for their id (for example each class that uses "oid" as its id-attribute). This can be toggled by changing SequenceManagerGlobalIDs in OJB.properties. One mayor drawback of this implementation is, that it doesn't interact cleanly with legacy data.

If you still think something is missing you can just write your own implementation of the interface ojb.broker.util.SequenceManager. OJB provides a simple mechanism to integrate your implementation:

OJB obtains SequenceManager objects from the factory ojb.broker.util.SequenceManagerFactory. This Factory can be configured to generate instances of your specific implementation by changing the following entry in the configuration file ojb.broker.util.PersistenceBrokerFactoryConfiguration.properties:

SequenceManagerClass=ojb.broker.util.SequenceManagerDefaultImpl

to

SequenceManagerClass=acme.com.MyOwnSequenceManager.



Of course I'm interested in your solutions! If you have implemented something interesting, just contact me.


release: 0.8.375, date: 2002-04-04