HomeEMF HibernateEMF JDO/JPOXWeb App GenerationServices
 

Initialize the Library Editor

Set dependencies of EMF model and editor plugins

The EMF persistency plugin (org.eclipse.emf.teneo.hibernate) has to be added to the dependencies tab of the plugin of the Library model project. Check 'reexport this dependency'.

Initialization

To initialize the Teneo layer the following code has to be added to the static inner class Implementation in the LibraryEditorPlugin class.

Note this code should not be added if you want to start the editor using the Resource Utility

public void start(BundleContext context) throws Exception 
{
	org.eclipse.osgi.framework.debug.Debug.DEBUG_GENERAL = true;
	
	// Set the database information, Environment is org.hibernate.cfg.Environment
	final Properties props = new Properties();
	props.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver");
	props.setProperty(Environment.USER, "root");
	props.setProperty(Environment.URL, "jdbc:mysql://127.0.0.1:3306/library");
	props.setProperty(Environment.PASS, "root");
	props.setProperty(Environment.DIALECT, org.hibernate.dialect.MySQLInnoDBDialect.class.getName());

	// Initialize create the HbDataStore
	HbDataStore hbds = HbHelper.INSTANCE.createRegisterDataStore("library");
	hbds.setEPackages(new EPackage[]{LibraryPackage.eINSTANCE});
	hbds.setProperties(props);
	hbds.initialize();
	
	super.start(context);
}
	

Remarks:

  • The database properties have to be changed to contain your own database connection information.
  • The above source code uses the database library. This database has to exist (but can be empty).
  • The name of the datastore is chosen on purpose. The extension of the resource name (library in this case) is used to find the datastore, so therefore here the name library is chosen.
  • If you get this exception then you did not set the Eclipse-BuddyPolicy in the plugin containing the Hibernate libraries (see here):
    org.hibernate.MappingException: Could not determine type for: 
    	org.eclipse.emf.teneo.hibernate.mapping.econtainer.EContainerUserType, for columns: [org.hibernate.mapping.Column(econtainer_class), org.hibernate.mapping.Column(e_container)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
    at org.hibernate.mapping.Property.isValid(Property.java:185)
    			

Resource Factory setting

For this tutorial an Hibernate EMF resource factory has to be specified. This is done by setting the library element of the org.eclipse.emf.ecore.extension_parser extension point to: org.eclipse.emf.teneo.hibernate.resource.HibernateResourceFactory. You can find this extension point in plugin.xml in the model plugin.

Enabling Hibernate support for the library project

Create valid EMF Objects

The standard generated EMF Library example creates invalid objects. For example when you run the editor you can save a Library object with an empty name while this is a required element. Hibernate is more precise and will not allow this.

To prevent this add the following code in the performFinish method of the LibraryModelWizard class in the library editor project. This has to be added somewhere near line 237 (exact line number can vary because of formatting etc.) just after the rootObject variable has been set:

if (rootObject instanceof Library)
{
	((Library)rootObject).setName("My Library");
}
else if (rootObject instanceof Book)
{
	((Book)rootObject).setTitle("My Title");
}
else if (rootObject instanceof Writer)
{
	((Writer)rootObject).setName("My Name");
}

Please click here to go to the next step.