Use Hibernate to connect to Spanner Omni

Hibernate ORM is an object-relational mapping (ORM) 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).

This document describes how to configure Hibernate to connect to Spanner Omni. Hibernate integrates with Spanner Omni in the same way it integrates with Spanner. Spanner is compatible with Hibernate ORM 6.x. The Spanner dialect produces SQL, DML, and DDL statements for most common entity types and relationships using standard Hibernate and Java Persistence annotations.

Using Hibernate with Spanner Omni lets you use your existing Hibernate experience to interact with your databases. For more information, see Integrate Spanner with Hibernate ORM (Spanner dialect) in the Spanner documentation.

Prerequisites

To use Hibernate with Spanner Omni, include the following Maven dependencies in your project:

  • Spanner Hibernate dialect: Add the following to your pom.xml file. Use version 4.2.1 or later.

    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-spanner-hibernate-dialect</artifactId>
      <version>4.2.1</version>
    </dependency>
    
  • Spanner JDBC driver: Add the following to your pom.xml file. Use version 2.35.0 or later.

    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-spanner-jdbc</artifactId>
      <version>2.35.0</version>
    </dependency>
    

Configure Hibernate

Configure the SpannerDialect and Spanner driver class in the hibernate.properties file using standard Hibernate dialect conventions:

hibernate.dialect=com.google.cloud.spanner.hibernate.SpannerDialect
hibernate.connection.driver_class=com.google.cloud.spanner.jdbc.JdbcDriver
hibernate.connection.url=jdbc:spanner://ENDPOINT/databases/DATABASE_ID?isExperimentalHost=true

Update the hibernate.connection.url for the specific connection protocol (plain-text, TLS, or mTLS) that your Spanner Omni instance uses. For more information, see Establish a Spanner Omni connection. For secure modes, add the CA certificate to the Java truststore or pass it directly when you run the application, as described in the Java SDK TLS instructions.