Although there has been support for SAP HANA in Hibernate for some time, the respective dialects implemented only the basic features and there was no official support from SAP.

Several features like, for example, database-generated identity columns and table comments were not available.

The recent Hibernate ORM release 5.2.12 comes with an improved support for SAP HANA that allows the use of all features provided by the Hibernate ORM framework.

This release comes with full support from the SAP HANA team. We will keep updating this open source project to deliver more advanced features.

Why should I use the SAP HANA platform?

In short, with SAP HANA in-memory computing power, any project using a database can now push more advanced calculations down to database layer with following benefits:

  1. SAP HANA offers powerful advanced features like real-time analytics, full-text search and analysis, geospatial data processing, graph processing, and a JSON document store. Using these multi-modal processing capabilities you can build applications that perform all required data processing directly in the database close to the data without the need for external components, data transfers between application layers, or data replication.

  2. Due to SAP HANA’s columnar in-memory data store, the data footprint of an application can be significantly reduced by removing all redundant data like materialized intermediate views or secondary indexes. Also, SAP HANA internally compresses the stored data resulting in an even smaller size of the database.

  3. SAP HANA in-memory technology enables transactions and analytics on a single data set, simplifying data landscapes and enabling advanced multi-model data processing on a single platform. With the Hibernate dialect for SAP HANA, Java applications can now also take advantage of these benefits.

What’s new?

Database-generated identity columns

The SAP HANA dialects now support the IDENTITY generation type for generated values.

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

On database-level this translates to a column with the GENERATED BY DEFAULT AS IDENTITY attribute.

"ID" INTEGER CS_INT GENERATED BY DEFAULT AS IDENTITY

Improved handling of LOB columns

The behavior of the HANA JDBC driver regarding LOBs deviates from the JDBC specification in that LOB objects (Blob, Clob, NClob) don’t remain valid until the end of the transaction, but are closed when the corresponding ResultSet is closed. Since Hibernate relies on the JDBC specification this leads to unexpected errors when working with LOBs on SAP HANA.

A workaround for this issue implemented by the current SAP HANA Hibernate dialect is to materialize all LOB objects and cache them on the application side before the ResultSet is closed. A drawback of this approach is that it is not possible to stream LOB data from the server on demand and materializing all LOBs leads to increased memory usage on the application side, especially when working with large LOBs. A developer needs to be aware of this fact when designing the application, but at least it should be a reasonable solution in most use cases.

The behavior of the caching mechanism can be controlled via the Hibernate parameter hibernate.dialect.hana.max_lob_prefetch_size. The value of this parameter should correspond to the value of the SAP HANA configuration parameter indexserver.ini/session/max_lob_prefetch_size. All LOBs greater than this value will be cached by the SAP HANA Hibernate dialect.

SAP HANA Spatial dialect

The Hibernate version 5.2.12 also introduces a SpatialDialect implementation for SAP HANA. This dialect enables the use of the SAP HANA geospatial capabilities when using the Hibernate Spatial module. To use it specify the HANA Spatial dialect in your persistence configuration:

<property name="hibernate.dialect" value="org.hibernate.spatial.dialect.hana.HANASpatialDialect"/>

Then you can run use geometry types in your entities and run spatial queries against them:

Entity definition
@Entity
public class Geography {
        private Point<C2D> position;
}
Run a query
// Select all events within a region
TypedQuery<Event> eventQuery = entityManager.createQuery(
                "select e from Event e where within(actionGeo.position, :area) = true",
                Event.class);

// Create a polygon describing a box around Europe
WktDecoder decoder = Wkt.newDecoder(Wkt.Dialect.HANA_EWKT);
Polygon<?> europeBox = (Polygon<?>) decoder.decode("POLYGON((35 -10, 35 30, 71 30, 71 -10, 35 -10))");
eventQuery.setParameter("area", europeBox);

// Return all events within the Europe box
return eventQuery.getResultList();

Table and column comments

The current SAP HANA Hibernate dialect now supports table and column comments.

@Entity
@Table(comment = "Table comment", appliesTo = "Post")
public class Post {
        @Id
        private int id;
}

On database-level this translates to a “comment” attribute at the end of the table create statement.

create column table Post (id integer generated by default as identity) comment 'Table comment'

Locking improvements

The previous implementation of the SAP HANA Hibernate dialect didn’t always generate the correct SQL statements when using locks. This has been fixed to work correctly in all situations.

JDBC driver improvements

Some features of Hibernate ORM rely on functionality that was not implemented by the SAP HANA JDBC driver. The missing functionality has been added in the meantime and has been released with the SAP HANA JDBC driver version 2.2.1. So if you plan on using Hibernate on SAP HANA be sure to use at least this version of the SAP HANA JDBC driver.

What’s next?

Since there isn’t much more to be done for the SAP HANA Hibernate dialect and the SAP HANA Hibernate spatial dialect, we’re currently investigating how to best leverage the advanced features of SAP HANA.

One area we’re investigating is how to integrate the SAP HANA full-text search and text analysis capabilities with Hibernate via the Hibernate Search project.

Another area we’re looking into is how to integrate the SAP HANA NoSQL capabilities like the SAP HANA document store and the SAP HANA graph engine into Hibernate via the Hibernate OGM project.

Stay tuned for updates on these topics.

How can I get started?

If you want to try out Hibernate on SAP HANA you can get a free version of SAP HANA, Express Edition. Simply go to http://sap.com/sap-hana-express, fill out the registration form and either download the SAP HANA, Express Edition binaries or setup a SAP HANA, Express Edition instance with your favorite cloud provider.

If you need more information on how to get started check out the information at www.sap.com/developer/topics/sap-hana-express.html. Here you can find tutorials which guide you through various topics related to SAP HANA, Express Edition.


Back to top