Hibernate ORM with Cloud Spanner

You can use Hibernate ORM with Cloud Spanner using the open source Cloud Spanner Dialect (SpannerDialect). Cloud Spanner Dialect is compatible with Hibernate ORM 5.4 for Google Cloud Spanner. Cloud Spanner Dialect produces SQL, DML, and DDL statements for most common entity types and relationships using standard Hibernate and Java Persistence annotations.

Setting up Hibernate ORM with Cloud Spanner

Add the Apache Maven dependencies for Hibernate ORM core, Cloud Spanner Dialect, and the Cloud Spanner officially supported Open Source JDBC driver.

  <!-- The Hibernate dialect for Spanner dependency -->

  <!-- The Spanner JDBC driver dependency -->

  <!-- Hibernate core dependency -->

Configure hibernate.cfg.xml to use Cloud Spanner Dialect and Cloud Spanner JDBC Driver.

<!-- Connection settings -->
<property name="hibernate.dialect">com.google.cloud.spanner.hibernate.SpannerDialect</property>
<property name="hibernate.connection.driver_class">com.google.cloud.spanner.jdbc.JdbcDriver</property>
<property name="hibernate.connection.url">jdbc:cloudspanner:/projects/{YOUR_PROJECT_ID}/instances/{YOUR_INSTANCE_ID}/databases/{YOUR_DATABASE_ID}</property>

The service account JSON credentials file location should be in the GOOGLE_APPLICATION_CREDENTIALS environment variable. The driver will use default credentials set in the Google Cloud SDK gcloud application otherwise.

Using Hibernate ORM with Cloud Spanner

For more information about the features and recommendations for Hibernate ORM with Cloud Spanner, please consult the reference documentation on GitHub.

What's next