[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-05 (世界標準時間)。"],[],[],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)."]]