Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Cloud Spanner Quarkus JPA and Panache with Hibernate Sample

Warning
This sample temporarily does not work; it hangs when trying to acquire Spanner sessions. See #373 for context.

This sample application demonstrates using Quarkus with JPA, Panache and Google Cloud Spanner.

This sample is only tested with Quarkus in JVM mode. Native mode is not tested/supported.

Setup & Configuration

  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 quarkus.datasource.jdbc.url defined in the file src/main/resources/application.properties. This setting configures the connection URL which specifies the database that your application will connect to.

    # Spanner connection URL.
    # - ${PROJECT_ID} Replace with your GCP project ID
    # - ${INSTANCE_ID} Replace with your Spanner instance ID
    # - ${DATABASE_NAME} Replace with the name of your Spanner database that you created inside your Spanner instance
    
    quarkus.datasource.jdbc.url=jdbc:cloudspanner:/projects/${PROJECT_ID}/instances/${INSTANCE_ID}/databases/${DATABASE_NAME}
  5. Open a terminal instance inside this directory and run mvn compile quarkus:dev to start the app.

    In the application.properties file, you’ll see that the application is running with quarkus.hibernate-orm.database.generation=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.

  6. Post a data entry with curl: curl -XPOST -d'{"name": "Ray"}' -H"Content-Type: application/json" http://localhost:8080/person

    You can view the data that was populated in your Cloud Spanner database by navigating to your database in the Spanner Console view.