Integra Spanner con Spring Data JPA (dialecto de PostgreSQL)
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Spring Data JPA, que forma parte de la gran familia de Spring Data, facilita la implementación de repositorios basados en JPA. Spring Data JPA admite PostgreSQL y una amplia variedad de otros sistemas de bases de datos. Agrega una capa de abstracción entre tu aplicación y tu base de datos que facilita el portabilidad de tu aplicación de un sistema de base de datos a otro.
Configura Spring Data JPA para bases de datos de dialecto PostgreSQL de Spanner
Puedes integrar bases de datos de dialecto PostgreSQL de Spanner con Spring Data JPA con el dialecto estándar de Hibernate de PostgreSQL y PGAdapter.
Agrega el siguiente método a tu aplicación para iniciar PGAdapter directamente desde tu aplicación de Java. PGAdapter se ejecuta en la misma JVM que tu aplicación, y la aplicación se conecta a PGAdapter en localhost:port.
/** Starts PGAdapter in-process and returns a reference to the server. */staticProxyServerstartPGAdapter(){// Start PGAdapter using the default credentials of the runtime environment on port 9432.OptionsMetadataoptions=OptionsMetadata.newBuilder().setPort(9432).build();ProxyServerserver=newProxyServer(options);server.startServer();server.awaitRunning();returnserver;}
Configuración
Configura application.properties para usar el dialecto de Hibernate de PostgreSQL y el controlador JDBC de PostgreSQL. Configura el controlador JDBC de PostgreSQL para conectarte a una base de datos de dialecto PostgreSQL a través de PGAdapter.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 2025-09-05 (UTC)"],[],[],null,["# Integrate Spanner with Spring Data JPA (PostgreSQL dialect)\n\nSpring Data JPA, part of the larger Spring Data family, makes it easier to\nimplement JPA based repositories. Spring Data JPA supports PostgreSQL\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 PostgreSQL-dialect databases\n---------------------------------------------------------------\n\nYou can integrate Spanner PostgreSQL-dialect databases with Spring Data JPA\nusing the standard PostgreSQL Hibernate dialect and\n[PGAdapter](/spanner/docs/pgadapter).\n\nTo see an example, refer to the full\n[working sample application](https://github.com/GoogleCloudPlatform/pgadapter/blob/-/samples/java/spring-data-jpa) on\nGitHub.\n\n### Dependencies\n\nIn your project, add Apache Maven dependencies for [Spring Data JPA](https://spring.io/projects/spring-data-jpa),\nthe [PostgreSQL JDBC driver](https://github.com/pgjdbc/pgjdbc), and [PGAdapter](/spanner/docs/pgadapter). \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!-- Add the PostgreSQL JDBC driver --\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003eorg.postgresql\u003c/groupId\u003e\n \u003cartifactId\u003epostgresql\u003c/artifactId\u003e\n \u003c/dependency\u003e\n \u003c!-- Add PGAdapter as a dependency, so we can start it in-process --\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.cloud\u003c/groupId\u003e\n \u003cartifactId\u003egoogle-cloud-spanner-pgadapter\u003c/artifactId\u003e\n \u003c/dependency\u003e\n \u003c/dependencies\u003e\n\n### Start PGAdapter in-process\n\nAdd the following method to your application to start PGAdapter\ndirectly from your Java application. PGAdapter runs in the same JVM as\nyour application, and the application connects to PGAdapter on\n`localhost:port`. \n\n /** Starts PGAdapter in-process and returns a reference to the server. */\n static ProxyServer startPGAdapter() {\n // Start PGAdapter using the default credentials of the runtime environment on port 9432.\n OptionsMetadata options = OptionsMetadata.newBuilder().setPort(9432).build();\n ProxyServer server = new ProxyServer(options);\n server.startServer();\n server.awaitRunning();\n\n return server;\n }\n\n### Configuration\n\nConfigure `application.properties` to use the PostgreSQL Hibernate\nDialect and the PostgreSQL JDBC Driver. Configure the PostgreSQL\nJDBC Driver to connect to a PostgreSQL-dialect database through PGAdapter. \n\n # The example uses the standard PostgreSQL Hibernate dialect.\n spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect\n\n # Defining these properties here makes it a bit easier to build the connection string.\n # Change these to match your Cloud Spanner PostgreSQL-dialect database.\n spanner.project=my-project\n spanner.instance=my-instance\n spanner.database=my-database\n # This setting ensures that PGAdapter automatically commits the current transaction if it encounters\n # a DDL statement in a read/write transaction, and then executes the DDL statements as a single DDL\n # batch. \n spanner.ddl_transaction_mode=options=-c%20spanner.ddl_transaction_mode=AutocommitExplicitTransaction\n\n # This is the connection string to PGAdapter running in-process.\n spring.datasource.url=jdbc:postgresql://localhost:9432/projects%2F${spanner.project}%2Finstances%2F${spanner.instance}%2Fdatabases%2F${spanner.database}?${spanner.ddl_transaction_mode}\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\n # Enable JDBC batching.\n spring.jpa.properties.hibernate.jdbc.batch_size=100\n spring.jpa.properties.hibernate.order_inserts=true\n\nFull Sample Application\n-----------------------\n\nA [working sample application](https://github.com/GoogleCloudPlatform/pgadapter/blob/-/samples/java/spring-data-jpa) 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 [PGAdapter](https://github.com/GoogleCloudPlatform/pgadapter) on GitHub.\n- [File a GitHub issue](https://github.com/GoogleCloudPlatform/pgadapter/issues) to report a bug or ask a question about PGAdapter.\n- [Integrate Spanner with Spring Data JPA (GoogleSQL dialect)](/spanner/docs/use-spring-data-jpa)."]]