HomeEMF HibernateEMF JDO/JPOXWeb App GenerationServices
 

Environment setup and initialization

Set dependencies of EMF model and GMF generated plugins

As a first step you have to set the following dependencies in the Mindmap model Manifest.MF: Hibernate Libraries (replace with the name of your Hibernate Library plugin) and org.eclipse.emf.teneo.hibernate. Check 'reexport this dependency' for both dependencies.

Set dependency

Initialization of Teneo

The Teneo layer has to be activated when the diagram editor starts. Note that this code assumes that database has been created. This example uses mysql, replace the relevant options for your specific database.

As a first step create a separate package in the org.eclipse.gmf.examples.mindmap.diagram plugin. Name this package: org.eclipse.gmf.examples.mindmap.diagram.db. In this step of the tutorial four files should be placed in this package (for more information see below):

  1. the StoreController.java file (download here). This class initializes and manages one Teneo datastore.
  2. an annotations.xml file to map the GMF model to a relational store (download here). This file contains hints to Teneo on how to map the GMF model. It is required because the GMF model contains multiple-inheritance structures which can not be translated automatically to a relational store. See below for some more information.
  3. a specific EList property handler (download here). This is to solve a minor technical detail with the GMF generated code and the GMF model, see here for more details.
  4. a teneo.properties file (download here) containing hibernate and teneo properties. This file contains database connection information which probably has to be adapted to your specific case.

After creating the package and importing the files your package should look like this:

Files in the db package

The StoreController class is discussed in more detail.

StoreController: initializing the Teneo layer

The StoreController class initializes the Teneo layer using the properties in the teneo.properties file.

The class starts with a declaration of the uri to connect to the database:

public static final URI DATABASE_URI = URI.createURI("hbxml://?dsname=mindmap&query1=from Map&query2=from Diagram");
		

This uri has some specific parts: 1) the dsname parameter is the name of datastore (see below), 2) the queries in the uri ensure that the resource loads the Mindmap Map and the GMF Diagram as roots in the resource. The uri starts with hbxml to ensure that the HibernateXMLResource is used.

The next piece of code can be found in the initializeDataStore method:

// create and register the datastore using the mindmap name
final HbSessionDataStore localDataStore = new HbSessionDataStore();
localDataStore.setName("mindmap");
HbHelper.INSTANCE.register(localDataStore);
		

This code creates a datastore and registers it in the global datastore registry. There the HibernateXMLResource can find it using the dsname parameter and its value: mindmap.

A number of EPackages have to be registered with Teneo to persist the mindmap data as well as the GMF diagram data:

// now register the epackages. There are four epackages:
// 1) the model itself
// 2) the GMF model
// 3) the ecore model because GMF depends on it
// 4) and the ecore XML type package
final EPackage[] ePackages = new EPackage[] { MindmapPackage.eINSTANCE,
		NotationPackage.eINSTANCE, EcorePackage.eINSTANCE,
		XMLTypePackage.eINSTANCE };
localDataStore.setEPackages(ePackages);
		

The following code loads the teneo.properties and directs Teneo to use the annotations.xml file to influence the mapping to the relational model:

final Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("teneo.properties"));

// handle multiple inheritance in the GMF model
props.setProperty(PersistenceOptions.PERSISTENCE_XML,
		"annotations.xml");

localDataStore.setProperties(props);
		

Note that it is possible that the annotations.xml and teneo.properties file are not found. In that case you can try to put them in the root of the source tree and let the build process copy them from there. In that case the references to these names in the source code have to be changed to resp. "/teneo.properties" and "/annotations.xml".

In GMF there is a difference between the generated code and the in-memory model. Teneo can not handle this out of the box and requires a different property handler (the GMFEListPropertyHandler from above) to handle this correctly. The following code registers this property handler using the Teneo extension mechanism (see here for more info on Teneo extensions).

// solve a specific issue with the GMF model
localDataStore.getExtensionManager().registerExtension(
		EListPropertyHandler.class.getName(),
		GMFEListPropertyHandler.class.getName());
		

As a last step the actual Teneo initialization has to take place. This step will also create the database tables.

localDataStore.initialize();
// print the hibernate mapping
System.err.println(localDataStore.getMappingXML());
		

The StoreController class will initialize the Teneo layer when it is used for the first time.

Initialize Teneo when plugin starts

To initialize Teneo when the diagram plugin starts add the following code to the start method of the generated MindmapDiagramEditorPlugin: StoreController.getInstance().initializeDataStore(). To stop the Teneo layer when the plugin stops, add this to the stop method: StoreController.getInstance().closeDataStore.

GMF Multiple Inheritance

There are a number of GMF types (ShapeStyle, DiagramStyle and ConnectorStyle) which inherit from multiple other GMF types as well as the EMF EObject type. The EObject type is listed as the first supertype of these types. This means that Teneo will place these types in the EObject type hierarchy when mapping to a relational store. This will not work as for GMF these types are primarily subtypes of Style. The annotations.xml file in this tutorial ensures that the mentioned Style types are placed in the correct hierarchy (in the relational model).

The next step of this tutorial describes what needs to change in the generated GMF diagram code to use the Teneo Hibernateresource.