HomeEMF HibernateEMF JDO/JPOXWeb App GenerationServices
 

Step 2: Create and store EMF Objects

The complete code for this tutorial can be found here: Tutorial1.java.

Hibernate database configuration

To let Hibernate access your database, you must specify the JDBC driver, database URL, username/password and the database-specific dialect to use. These settings are specified using the standard Properties mechanism.

Teneo allows you to configure Hibernate in two ways:

  • Automatically: by placing the properties in the file "hibernate.properties" in a root directory of the classpath. We use this method in this tutorial.
  • Programmatically: by creating a Properties instance and using HbDataStore.setProperties().

A sample hibernate.properties file is shown below:

hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.url=jdbc:hsqldb:hsql://127.0.0.1/library
hibernate.connection.username=sa
hibernate.connection.password= 
hibernate.dialect=org.hibernate.dialect.HSQLDialect

Example hibernate.properties:

Note that Hibernate has many more configuration properties. For more information see the Hibernate reference documentation.

Initialize runtime layer

Create and initialize the runtime Hibernate EMF layer as follows:

// Create the DataStore.
final String dataStoreName = "LibraryDataStore";
final HbDataStore dataStore = HbHelper.INSTANCE.createRegisterDataStore(dataStoreName);

// Configure the EPackages used by this DataStore.
dataStore.setEPackages(new EPackage[] { LibraryPackage.eINSTANCE });

// Initialize the DataStore. This sets up the Hibernate mapping and
// creates the corresponding tables in the database.
dataStore.initialize();

Obtain a SessionFactory and start a Transaction.

The SessionFactory, Session and Transaction are the familiar Hibernate entry points:

final SessionFactory sessionFactory = dataStore.getSessionFactory();

// Open a new Session and start Transaction.
Session session = sessionFactory.openSession();
session.beginTransaction();

Create and populate Library

// Create a Library.
Library library = LibraryFactory.eINSTANCE.createLibrary();
library.setName("My Library");
// Make it persistent.
session.save(library);
		
// 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 and Book to the Library. They are made
// persistent automatically because the Library is itself
// already persistent.
library.getWriters().add(writer);
library.getBooks().add(book);

Commit Transaction and close Session

The Library is not stored in the database until you commit the Transaction:

session.getTransaction().commit();
session.close();

Next Step

In the next step of this tutorial we retrieve the Library from the database and add a new Book and Writer.

Click here to go to the next step.