Hibernate.orgCommunity Documentation

Chapter 6. Hibernate OGM APIs

6.1. Bootstrap Hibernate OGM
6.2. JPA and native Hibernate ORM APIs
6.2.1. Accessing the OgmSession API
6.3. On flush and transactions
6.3.1. Acting upon errors during application of changes
6.4. SPIs

Hibernate OGM has very few specific APIs. For the most part, you will interact with it via either:

This chapter will only discuss the Hibernate OGM specific behaviors regarding these APIs. If you need to learn JPA or the native Hibernate APIs, check out the Hibernate ORM documentation.

We already discussed this subject earlier, have a look at Chapter 4, Configure and start Hibernate OGM for all the details.

As a reminder, it basically boils down to either:

  • set the right persistence provider in your persistence.xml file and create an EntityManagerFactory the usual way
  • start via the Hibernate ORM native APIs using StandardServiceRegistryBuilder and MetadataSources to boot a SessionFactory

You know of the Java Persistence and Hibernate ORM native APIs? You are pretty much good to go. If you need a refresher, make sure you read the Hibernate ORM documentation.

A few things are a bit different though, let’s discuss them.

Most of the EntityManager and Session contracts are supported. Here are the few exceptions:

  • Session.createCriteria: criteria queries are not yet supported in Hibernate OGM
  • Session.createFilter: queries on collections are not supported yet
  • Session 's enableFilter, disableFilter etc: query filters are not supported at the moment
  • doWork and doReturningWork are not implemented as they rely on JDBC connections - see OGM-694
  • Session 's stored procedure APIs are not supported
  • Session 's natural id APIs are not yet supported
  • Session.lock is not fully supported at this time
  • EntityManager 's criteria query APIs are not supported
  • EntityManager 's stored procedure APIs are not supported - see OGM-695
  • EntityManager.lock is not fully supported at this time
  • see Chapter 7, Query your entities to know what is supported for JP-QL and native queries

Even though some underlying NoSQL datastores do not support transaction, it is important to demarcate transaction via the Hibernate OGM APIs. Let’s see why.

Hibernate does pile up changes for as long as it can before pushing them down to the datastore. This opens up the doors to huge optimizations (avoiding duplication, batching operations etc). You can force changes to be sent to the datastore by calling Session.flush or EntityManager.flush. In some situations - for example before some queries are executed -, Hibernate will flush automatically. It will also flush when the transaction demarcation happens (whether there is a real transaction or not).

The best approach is to always demarcate the transaction as shown below. This avoids the needs to manually call flush and will offer future opportunities for Hibernate OGM.


If an error occurs during flushing a set of changes, some data changes may already have been applied in the datastore. If the store is non-transactional, there is no way to rollback (undo) these changes if they were already flushed. In this case it is desirable to know which changes have been applied and which ones failed in order to take appropriate action.

Hibernate OGM provides an error compensation API for this purpose. By implementing the org.hibernate.ogm.failure.ErrorHandler interface, you will be notified if

Use cases for the error compensation API include:

In its current form the API lays the ground for manually performing these and similar tasks, but we envision a more automated approach in future versions, e.g. for automatic retries of failed operations or the automatic application of compensating operations.

Let’s take a look at an example:


The onRollback() method - which is called when the transaction is rolled back (either by the user or by the container) - shows how to iterate over all methods applied prior to the rollback, examine their specific type and e.g. write them to a log file.

The onFailedGridDialectOperation() method is called for each specific datastore operation failing. It lets you decide whether to continue ignoring the failure, retry or abort the operation. If ABORT is returned, the causing exception will be re-thrown, eventually causing the current transaction to be rolled back. If CONTINUE is returned, that exception will be ignored, causing the current transaction to continue.

The decision whether to abort or continue can be based on the specific exception type or on the grid dialect operation which caused the failure. In the example all exceptions of type TupleAlreadyExistsException are ignored, whereas all other exceptions cause the current flush cycle to be aborted. You also could react to datastore-specific exceptions such as MongoDB’s MongoTimeoutException, if needed.

Note that by extending the provided base class BaseErrorHandler rather than implementing the interface directly, you only need to implement those callback methods you are actually interested in. The implementation will also not break if further callback methods are added to the ErrorHandler interface in future releases.

Having implemented the error handler, it needs to be registered with Hibernate OGM. To do so, specify it using the property hibernate.ogm.error_handler, e.g. as a persistence unit property in META-INF/persistence.xml:

<property name="hibernate.ogm.error_handler" value="com.example.ExampleErrorHandler"/>

Some of the Hibernate OGM public contracts are geared towards either integrators or implementors of datastore providers. They should not be used by a regular application. These contracts are named SPIs and are in a .spi package.

To keep improving Hibernate OGM, we might break these SPIs between versions. If you plan on writing a datastore, come and talk to us.