Intégrer Spanner à Spring Data JPA (dialecte PostgreSQL)
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Spring Data JPA, qui fait partie de la grande famille Spring Data, facilite l'implémentation de dépôts basés sur JPA. Spring Data JPA est compatible avec PostgreSQL et un large éventail d'autres systèmes de base de données. Il ajoute une couche d'abstraction entre votre application et votre base de données, ce qui facilite le transfert de votre application d'un système de base de données à un autre.
Configurer Spring Data JPA pour les bases de données Spanner en dialecte PostgreSQL
Vous pouvez intégrer des bases de données Spanner en dialecte PostgreSQL à Spring Data JPA à l'aide du dialecte PostgreSQL Hibernate standard et de PGAdapter.
Ajoutez la méthode suivante à votre application pour démarrer PGAdapter directement à partir de votre application Java. PGAdapter s'exécute dans la même JVM que votre application, et l'application se connecte à PGAdapter sur 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;}
Configuration
Configurez application.properties pour qu'il utilise le dialecte PostgreSQL Hibernate et le pilote JDBC PostgreSQL. Configurez le pilote JDBC PostgreSQL pour vous connecter à une base de données en dialecte PostgreSQL via PGAdapter.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/05 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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)."]]