Spring Data JPA, parte de la familia Spring Data, facilita la implementación de repositorios basados en JPA. Spring Data JPA es compatible con PostgreSQL y una amplia gama de otros sistemas de bases de datos. Añade una capa de abstracción entre la aplicación y la base de datos, lo que facilita la migración de la aplicación de un sistema de bases de datos a otro.
Configurar Spring Data JPA para bases de datos Spanner PostgreSQL-dialect
Puede integrar bases de datos del dialecto PostgreSQL de Spanner con Spring Data JPA utilizando el dialecto Hibernate PostgreSQL estándar y PGAdapter .
Para ver un ejemplo, consulte la aplicación de muestra de funcionamiento completo en GitHub.
Dependencias
En su proyecto, agregue dependencias de Apache Maven para Spring Data JPA , el controlador JDBC de PostgreSQL y PGAdapter .
<dependencies>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Add the PostgreSQL JDBC driver -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- Add PGAdapter as a dependency, so we can start it in-process -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner-pgadapter</artifactId>
</dependency>
</dependencies>
Iniciar PGAdapter en proceso
Agregue el siguiente método a su aplicación para iniciar PGAdapter directamente desde su aplicación Java. PGAdapter se ejecuta en la misma JVM que su aplicación y esta se conecta a PGAdapter en localhost:port
.
/** Starts PGAdapter in-process and returns a reference to the server. */
static ProxyServer startPGAdapter() {
// Start PGAdapter using the default credentials of the runtime environment on port 9432.
OptionsMetadata options = OptionsMetadata.newBuilder().setPort(9432).build();
ProxyServer server = new ProxyServer(options);
server.startServer();
server.awaitRunning();
return server;
}
Configuración
Configure application.properties
para usar el dialecto Hibernate de PostgreSQL y el controlador JDBC de PostgreSQL. Configure el controlador JDBC de PostgreSQL para conectarse a una base de datos con dialecto PostgreSQL mediante PGAdapter.
# The example uses the standard PostgreSQL Hibernate dialect.
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
# Defining these properties here makes it a bit easier to build the connection string.
# Change these to match your Cloud Spanner PostgreSQL-dialect database.
spanner.project=my-project
spanner.instance=my-instance
spanner.database=my-database
# This setting ensures that PGAdapter automatically commits the current transaction if it encounters
# a DDL statement in a read/write transaction, and then executes the DDL statements as a single DDL
# batch.
spanner.ddl_transaction_mode=options=-c%20spanner.ddl_transaction_mode=AutocommitExplicitTransaction
# This is the connection string to PGAdapter running in-process.
spring.datasource.url=jdbc:postgresql://localhost:9432/projects%2F${spanner.project}%2Finstances%2F${spanner.instance}%2Fdatabases%2F${spanner.database}?${spanner.ddl_transaction_mode}
# You can display SQL statements and stats for debugging if needed.
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
# Enable JDBC batching.
spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.order_inserts=true
Solicitud de muestra completa
Hay una aplicación de muestra funcional disponible en GitHub.
¿Qué sigue?
- Obtenga más información sobre Spring Data JPA .
- Obtenga más información sobre Hibernate ORM .
- Ver el repositorio de PGAdapter en GitHub.
- Presenta un problema en GitHub para informar un error o hacer una pregunta sobre PGAdapter.
- Integrar Spanner con Spring Data JPA (dialecto GoogleSQL) .
Spring Data JPA, parte de la familia Spring Data, facilita la implementación de repositorios basados en JPA. Spring Data JPA es compatible con PostgreSQL y una amplia gama de otros sistemas de bases de datos. Añade una capa de abstracción entre la aplicación y la base de datos, lo que facilita la migración de la aplicación de un sistema de bases de datos a otro.
Configurar Spring Data JPA para bases de datos Spanner PostgreSQL-dialect
Puede integrar bases de datos del dialecto PostgreSQL de Spanner con Spring Data JPA utilizando el dialecto Hibernate PostgreSQL estándar y PGAdapter .
Para ver un ejemplo, consulte la aplicación de muestra de funcionamiento completo en GitHub.
Dependencias
En su proyecto, agregue dependencias de Apache Maven para Spring Data JPA , el controlador JDBC de PostgreSQL y PGAdapter .
<dependencies>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Add the PostgreSQL JDBC driver -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- Add PGAdapter as a dependency, so we can start it in-process -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner-pgadapter</artifactId>
</dependency>
</dependencies>
Iniciar PGAdapter en proceso
Agregue el siguiente método a su aplicación para iniciar PGAdapter directamente desde su aplicación Java. PGAdapter se ejecuta en la misma JVM que su aplicación y esta se conecta a PGAdapter en localhost:port
.
/** Starts PGAdapter in-process and returns a reference to the server. */
static ProxyServer startPGAdapter() {
// Start PGAdapter using the default credentials of the runtime environment on port 9432.
OptionsMetadata options = OptionsMetadata.newBuilder().setPort(9432).build();
ProxyServer server = new ProxyServer(options);
server.startServer();
server.awaitRunning();
return server;
}
Configuración
Configure application.properties
para usar el dialecto Hibernate de PostgreSQL y el controlador JDBC de PostgreSQL. Configure el controlador JDBC de PostgreSQL para conectarse a una base de datos con dialecto PostgreSQL mediante PGAdapter.
# The example uses the standard PostgreSQL Hibernate dialect.
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
# Defining these properties here makes it a bit easier to build the connection string.
# Change these to match your Cloud Spanner PostgreSQL-dialect database.
spanner.project=my-project
spanner.instance=my-instance
spanner.database=my-database
# This setting ensures that PGAdapter automatically commits the current transaction if it encounters
# a DDL statement in a read/write transaction, and then executes the DDL statements as a single DDL
# batch.
spanner.ddl_transaction_mode=options=-c%20spanner.ddl_transaction_mode=AutocommitExplicitTransaction
# This is the connection string to PGAdapter running in-process.
spring.datasource.url=jdbc:postgresql://localhost:9432/projects%2F${spanner.project}%2Finstances%2F${spanner.instance}%2Fdatabases%2F${spanner.database}?${spanner.ddl_transaction_mode}
# You can display SQL statements and stats for debugging if needed.
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
# Enable JDBC batching.
spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.order_inserts=true
Solicitud de muestra completa
Hay una aplicación de muestra funcional disponible en GitHub.
¿Qué sigue?
- Obtenga más información sobre Spring Data JPA .
- Obtenga más información sobre Hibernate ORM .
- Ver el repositorio de PGAdapter en GitHub.
- Presenta un problema en GitHub para informar un error o hacer una pregunta sobre PGAdapter.
- Integrar Spanner con Spring Data JPA (dialecto GoogleSQL) .
Spring Data JPA, parte de la familia Spring Data, facilita la implementación de repositorios basados en JPA. Spring Data JPA es compatible con PostgreSQL y una amplia gama de otros sistemas de bases de datos. Añade una capa de abstracción entre la aplicación y la base de datos, lo que facilita la migración de la aplicación de un sistema de bases de datos a otro.
Configurar Spring Data JPA para bases de datos Spanner PostgreSQL-dialect
Puede integrar bases de datos del dialecto PostgreSQL de Spanner con Spring Data JPA utilizando el dialecto Hibernate PostgreSQL estándar y PGAdapter .
Para ver un ejemplo, consulte la aplicación de muestra de funcionamiento completo en GitHub.
Dependencias
En su proyecto, agregue dependencias de Apache Maven para Spring Data JPA , el controlador JDBC de PostgreSQL y PGAdapter .
<dependencies>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Add the PostgreSQL JDBC driver -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- Add PGAdapter as a dependency, so we can start it in-process -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner-pgadapter</artifactId>
</dependency>
</dependencies>
Iniciar PGAdapter en proceso
Agregue el siguiente método a su aplicación para iniciar PGAdapter directamente desde su aplicación Java. PGAdapter se ejecuta en la misma JVM que su aplicación y esta se conecta a PGAdapter en localhost:port
.
/** Starts PGAdapter in-process and returns a reference to the server. */
static ProxyServer startPGAdapter() {
// Start PGAdapter using the default credentials of the runtime environment on port 9432.
OptionsMetadata options = OptionsMetadata.newBuilder().setPort(9432).build();
ProxyServer server = new ProxyServer(options);
server.startServer();
server.awaitRunning();
return server;
}
Configuración
Configure application.properties
para usar el dialecto Hibernate de PostgreSQL y el controlador JDBC de PostgreSQL. Configure el controlador JDBC de PostgreSQL para conectarse a una base de datos con dialecto PostgreSQL mediante PGAdapter.
# The example uses the standard PostgreSQL Hibernate dialect.
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
# Defining these properties here makes it a bit easier to build the connection string.
# Change these to match your Cloud Spanner PostgreSQL-dialect database.
spanner.project=my-project
spanner.instance=my-instance
spanner.database=my-database
# This setting ensures that PGAdapter automatically commits the current transaction if it encounters
# a DDL statement in a read/write transaction, and then executes the DDL statements as a single DDL
# batch.
spanner.ddl_transaction_mode=options=-c%20spanner.ddl_transaction_mode=AutocommitExplicitTransaction
# This is the connection string to PGAdapter running in-process.
spring.datasource.url=jdbc:postgresql://localhost:9432/projects%2F${spanner.project}%2Finstances%2F${spanner.instance}%2Fdatabases%2F${spanner.database}?${spanner.ddl_transaction_mode}
# You can display SQL statements and stats for debugging if needed.
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
# Enable JDBC batching.
spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.order_inserts=true
Solicitud de muestra completa
Hay una aplicación de muestra funcional disponible en GitHub.
¿Qué sigue?
- Obtenga más información sobre Spring Data JPA .
- Obtenga más información sobre Hibernate ORM .
- Ver el repositorio de PGAdapter en GitHub.
- Presenta un problema en GitHub para informar un error o hacer una pregunta sobre PGAdapter.
- Integrar Spanner con Spring Data JPA (dialecto GoogleSQL) .
Spring Data JPA, parte de la familia Spring Data, facilita la implementación de repositorios basados en JPA. Spring Data JPA es compatible con PostgreSQL y una amplia gama de otros sistemas de bases de datos. Añade una capa de abstracción entre la aplicación y la base de datos, lo que facilita la migración de la aplicación de un sistema de bases de datos a otro.
Configurar Spring Data JPA para bases de datos Spanner PostgreSQL-dialect
Puede integrar bases de datos del dialecto PostgreSQL de Spanner con Spring Data JPA utilizando el dialecto Hibernate PostgreSQL estándar y PGAdapter .
Para ver un ejemplo, consulte la aplicación de muestra de funcionamiento completo en GitHub.
Dependencias
En su proyecto, agregue dependencias de Apache Maven para Spring Data JPA , el controlador JDBC de PostgreSQL y PGAdapter .
<dependencies>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Add the PostgreSQL JDBC driver -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- Add PGAdapter as a dependency, so we can start it in-process -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner-pgadapter</artifactId>
</dependency>
</dependencies>
Iniciar PGAdapter en proceso
Agregue el siguiente método a su aplicación para iniciar PGAdapter directamente desde su aplicación Java. PGAdapter se ejecuta en la misma JVM que su aplicación y esta se conecta a PGAdapter en localhost:port
.
/** Starts PGAdapter in-process and returns a reference to the server. */
static ProxyServer startPGAdapter() {
// Start PGAdapter using the default credentials of the runtime environment on port 9432.
OptionsMetadata options = OptionsMetadata.newBuilder().setPort(9432).build();
ProxyServer server = new ProxyServer(options);
server.startServer();
server.awaitRunning();
return server;
}
Configuración
Configure application.properties
para usar el dialecto Hibernate de PostgreSQL y el controlador JDBC de PostgreSQL. Configure el controlador JDBC de PostgreSQL para conectarse a una base de datos con dialecto PostgreSQL mediante PGAdapter.
# The example uses the standard PostgreSQL Hibernate dialect.
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
# Defining these properties here makes it a bit easier to build the connection string.
# Change these to match your Cloud Spanner PostgreSQL-dialect database.
spanner.project=my-project
spanner.instance=my-instance
spanner.database=my-database
# This setting ensures that PGAdapter automatically commits the current transaction if it encounters
# a DDL statement in a read/write transaction, and then executes the DDL statements as a single DDL
# batch.
spanner.ddl_transaction_mode=options=-c%20spanner.ddl_transaction_mode=AutocommitExplicitTransaction
# This is the connection string to PGAdapter running in-process.
spring.datasource.url=jdbc:postgresql://localhost:9432/projects%2F${spanner.project}%2Finstances%2F${spanner.instance}%2Fdatabases%2F${spanner.database}?${spanner.ddl_transaction_mode}
# You can display SQL statements and stats for debugging if needed.
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
# Enable JDBC batching.
spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.order_inserts=true
Solicitud de muestra completa
Hay una aplicación de muestra funcional disponible en GitHub.
¿Qué sigue?
- Obtenga más información sobre Spring Data JPA .
- Obtenga más información sobre Hibernate ORM .
- Ver el repositorio de PGAdapter en GitHub.
- Presenta un problema en GitHub para informar un error o hacer una pregunta sobre PGAdapter.
- Integrar Spanner con Spring Data JPA (dialecto GoogleSQL) .