Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Cloud Spanner Spring Data JPA with Hibernate Sample

This sample application demonstrates using Spring Data JPA with Hibernate and Google Cloud Spanner.

Running on the Emulator

This sample by default runs on the Spanner emulator:

  1. Start the Spanner emulator with either gcloud emulators spanner start or docker 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.

  2. Open a terminal instance inside this directory and run mvn spring-boot:run to run the sample application.

Setup & Configuration for Running on real Spanner

  1. Create a Google Cloud Platform Project

  2. 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.

  3. Enable the Cloud Spanner API.

  4. In the Spanner Cloud Console UI, create a Cloud Spanner instance and database and insert those details into the spanner.project, spanner.instance and spanner.database defined in the file src/main/resources/application.properties. Also set spanner.emulator=false in the same file. These settings configure the connection URL which specifies the database that your application will connect to.

  5. 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 with spring.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.

Important Features

This sample application showcases how to use the following features:

  1. How to use the emulator for local development. See the application.properties file for an example of how this is set up.

  2. Use auto-generated UUIDs as primary keys. See the AbstractNonInterleavedEntity.java entity for an example.

  3. Use a bit-reversed sequence to generate a numerical primary key value. See the TicketSale.java entity for an example.

  4. Use interleaved tables. See Track.java for an example of an entity that is interleaved in a parent table.

  5. 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.

  6. Execute read-only transactions. See SingerService.java for an example.

  7. Execute a stale read. See StaleReadService.java for an example.

  8. 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 a TransactionTagInterceptor to your Hibernate configuration. See TaggingHibernatePropertiesCustomizer.java for how this is done in this sample application.

  9. Add statement tags to generated queries. See the searchByLastNameStartsWith method in SingerRepository.java for an example of how to add a statement tag.

  10. Add statement and query hints to generated queries. See the findByActive method in the SingerRepository.java file for an example.