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

Teneo/Hibernate/Hibernate Datastore


The HbDataStore/HbSessionDataStore

The HbSessionDataStore object controls the persistence of a set of EPackage in a specific relational database. The HbSessionDataStore contains a SessionFactory and is responsible for creating and updating the database schema.

To create and initialize a HbSessionDataStore, follow these steps:

  1. Create and register the HbSessionDataStore using HbHelper.INSTANCE.createRegisterDataStore().
  2. Configure the EPackages to be handled by this DataStore using HbDataStore.setEPackages().
  3. Configure Hibernate properties for accessing the database. (Described here.)
  4. Call initialize() to build the Hibernate mapping and create the tables in the database.

You can then obtain the Hibernate SessionFactory using HbSessionDataStore.getSessionFactory().

The HbSessionDataStore can also be used to import and export xml and xmi through the importDataStore and exportDataStore methods.

The HbSessionDataStore has a specific method to retrieve all referers to a passed EObject: getCrossReferencers.


HbContext

The HbContext returns default implementations for Accessors, class names of Tuplizers and contains a method to create the Hibernate Configuration. HbContext is set in the HbSessionDataStore using the setHbContext method, as a default the HbContextImpl is used. You can extend HbContextImpl to override only specific methods.


Overriding the HbDataStore, HbSessionDataStore, HbEntityDataStore

It is possible to override one of the concrete subclasses of HbDataStore, i.e. HbSessionDataStore or HbEntityDataStore, with your own implementation. Next to the specific implementation of the HbDataStore you also need to register a HbDataStoreFactory which creates this specific HbDataStore, the HbDataStoreFactory is registered through the HbHelper.setHbDataStoreFactory method.


HbEntityDataStore, support for Hibernate EntityManager

Teneo can also operate together with the Hibernate EntityManager. All the documentation, examples and tutorials on this website also apply in case of using Teneo together with the Hibernate Entitymanager.

To use Teneo and the EJB3/JPA EntityManagerFactory (instead of the SessionFactory) you need to use a different datastore: HbEntityDataStore. Instead of using the HbHelper to create a datastore you need to directly create a HbEntityDataStore (so do: new HbEntityDataStore()). After creation you need to set its name by calling setName. After that the use of the HbEntityDataStore is the same as for the HbDataStore.

The HbEntityDataStore is a subclass of HbDataStore and adds a getEntityManagerFactory method. This means that everywhere on this site where you read HbDataStore you can also use the HbEntityDataStore.

Note: one thing we noticed is that in combination with the Hibernate Entitymanager queries did not always work correctly. For example the following query:

SELECT REF FROM Library AS REF WHERE :TO IN elements(REF.writers)

This query did not work because Hibernate was not able to determine the type of the :to parameter. The query had to be changed to:

SELECT REF FROM Library AS REF, Writer AS refto WHERE refto = :TO AND refto IN elements(REF.writers)

All testcases are run also for the Hibernate Entitymanager.

Spring configuration and the HbSessionDataStore/HbEntityDataStore

It is possible to use the HbSessionDataStore and HbEntityDataStore as a sessionFactory or entityManagerFactory for Spring. The HbSessionDataStore implements the SessionFactory and the HbEntityDataStore implements the EntityManagerFactory interface.

Note: a recent blog describes a setup of EMF Teneo and Spring, it is more recent than the description below so it can be more accurate, see here

To configure the datastore through Spring and let Spring use it as a sessionFactory/entityManagerFactory, follow these guidelines:

Define a bean declaration in your applicationContext.xml with id set to "sessionFactory" or "entityManagerFactory" and the class attribute set to resp. the HbSessionDataStore or HbEntityDataStore classname. The properties of the datastore have to be set as follows

  • name: set the name property of the datastore.
  • persistenceProperties: set the persistenceProperties using standard Spring methods for setting properties.
  • hibernateProperties: set the hibernateProperties using standard Spring methods for setting properties. The hibernateProperties also define the datasource or jdbc parameters, so a separate persistence.xml is not required (and not used).
  • ePackageClasses: set the epackage class names through the ePackageClasses property on the HbSessionDataStore/HbEntityDataStore. The ePackageClasses property is a list of strings, with each string containing the classname of the EPackage Impl class, for example: org.eclipse.emf.teneo.samples.emf.sample.library.impl.LibraryPackageImpl.

When using Spring managed transactions you possibly also have to set the hibernate.current_session_context_class property. The value should then be set to: org.springframework.orm.hibernate3.SpringSessionContext. Note this property can not be set through the datastore properties but must be set in a hibernate.properties file or a hibernate.cfg.xml file. Apparently setting this programmatically does not work in Hibernate.

This Spring configuration is not tested using a separate testcase, let us know if the above description needs to be corrected.

Runtime Options

Options are passed to the HbDataStore using a Properties object which is set using the HbDataStore.setProperties method.

The available options are all present in the org.eclipse.emf.teneo.PersistenceOptions class. See the configurations options page for a complete overview.


Getting the Hibernate Mapping XML

There are two methods to get to the hibernate mapping xml programmatically.

The mapping xml which has been generated by Teneo automatically (if no hibernate.hbm.xml file was specified explicitly) can be retrieved using the method: getMappingXML on the HbDataStore.

The method HbHelper.INSTANCE.generateMapping(....) can be used to programmatically generate the mapping file.

You can save the generated mapping xml to a file and then use that file. See the persistence option to tell Teneo to use the mapping file, here.

The third method to get the hibernate mapping xml is by right clicking on an ecore file and select the appropriate Teneo submenu option. The disadvantage of this method is that it does not allow you to pass your own menu options.

Back to the top