Integrate Spanner with Hibernate ORM (GoogleSQL dialect)

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

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

Set up Hibernate

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

<dependencies><!--TheSpannerJDBCdriverdependency--><dependency><groupId>com.google.cloud</groupId><artifactId>google-cloud-spanner-jdbc</artifactId></dependency><!--Hibernatecoredependency--><dependency><groupId>org.hibernate.orm</groupId><artifactId>hibernate-core</artifactId><version>6.4.4.Final</version></dependency></dependencies>

Configurehibernate.cfg.xml to use Spanner Dialect andSpanner JDBC Driver.

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

Theservice account JSON credentials file locationshould be in theGOOGLE_APPLICATION_CREDENTIALS environment variable. Thedriver will use default credentials set in the Google Cloud CLIgcloudapplication otherwise.

Use Hibernate with Spanner GoogleSQL

For more information about the features and recommendations forHibernate, consult thereference documentation on GitHub.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.