This sample application demonstrates using Spring Data JPA with Hibernate and Google Cloud Spanner.
This sample by default runs on the Spanner emulator:
-
Start the Spanner emulator with either
gcloud emulators spanner start
ordocker run -p 9010:9010 -p 9020:9020 gcr.io/cloud-spanner-emulator/emulator
. See https://cloud.google.com/spanner/docs/emulator#install for more information on installing and running the emulator. -
Open a terminal instance inside this directory and run
mvn spring-boot:run
to run the sample application.
-
Create a Google Cloud Platform Project
-
Create a service account with Cloud Spanner permission. Furnish a new JSON key and then set the credentials using the
GOOGLE_APPLICATION_CREDENTIALS
environment variable.Alternatively, have the Google Cloud SDK installed and initialized and logged in with application default credentials.
-
Enable the Cloud Spanner API.
-
In the Spanner Cloud Console UI, create a Cloud Spanner instance and database and insert those details into the
spanner.project
,spanner.instance
andspanner.database
defined in the filesrc/main/resources/application.properties
. Also setspanner.emulator=false
in the same file. These settings configure the connection URL which specifies the database that your application will connect to. -
Open a terminal instance inside this directory and run
mvn spring-boot:run
to run the sample application.In the
application.properties
file, you’ll see that the application is running withspring.jpa.hibernate.ddl-auto=update
which allows Hibernate to create tables for you if they do not exist. On the very first time you run the app, Hibernate will automatically create the schema and missing tables based on the@Entity
definitions.You can view the data that was populated in your Cloud Spanner database by navigating to your database in the Spanner Console view.
This sample application showcases how to use the following features:
-
How to use the emulator for local development. See the application.properties file for an example of how this is set up.
-
Use auto-generated UUIDs as primary keys. See the AbstractNonInterleavedEntity.java entity for an example.
-
Use a bit-reversed sequence to generate a numerical primary key value. See the TicketSale.java entity for an example.
-
Use interleaved tables. See Track.java for an example of an entity that is interleaved in a parent table.
-
Execute multiple DML statements as a single batch on Spanner. This is achieved by using the
auto_batch_dml
flag in the Spanner JDBC driver. See BatchService.java for an example. -
Execute read-only transactions. See SingerService.java for an example.
-
Execute a stale read. See StaleReadService.java for an example.
-
Add transaction tags to read/write transactions. See the
generateRandomVenues
method in VenueService.java for an example of how to set a transaction tag. Note that you also need to add aTransactionTagInterceptor
to your Hibernate configuration. See TaggingHibernatePropertiesCustomizer.java for how this is done in this sample application. -
Add statement tags to generated queries. See the
searchByLastNameStartsWith
method in SingerRepository.java for an example of how to add a statement tag. -
Add statement and query hints to generated queries. See the
findByActive
method in the SingerRepository.java file for an example.