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.

<dependencies>
  <!-- The Hibernate dialect for Spanner dependency -->
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-spanner-hibernate-dialect</artifactId>
    <version>1.0.0</version>
  </dependency>

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

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

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

¿Te ha resultado útil esta página? Enviar comentarios:

Enviar comentarios sobre...

Cloud Spanner Documentation