Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Teneo/Hibernate/Editor Tutorial"

(Adapting the generated editor code)
Line 21: Line 21:
 
The following code needs to be added to the static inner class ''Implementation'' in the ''ExtlibraryEditorPlugin'' class:
 
The following code needs to be added to the static inner class ''Implementation'' in the ''ExtlibraryEditorPlugin'' class:
 
<source lang="java">
 
<source lang="java">
public void start(BundleContext context) throws Exception  
+
public void start(BundleContext context) throws Exception  
{
+
{
// Set the database information, Environment is org.hibernate.cfg.Environment
+
// Set the database information, Environment is org.hibernate.cfg.Environment
final Properties props = new Properties();
+
final Properties props = new Properties();
props.setProperty(Environment.DRIVER, "org.hsqldb.jdbcDriver");
+
props.setProperty(Environment.DRIVER, "org.hsqldb.jdbcDriver");
props.setProperty(Environment.USER, "sa");
+
props.setProperty(Environment.USER, "sa");
props.setProperty(Environment.URL, "jdbc:hsqldb:file:/tmp/hsqldb");
+
props.setProperty(Environment.URL, "jdbc:hsqldb:file:/tmp/hsqldb");
props.setProperty(Environment.PASS, "");
+
props.setProperty(Environment.PASS, "");
props.setProperty(Environment.DIALECT, org.hibernate.dialect.HSQLDialect.class.getName());
+
props.setProperty(Environment.DIALECT, org.hibernate.dialect.HSQLDialect.class.getName());
// props.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver");
+
// props.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver");
// props.setProperty(Environment.USER, "root");
+
// props.setProperty(Environment.USER, "root");
// props.setProperty(Environment.URL, "jdbc:mysql://127.0.0.1:3306/library");
+
// props.setProperty(Environment.URL, "jdbc:mysql://127.0.0.1:3306/library");
// props.setProperty(Environment.PASS, "root");
+
// props.setProperty(Environment.PASS, "root");
// props.setProperty(Environment.DIALECT, org.hibernate.dialect.MySQLInnoDBDialect.class.getName());
+
// props.setProperty(Environment.DIALECT, org.hibernate.dialect.MySQLInnoDBDialect.class.getName());
  
// Initialize create the HbDataStore
+
// Initialize create the HbDataStore
HbDataStore hbds = HbHelper.INSTANCE.createRegisterDataStore("library");
+
HbDataStore hbds = HbHelper.INSTANCE.createRegisterDataStore("library");
hbds.setEPackages(new EPackage[]{ExtlibraryPackage.eINSTANCE});
+
hbds.setEPackages(new EPackage[]{ExtlibraryPackage.eINSTANCE});
hbds.setProperties(props);
+
hbds.setProperties(props);
hbds.initialize();
+
hbds.initialize();
 
 
super.start(context);
+
super.start(context);
}
+
}
 
</source>
 
</source>
 +
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.
 +
 +
=== Resource Factory setting ===
 +
The resource factory setting in the plugin.xml of the org.eclipse.emf.teneo.hibernate.examples has been set specifically for this tutorial. 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.
 +
 +
 +
[[Image:org.eclipse.emf.teneo.resource.factory.png|center]]

Revision as of 04:50, 3 March 2010

This tutorial describes how the Hibernate EMF resource can be used in the Library editor. This tutorial assumes that you have a basic knowledge of EMF and EMF generated editors. For information on EMF see http://eclipse.org/modeling/emf/docs this page].

Initial Setup

This tutorial assumes that you have a running Eclipse with EMF and Teneo installed. In addition the Teneo dependencies (incl. hsqldb and mysql drivers) should be installed. See the Download & Install page for more information.

The tutorial uses hsqldb but it can easily be changed to use mysql or another database. For other databases than hsqldb and mysql you need to take make sure that the jdbc driver is in the classpath of the org.eclipse.emf.teneo.hibernate.examples project.

For mysql and other non-in-memory databases you have to create the database up-front (so not the tables inside the database but just the database itself). For this tutorial the database name should be: library.

Generating the editor code

After downloading the example project the first step is to generate the EMF editor code. Open the genmodel file in the model folder and right click and select 'Generate All'. This will generate three new development projects (see screenshot).


Org.eclipse.emf.teneo.generate.all.png


Adapting the generated editor code

The generated editor code needs to be adapted so that it start the Teneo persistence layer when the editor starts.

The following code needs to be added to the static inner class Implementation in the ExtlibraryEditorPlugin class:

public void start(BundleContext context) throws Exception 
{
	// Set the database information, Environment is org.hibernate.cfg.Environment
	final Properties props = new Properties();
	props.setProperty(Environment.DRIVER, "org.hsqldb.jdbcDriver");
	props.setProperty(Environment.USER, "sa");
	props.setProperty(Environment.URL, "jdbc:hsqldb:file:/tmp/hsqldb");
	props.setProperty(Environment.PASS, "");
	props.setProperty(Environment.DIALECT, org.hibernate.dialect.HSQLDialect.class.getName());
//	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[]{ExtlibraryPackage.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.

Resource Factory setting

The resource factory setting in the plugin.xml of the org.eclipse.emf.teneo.hibernate.examples has been set specifically for this tutorial. 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.


Org.eclipse.emf.teneo.resource.factory.png

Back to the top