대규모 Spring Data 계열의 일부인 Spring Data JPA를 사용하면 JPA 기반 저장소를 더욱 쉽게 구현할 수 있습니다. Spring Data JPA는 PostgreSQL 및 다양한 데이터베이스 시스템을 지원합니다. 애플리케이션이 한 데이터베이스 시스템에서 다른 데이터베이스 시스템으로 쉽게 포팅할 수 있게 하는 데이터베이스와 애플리케이션 사이에 추상화 레이어를 추가합니다.
Spanner PostgreSQL 언어 데이터베이스용 Spring Data JPA 설정
표준 PostgreSQL Hibernate 언어 및 PGAdapter를 사용하여 Spanner PostgreSQL 언어 데이터베이스와 Spring Data JPA를 통합할 수 있습니다.
애플리케이션에 다음 메서드를 추가하여 자바 애플리케이션에서 직접 PGAdapter를 시작합니다. PGAdapter는 애플리케이션과 동일한 JVM에서 실행되며 애플리케이션은 localhost:port의 PGAdapter에 연결됩니다.
/** 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;}
구성
PostgreSQL Hibernate 언어 및 PostgreSQL JDBC 드라이버를 사용하려면 application.properties를 구성합니다. PGAdapter를 통해 PostgreSQL 언어 데이터베이스에 연결할 PostgreSQL JDBC 드라이버를 구성합니다.
[[["이해하기 쉬움","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(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)."]]