HomeEMF HibernateEMF JDO/JPOXWeb App GenerationServices
 

Quick Start

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

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 - JPOX persistency runtime plugin (org.eclipse.emf.teneo.jpox) to the plugin dependencies of the Library (model) project
    • Add the plugin with JPOX libraries to the plugin dependencies of the Library (model) project
  2. Enable JPOX: Right click on the Library project and select the menu JPOX and select 'Add JPOX Support' and check 'Enable Auto Enhancement'.
  3. Generate JDO file: right click on the ecore file (normally called library.ecore) and in the Teneo menu choose one of the Generate EMF - JDO/JPOX OR Mapping (...) option. A package.jdo file should be created next to the ecore file. It is possible that you need to refresh the folder to see the created package.jdo.
  4. Copy the package.jdo to a location in the source tree and ensure that the build process copies it to the build target location, if you have automatic build enabled then JPOX should now enhance the classes automatically (after the build).
  5. Create a database (with the name mylibrary) in your database server
  6. Create a new class in the org.eclipse.example.library with a static main method
  7. Initialize the runtime layer, add the following to the main method. Replace the database connection information here with your own information:
    String pmfName = "MyPMF"; // the name of the JpoxDataStore
        	
    // db connection info, replace with your own database connection information
    Properties properties = new Properties();
    properties.setProperty(PMFConfiguration.JDO_DATASTORE_DRIVERNAME_PROPERTY, "com.mysql.jdbc.Driver");
    properties.setProperty(PMFConfiguration.JDO_DATASTORE_URL_PROPERTY, "jdbc:mysql://127.0.0.1:3306/mylibrary");
    properties.setProperty(PMFConfiguration.JDO_DATASTORE_USERNAME_PROPERTY, "root");
    properties.setProperty(PMFConfiguration.JDO_DATASTORE_PASSWORD_PROPERTY, "root");
    
    // create the data store  
    JpoxDataStore jpoxDataStore = JpoxHelper.INSTANCE.createRegisterDataStore(pmfName);
    jpoxDataStore.setProperties(properties);
    jpoxDataStore.setEPackages(new EPackage[]{LibraryPackage.eINSTANCE});
    jpoxDataStore.initialize();
    					
  8. Create a persistence manager and factory
    // Now create a persistence manager and a transaction
    PersistenceManager pm = jpoxDataStore.getPMF().getPersistenceManager();
    Transaction tx = pm.currentTransaction(); 
        	
  9. 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");
    pm.makePersistent(lib);
    	
  10. 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 was already
    // made persistent
    lib.getWriters().add(writer);
    lib.getBooks().add(book);
        
    // at commit the objects will be present in the database
    tx.commit();
    pm.close();