HomeEMF HibernateEMF JDO/JPOXWeb App GenerationServices
 

Quick Start

This quick start describes in a few short steps how to setup, initialize and use the runtime layer. It uses the well-known EMF Library example. It does not go as far as the Library Tutorial but gives a quick idea on how to get up-and running.

The source code of this quick start can be downloaded here: QuickStart.java.

It is assumed that you have done one of the EMF Library tutorials (see here or here)

The quick start consists of the following steps:

  1. Set Library model plugin dependency:
    • Add the EMF persistency runtime plugin (org.eclipse.emf.teneo.hibernate) to the plugin dependencies of the Library (model) project. Note enable reexport depencency for the plugin (right-click on entered dependency to find this option)
    • Add a jdbc driver to the model project.
    • Add the hibernate libraries to the model project.
  2. Create an empty database (with the name library) in your database server
  3. Create a new class in the org.eclipse.example.library package with a static main method
  4. Initialize the runtime layer and create a HbDataStore: add the following to the main method. Replace the database connection information here with your own information.:
    // the name of the datastore
    String hbName = "MySF"; 
    
    // the name of the database, this database should exist but does not need to contain tables
    String dbName = "library"; 
    
    // 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.PASS, "root");
    props.setProperty(Environment.URL, "jdbc:mysql://127.0.0.1:3306/" + dbName);
    props.setProperty(Environment.DIALECT, org.hibernate.dialect.MySQLInnoDBDialect.class.getName());
    
    // create the HbDataStore
    HbDataStore hbds = HbHelper.INSTANCE.createRegisterDataStore(hbName);
    
    // sets its epackages stored in this datastore
    hbds.setEPackages(new EPackage[]{LibraryPackage.eINSTANCE});
    
    // set the relational database to use and other properties
    hbds.setProperties(props);
    
    // initialize, also creates the database tables
    hbds.initialize();
    					
  5. Create a Hibernate session and a transaction:
    // Create a session and a transaction
    Session session = hbds.getSessionFactory().openSession();
    Transaction tx = session.getTransaction();
        	
  6. Begin a transaction, create a library and make it persistent
    // start a transaction, create a library and make it persistent
    tx.begin();
    Library lib = LibraryFactory.eINSTANCE.createLibrary();
    lib.setName("My Library");
    session.save(lib);
    	
  7. Create a writer and book, add to the library and commit
    // create a writer
    Writer writer = LibraryFactory.eINSTANCE.createWriter();
    writer.setName("JRR Tolkien");
    	       
    // and one of his books
    Book book = LibraryFactory.eINSTANCE.createBook();
    book.setAuthor(writer);
    book.setPages(305);
    book.setTitle("The Hobbit");
    book.setCategory(BookCategory.SCIENCE_FICTION);
    	
    // add the writer/book to the library. The writer and book are automatically
    // made persistent because they are added to the library which is already
    // made persistent
    lib.getWriters().add(writer);
    lib.getBooks().add(book);
    	        
    // at commit the objects will be present in the database
    tx.commit();
    // and close of 
    session.close();