Eclipse EMF: how to create and persist a model

Eclipse EMF: how to create and persist a model

In this tutorial we will learn how to create and persist an EMF model.
The tutorial is based on Eclipse Galileo, but the steps should be the same for older versions. Anyway here are the steps needed if you decide to start from scratch.

Download Eclipse Galileo

Download Eclipse Galileo 3.5M6 from the Eclipse.org download page, extract on your file-system and run eclipse.exe.

Add EMF and Teneo support

Go to Help -> Install New Software…; in the text field “Work with:” select “All Avaiable Sites“.
In the text just after (not labeled) enter “EMF” and wait for the results; you should see the following items:Check both EMF SDK and Teneo EMF Hibernate SDK, then press Next and follow the wizard to install the new features.
Restart Eclipse at the end of the installation.

Create the EMF Project

Go to File -> New -> Other…; expand Eclipse Modeling Framework and select Empty EMFProject.press Next, enter a name for the project (e.g. MyModel) and press Finish.

Create the Ecore Model

Select the model folder (generated from the previous EMF project wizard), right-click New -> Other… and select Ecore Model under Eclipse Modeling Frameworkthen press Next and enter a name for the Ecore Model or leave the proposed one (My.ecore), press Finish.

Expand the Ecore Model root node, select the only unnamed child node and right-click Show Properties ViewIn the Properties view, enter “orders” as the Name and NS Prefix, while as NS URI enter “http://orders“.Right-click the node orders and select New Child -> EClassselect the new child, go to the Property View and enter “Customer” as the NameRight-click the new node Customer and select New Child -> EAttributeselect the new child, go to the Property View and enter “code” as the Name and EString as the Etypein the same way add another EAttribute called “name” of type EString and press Ctrl-S to save the work.

Create the Generator Model

From the Package Explorer View select the Ecore file (e.g. My.ecore), right-click New -> Other… and select EMF Generator Model under Eclipse Modeling Frameworkthen press Next and enter a name for the EMF Generator Model or leave the proposed one (My.genmodel), press Next, select Ecore Model in the Model Importers list, then Nextpress the Load button in the Ecore Import step, then Next and Finish.Now, from the Generator Model editor just opened, right-click Generate Model Codel

Use EMF-Teneo to persist and access the Model on a Database

Prior to invoking persistence operations on the EMF Model we need to include some dependencies into the current project.
In order to do this double-click the file plugin.xml from the Package Explorer view and select the tab Dependencies. Then press the Add… button in the left-upper area (Required Plug-ins) and add the following bundles:
– org.eclipse.emf.ecore.xmi
– org.eclipse.emf.teneo.hibernate
– org.apache.commons.loggingWe will also need some other JARs not included into the Eclipse environment. Let’s create therefore an ordinary folder for the additional libraries (New -> Folder), let’s call this “lib“.

The needed libraries are all included in the Hibernate Core Package, downloadable at http://www.hibernate.org/downloads.htmll.

hibernate3.jar
jta-1.1.jar (/lib/required)
dom4j-1.6.1.jar (/lib/required)
commons-collections-3.1.jar (/lib/required)

Copy the above JARs, along with the JDBC Driver (we will use MySQL for our test),

mysql-connector-java-5.1.7-bin.jar

into the lib folder.

Now go to the plugin.xml, select the Runtime tab, go to the Classpath area (bottom-right), press the Add… button, browse for the lib folder and add all the JARs.In order to test the persistence features of EMF and Teneo we will create a new source folder in our project (New -> Source Folder); let’s call it “src_test” and create there a new package and class with the following code into the main method:

[java]

import java.util.Properties;
import orders.Customer;
import orders.OrdersPackage;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.teneo.hibernate.HbDataStore;
import org.eclipse.emf.teneo.hibernate.HbHelper;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Environment;

HbDataStore hbds = (HbDataStore)HbHelper.INSTANCE.createRegisterDataStore("MyDb");

final Properties props = new Properties();
props.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver");
props.setProperty(Environment.URL, "jdbc:mysql://127.0.0.1:3306/test");
props.setProperty(Environment.USER, "root");
props.setProperty(Environment.PASS, "admin");
props.setProperty(Environment.DIALECT, org.hibernate.dialect.MySQLInnoDBDialect.class.getName());
props.setProperty(Environment.SHOW_SQL, "true");
hbds.setProperties(props);

hbds.setEPackages(new EPackage[]{OrdersPackage.eINSTANCE});
hbds.initialize();
SessionFactory sessionFactory = hbds.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.getTransaction();
tx.begin();
Customer customer = OrdersPackage.eINSTANCE.getOrdersFactory().createCustomer();
customer.setCode("0023485");
session.save(customer);

tx.commit();
session.close();
[/java]

Then launch this class and see what happens.

2012-06-08T13:30:36+00:00By |

2 Comments

  1. Josef Joller Sunday October 16th, 2011 at 08:16 PM - Reply

    Java Code is quite scrambled. I used the example from your EMF MySQL Tutorial, but work!added some more libs.
    Good works!!

    • Vincenzo Monday October 17th, 2011 at 09:16 AM - Reply

      Hi Josef,
      indeed this article is a bit old; it refers to Eclipse Galileo in fact.
      The complete updated tutorial series is here:
      http://www.rcp-vision.com/?p=1157&lang=en
      it is intended as a sequence of steps, each article built on the previous one.
      Let me know if you have suggestions or problems while following it.
      Cheers

      Vincenzo Caselli
      vincenzo.caselli@rcp-vision.com

Leave A Comment