Chapter 23. OJB (Object Relational Broker)

23.1. Introduction

The OJB Gps Device provides support for database indexing through the use of Apache OJB ORM mappings. If your application uses OJB, it couldn't be easier to integrate Compass into your application (Sometimes with no code attached - see the petclinic sample).

OJB Device uses Compass::Core OSEM feature (Object to Search Engine Mappings) and OJB ORM feature (Object to Relational Mappings) to provide simple database indexing. The device also utilizes OJB lifecycle events to provide real time mirroring of data changes done through OJB. The path data travels through the system is: Database -- OJB -- Objects -- Compass::Gps -- Compass::Core (Search Engine).

23.2. Index Operation

OJB device provides the ability to index the database. The objects that will be indexed (or their matching database tables in the OJB mappings) are ones that have both OJB mappings and Compass::Core mappings (OSEM).

Indexing the data (using the <code>index()</code> operation) requires the indexPersistentBroker property to be set, before the index() operation is called. You can use the OjbGpsDevice#attachPersistenceBrokerForIndex(CompassGpsDevice, PersistenceBroker) as a helper method.

23.3. Real Time Data Mirroring

Real-time mirroring of data changes requires using the OjbGpsDevice#attachLifecycleListeners(PersistenceBroker) to let the device listen for any data changes, and OjbGpsDevice#removeLifecycleListeners(PersistenceBroker) to remove the listener. Since the lifecycle listener can only be set on the instance level and not the factory level, attach and remove must be called every time a PersistentBroker is instantiated. You can use the OjbGpsDeviceUtils#attachPersistentBrokerForMirror(CompassGpsDevice, PersistenceBroker) and OjbGpsDeviceUtils#removePersistentBrokerForMirror(CompassGpsDevice, PersistenceBroker) as helper methods if attachment/removal is required for a generic device (i.e. OjbGpsDevice).

Since the real time mirroring and the event listener registration sounds like an aspect for Ojb aware classes/methods, Compass::Spring utilizes spring support for OJB and aspects for a much simpler event registration, please see Compass::Spring for more documentation.

23.4. Configuration

Here is a code sample of how to configure the ojb device:

Compass compass = ... // set compass instance
CompassGps gps = new SingleCompassGps(compass);
CompassGpsDevice ojbDevice = new OjbGpsDevice();
.... // configure other devices
// just before calling the index method
PersistenceBroker pb = // create Persistence Broker
OjbGpsDeviceUtils.attachPersistenceBrokerForIndex(ojbDevice, pb);
// a Persistence Broker operation level
PersistenceBroker pb = // create Persistence Broker
OjbGpsDeviceUtils.attachPersistenceBrokerForMirror(ojbDevice, pb);
.... // Persistence Broker operations
OjbGpsDeviceUtils.removePersistenceBrokerForMirror(ojbDevice, pb);