EMF allows you to dynamically change the in-memory ecore model by adding EPackages, EClasses and EStructuralFeatures. Teneo supports persisting dynamically created models. It is for example possible to read an ecore model from an ecore file (without generating code) and create the relational database schema. There are however some (practical) limitations, see the last section on this page.

The source code of this tutorial is available in the example project which you can download here.

This tutorial assumes some basic knowledge of EMF. If you are new to EMF then it can make sense to first try one of the core EMF tutorials which you can find here.

Before starting this tutorial

Before starting this tutorial there are a number of things which you need to take into account:

It uses the QuickStart tutorial class described in the quick start tutorial to create a database for the Library example and some content.

When using a non-in-memory database: the tutorial assumes that there is an empty database with the name DynamicLibrary. When you rerun this tutorial you should make sure that this database is empty again (otherwise one of the smaller tests in the tutorial will fail).

In this tutorial the Library example is extended with two new types: SchoolBook (which inherits from Book) and Course which refers to a SchoolBook.

Running the tutorial

After downloading the examples project from cvs you can directly run the quick start program. Open the project and navigate to the Dynamic.java file and right click and select 'Run As > Java Application'. You will see several things getting printed to the console (one of them is the generated Hibernate mapping).

This run takes the following steps:

reads the EPackage model from the generated EPackage file.

generate a hibernate mapping for the EPackage

create the database (on hsqldb)

create EMF objects and persist them

change the model

re-initialize the datastore and update the relational database schema

create and persist EMF objects adhering to the changed model

query for these objects

retrieve the EMF objects using an Hibernate EMF resource

Walk-through

Now let's walk through the code.

Quick start steps

The tutorial first calls the same code as described in the quick start tutorial, so it can make sense to first walk-through the quick start example. Further here only the additional steps are described.

Adapt the model

The following code extends the model with new types. It creates the SchoolBook EClass and sets the Book from the LibraryPackage as the supertype:

final EcoreFactory efactory = EcoreFactory.eINSTANCE;final EcorePackage epackage= EcorePackage.eINSTANCE;// create the SchoolBook EClass
EClass schoolBookEClass = efactory.createEClass();
schoolBookEClass.setName("SchoolBook");// create a new attribute for this EClass
EAttribute level = efactory.createEAttribute();
level.setName("level");
level.setEType(epackage.getEInt());
schoolBookEClass.getEStructuralFeatures().add(level);// Set the supertype of SchoolBook to the Book
schoolBookEClass.getESuperTypes().add(LibraryPackage.eINSTANCE.getBook());

Reconfigure Hibernate and the Relational Database Schema

The EPackage has been created now Hibernate should be reconfigured and the database schema should be updated.

// Now reset the epackages in the datastore, only required if you add an EPackage
hbds.setEPackages(new EPackage[]{LibraryPackage.eINSTANCE, schoolPackage});// reinitialize hibernate and update the database schema
hbds.initialize();// print the new hibernate.hbm.xmlSystem.err.println(hbds.getMappingXML());

At this point the database schema should have been updated.

Create and persist Dynamic EObject

The dynamic EMF objects can now be created. Because SchoolBook inherits from Book a real Book object is created:

Query for dynamic EMF objects

The SchoolBook can be retrieved using a polymorphic query, for a Course the dynamic EAttribute can be used in the where clause.

// Now query for the books, one of them should be a SchoolBook
tx.begin();
Query qry = session.createQuery("from Book");List list = qry.list();Book schoolBook =null;for(Iterator it = list.iterator(); it.hasNext();){Book book =(Book)it.next();if(book.eClass()== schoolBookEClass){if(schoolBook !=null){thrownewError("More than one schoolbook? Was the database not empty?");}
schoolBook = book;}}if(schoolBook ==null){thrownewError("No schoolbook??");}// now query for all courses with the right name
qry = session.createQuery("from Course where courseName='Dutch Literature Level 1'");
list = qry.list();
EObject eobject =(EObject)list.get(0);if(eobject.eClass()!= courseEClass){thrownewError("No Course?");}// the schoolBook should be the book of the courseBook courseBk =(Book)eobject.eGet(courseBook);if(courseBk != schoolBook){thrownewError("No schoolbook?");}// and the dynamic feature level should be 1if(((Integer)courseBk.eGet(level)).intValue()!=1){thrownewError("Incorrect level?");}
tx.commit();
session.close();

Limitations

It is not possible to create a containment relation between a dynamic model and a static model part. With static we mean the model for which java code has been generated.

When the model is dynamically changed it is required to update the database schema. To change the database schema database servers can lock the whole database. This is probably not practical in a live production environment.