Integrate Spanner with Spring Data JPA (GoogleSQL dialect)
Stay organized with collections
Save and categorize content based on your preferences.
Spring Data JPA, part of the larger Spring Data family, makes it
easier to implement JPA based repositories. Spring Data JPA
supports Spanner
and a wide range of other database systems. It adds an abstraction layer between
your application and your database that makes your application easier to port
from one database system to another.
Set up Spring Data JPA for Spanner GoogleSQL-dialect databases
You can integrate Spanner GoogleSQL-dialect databases with Spring Data JPA using the
open source Spanner Hibernate Dialect
(SpannerDialect).
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[],[],null,["# Integrate Spanner with Spring Data JPA (GoogleSQL dialect)\n\nSpring Data JPA, part of the larger Spring Data family, makes it\neasier to implement JPA based repositories. Spring Data JPA\nsupports Spanner\nand a wide range of other database systems. It adds an abstraction layer between\nyour application and your database that makes your application easier to port\nfrom one database system to another.\n\nSet up Spring Data JPA for Spanner GoogleSQL-dialect databases\n--------------------------------------------------------------\n\nYou can integrate Spanner GoogleSQL-dialect databases with Spring Data JPA using the\nopen source [Spanner Hibernate Dialect](https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate)\n(`SpannerDialect`).\n\nTo see an example, refer to the full\n[working sample application](https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate/blob/-/google-cloud-spanner-hibernate-samples/spring-data-jpa-full-sample) on\nGitHub.\n\n### Dependencies\n\nIn your project, add Apache Maven dependencies for [Spring Data JPA](https://spring.io/projects/spring-data-jpa),\n[Spanner Hibernate Dialect](https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate),\nand the Spanner officially supported [Open Source JDBC driver](/spanner/docs/use-oss-jdbc). \n\n \u003cdependencies\u003e\n \u003c!-- Spring Data JPA --\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n \u003cartifactId\u003espring-boot-starter-data-jpa\u003c/artifactId\u003e\n \u003c/dependency\u003e\n \u003c!-- Hibernate Dialect and JDBC Driver dependencies--\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.cloud\u003c/groupId\u003e\n \u003cartifactId\u003egoogle-cloud-spanner-hibernate-dialect\u003c/artifactId\u003e\n \u003c/dependency\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.cloud\u003c/groupId\u003e\n \u003cartifactId\u003egoogle-cloud-spanner-jdbc\u003c/artifactId\u003e\n \u003c/dependency\u003e\n \u003c/dependencies\u003e\n\n### Configuration\n\nConfigure `application.properties` to use the Spanner Hibernate\nDialect and the Spanner JDBC Driver. \n\n # Spanner connection URL.\n # - ${PROJECT_ID} Replace with your GCP project ID\n # - ${INSTANCE_ID} Replace with your Spanner instance ID\n # - ${DATABASE_NAME} Replace with the name of your Spanner database that you created inside your Spanner instance\n\n spring.datasource.url=jdbc:cloudspanner:/projects/${PROJECT_ID}/instances/${INSTANCE_ID}/databases/${DATABASE_NAME}\n\n # Specify the Spanner JDBC driver.\n spring.datasource.driver-class-name=com.google.cloud.spanner.jdbc.JdbcDriver\n\n # Specify the Spanner Hibernate dialect.\n spring.jpa.properties.hibernate.dialect=com.google.cloud.spanner.hibernate.SpannerDialect\n\n spring.jpa.hibernate.ddl-auto=update\n\n # Settings to enable batching statements for efficiency\n spring.jpa.properties.hibernate.jdbc.batch_size=100\n spring.jpa.properties.hibernate.order_inserts=true\n\n # You can display SQL statements and stats for debugging if needed.\n spring.jpa.properties.hibernate.show_sql=true\n spring.jpa.properties.hibernate.format_sql=true\n\nFull Sample Application\n-----------------------\n\nA [working sample application](https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate/blob/-/google-cloud-spanner-hibernate-samples/spring-data-jpa-full-sample) is\navailable on GitHub.\n\nWhat's next\n-----------\n\n- Learn more about [Spring Data JPA](https://spring.io/projects/spring-data-jpa).\n- Learn more about [Hibernate ORM](https://hibernate.org/orm/).\n- View the repository for [Spanner Dialect](https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate) on GitHub.\n- [File a GitHub issue](https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate/issues) to report a bug or ask a question about Hibernate.\n- [Integrate Spanner with Spring Data JPA (PostgreSQL dialect)](/spanner/docs/use-spring-data-jpa-postgresql)."]]