Integrate Spanner with Hibernate ORM (GoogleSQL dialect)

Hibernate is an object-relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database.

You can integrate GoogleSQL-dialect databases with Hibernate using the open source Spanner Dialect (SpannerDialect). Spanner is compatible with Hibernate ORM 6.x. Spanner Dialect produces SQL, DML, and DDL statements for most common entity types and relationships using standard Hibernate and Java Persistence annotations.

Set up Hibernate

In your project, add Apache Maven dependencies for Hibernate ORM core, Spanner Dialect, and the Spanner officially supported Open Source JDBC driver.

<dependencies>
  <!-- The Spanner JDBC driver dependency -->
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-spanner-jdbc</artifactId>
  </dependency>

  <!-- Hibernate core dependency -->
  <dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>6.4.4.Final</version>
  </dependency>
</dependencies>

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

<!-- Connection settings -->
<property name="hibernate.dialect">org.hibernate.dialect.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 CLI gcloud application otherwise.

Use Hibernate with Spanner GoogleSQL

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

What's next