Spanner in Spring Data JPA (PostgreSQL-Dialekt) einbinden
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Spring Data JPA, Teil der größeren Spring Data-Familie, erleichtert die Implementierung von JPA-basierten Repositories. Spring Data JPA unterstützt PostgreSQL und eine Vielzahl anderer Datenbanksysteme. Es fügt eine Abstraktionsschicht zwischen Ihrer Anwendung und Ihrer Datenbank hinzu, die die Portierung Ihrer Anwendung von einem Datenbanksystem in ein anderes erleichtert.
Spring Data JPA für Spanner-Datenbanken mit PostgreSQL-Dialekt einrichten
Sie können Spanner-Datenbanken im PostgreSQL-Dialekt mit Spring Data JPA mithilfe des standardmäßigen PostgreSQL-Hibernate-Dialekts und PGAdapter integrieren.
Fügen Sie Ihrer Anwendung die folgende Methode hinzu, um PGAdapter direkt über Ihre Java-Anwendung zu starten. PGAdapter wird in derselben JVM wie Ihre Anwendung ausgeführt und die Anwendung stellt eine Verbindung zu PGAdapter auf localhost:port her.
/** 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;}
Konfiguration
Konfigurieren Sie application.properties für die Verwendung des PostgreSQL Hibernate-Dialekts und des PostgreSQL JDBC-Treibers. Konfigurieren Sie den PostgreSQL-JDBC-Treiber so, dass eine Verbindung über PGAdapter zu einer Datenbank mit PostgreSQL-Dialekt hergestellt wird.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 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)."]]