Persistence/OrderSystemExample

Adding Persistence to the Order-System Example

Using a recent OTDT ...

... install and run the example following these steps:

1. Install the OTSample-OrderSystem into your workspace (New > Examples ... > Object Teams / Order System Example).

1.a. You may run the demo without persistence using these parameters:

  • Main class is org.objectteams.samples.ordersystem.gui.GUITest
  • These teams need to be instantiated/activated: ModelAdapterTeam, ControllerAdapterTeam, GUIAdapterTeam (tab Team Activation)

2. Download http://www.objectteams.org/distrib/demos/ordersystem-persistence.zip and import its content into the project. This contains:

  • a bunch of jars: EclipseLink, the OT/JPA integration, the database implementation "hsqldb", plus several dependencies
  • the configuration files persistence.xml and orm.xml (in folder resources/META-INF), plus a tiny build script that puts these files into place for the running application plus an external builder that invokes the script during building.
  • a source folder src-persistence containing the programmatical part of binding EclipseLink to the application.
  • updated files .project and .classpath to integrate the above (no OT-specific tricks here)

3. Run the application and in addition to the above mentioned teams (step 1.a) also instantiate & activate the OrderSystemPersistenceTeam.

  • Watch additional print outs starting with "OrderSystem: "
  • Notice that any changes to the application's data survive an exit-restart cycle.
  • The curious will find the data in file hsqldb/database.script, the database encoded as an SQL script.

Detailed Explanation

The integration consists of 5 additional files and no changes in existing files other than the mentioned .project and .classpath.

persistence.xml (source)

This file defines the persistence unit by listing all classes that should be persisted. Note that role classes are denoted using their binary name, i.e., using a '$' to separate the role name from its enclosing team

orm.xml (source)

This file defines the database mapping for the types listed in persistence.xml. While most declarations are standard uses of the JPA all role classes are declared to use the inheritance strategy "TABLE_PER_CLASS", as required to support implicit inheritance.

AbstractPersistenceTeam.java (source)

A re-usable team class providing functions for transactioned CRUD (create/read/update/delete).

OrderSystemPersistanceTeam.java (source) and ModelBridge.java (source)

Specializes AbstractPersistenceTeam for connecting the persistence service to the application:

  • configure the persistence unit (method getPersistenceUnitName())
  • connect application life-cycle events (role ShutDownHook -- startup is already handled in the super team).
  • intercept dummy data initialization to Read from database if available:
    • callin intercepting StockReservations.theInstance()
    • callins intercepting ModelAdapterTeam.fillStorage(), ModelAdapterTeam.createCustomers(), ModelAdapaterTeam.createOrders().
  • dispatch events for Create/Update/Delete
    • callin bindings in subclasses of OrderSystemPersistenceTeam.ModelBridge.EntityBridge.
      Note, that these nested roles are contained in the role file OrderSystemPersistenceTeam/ModelBridge.java.