Integrate Spanner with Spring Data JPA (GoogleSQL dialect)

Spring Data JPA, part of the larger Spring Data family, makes iteasier to implement JPA based repositories. Spring Data JPAsupports Spannerand a wide range of other database systems. It adds an abstraction layer betweenyour application and your database that makes your application easier to portfrom one database system to another.

Set up Spring Data JPA for Spanner GoogleSQL-dialect databases

You can integrate Spanner GoogleSQL-dialect databases withSpring Data JPA using the open sourceSpannerHibernate Dialect (SpannerDialect).

To see an example, refer to the fullworking sample application on GitHub.

Dependencies

In your project, add Apache Maven dependencies for Spring Data JPA,SpannerHibernate Dialect, and theSpanner officially supportedOpen Source JDBC driver.

<dependencies><!--SpringDataJPA--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!--HibernateDialectandJDBCDriverdependencies--><dependency><groupId>com.google.cloud</groupId><artifactId>google-cloud-spanner-hibernate-dialect</artifactId></dependency><dependency><groupId>com.google.cloud</groupId><artifactId>google-cloud-spanner-jdbc</artifactId></dependency></dependencies>

Configuration

Configureapplication.properties to use the SpannerHibernate Dialect and the Spanner JDBC Driver.

#SpannerconnectionURL.#-${PROJECT_ID}Replacewithyour Google CloudprojectID#-${INSTANCE_ID}ReplacewithyourSpannerinstanceID#-${DATABASE_NAME}ReplacewiththenameofyourSpannerdatabasethatyoucreatedinsideyourSpannerinstancespring.datasource.url=jdbc:cloudspanner:/projects/${PROJECT_ID}/instances/${INSTANCE_ID}/databases/${DATABASE_NAME}#SpecifytheSpannerJDBCdriver.spring.datasource.driver-class-name=com.google.cloud.spanner.jdbc.JdbcDriver#SpecifytheSpannerHibernatedialect.spring.jpa.properties.hibernate.dialect=com.google.cloud.spanner.hibernate.SpannerDialectspring.jpa.hibernate.ddl-auto=update#Settingstoenablebatchingstatementsforefficiencyspring.jpa.properties.hibernate.jdbc.batch_size=100spring.jpa.properties.hibernate.order_inserts=true#YoucandisplaySQLstatementsandstatsfordebuggingifneeded.spring.jpa.properties.hibernate.show_sql=truespring.jpa.properties.hibernate.format_sql=true

Full Sample Application

Aworking sample application is available onGitHub.

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.