Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Teneo/Hibernate/Hibernate Datastore"

(New page: __TOC__ ==The HbDataStore== The HbDataStore object controls the persistence of a set of EPackage in a specific relational database. The HbDataStore contains a SessionFactory and is respo...)
 
Line 61: Line 61:
 
The method HbHelper.INSTANCE.generateMapping(....) can be used to programmatically generate the mapping file.
 
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.
+
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, [[Teneo/Hibernate/Configuration_Options|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, see here. The disadvantage of this method is that it does not allow you to pass your own menu options.
+
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.

Revision as of 15:50, 21 January 2010


The HbDataStore

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

To create and initialize a HbDataStore, follow these steps:

  1. Create and register the HbDataStore 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 HbDataStore.getSessionFactory().

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

The HbDataStore 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 HbDataStore 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 on 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. In this case you need to create a HbEntityDataStore instead of the HbDataStore through the HbHelper interface. See also here.


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.

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