Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Full Stack Quarkus and React

You're reading from  Full Stack Quarkus and React

Product type Book
Published in Nov 2022
Publisher Packt
ISBN-13 9781800562738
Pages 324 pages
Edition 1st Edition
Languages
Author (1):
Marc Nuri San Felix Marc Nuri San Felix
Profile icon Marc Nuri San Felix

Table of Contents (21) Chapters

Preface 1. Part 1– Creating a Backend with Quarkus
2. Chapter 1: Bootstrapping the Project 3. Chapter 2: Adding Persistence 4. Chapter 3: Creating the HTTP API 5. Chapter 4: Securing the Application 6. Chapter 5: Testing Your Backend 7. Chapter 6: Building a Native Image 8. Part 2– Creating a Frontend with React
9. Chapter 7: Bootstrapping the React Project 10. Chapter 8: Creating the Login Page 11. Chapter 9: Creating the Main Application 12. Chapter 10: Testing Your Frontend 13. Chapter 11: Quarkus Integration 14. Part 3– Deploying Your Application to the Cloud
15. Chapter 12: Deploying Your Application to Kubernetes 16. Chapter 13: Deploying Your Application to Fly.io 17. Chapter 14: Creating a Continuous Integration Pipeline 18. Index 19. Other Books You May Enjoy Appendix – Answers

Adding Persistence

In this chapter, we’ll add a persistence layer to the application and learn how to connect it to a database. We will also define the Entity classes that will support the main functionality for the task manager web application we are building throughout the book. By the end of this chapter, you should be able to persist and store the data of your application in a database. You should also have a basic understanding of Hibernate and be able to define your own entities.

We will be covering the following topics in this chapter:

  • Data persistence in Quarkus
  • Implementing the task manager data model
  • Quarkus Dev Services

Technical requirements

You will need the latest Java JDK LTS version (at the time of writing, Java 17). In this book, we will be using Fedora Linux, but you can use Windows or macOS as well.

You will need a working Docker environment to take advantage of Quarkus Dev Services. There are Docker packages available for most Linux distributions. If you are on a Windows or macOS machine, you can install Docker Desktop.

You can download the full source code for this chapter from https://github.com/PacktPublishing/Full-Stack-Quarkus-and-React/tree/main/chapter-02

Data persistence in Quarkus

Data persistence is the means by which your application will be able to store and retrieve its data from one execution to the next. This means that any data that was input will survive after the process that created it has ended. Quarkus has a growing list of extensions to support data persistence. In Chapter 1, Bootstrapping the Project, we learned about Quarkus’ support for both the imperative and reactive paradigms. Since the reactive approach will bring considerable performance improvements to our application, in this book, we’re going to use Hibernate Reactive, a reactive persistence API, in its simplified Panache version, to provide data persistence to our application. We are also going to use a PostgreSQL database since there is a reactive client extension for Quarkus too. Let us now see which dependencies we should add to the project.

Adding dependencies to our project

We will start by adding all of the required dependencies for...

Implementing the task manager data model

In this book, we are going to implement a full-featured task manager web application. In this chapter, we’ll start by implementing the data model that we’ll be using to persist the application’s state in a database.

The following diagram shows the different classes/entities that we are going to implement and their relationships:

Figure 2.2 – Entity-relationship diagram for the task manager application data model

The model is very straightforward: the main entity is a Task that might be assigned to a Project and always belongs to a User. The application is quite simple: a user logs into the task manager and can create different tasks. Each task has a title and a longer optional description. The user may assign a priority to the task and mark it as complete when it’s done. In addition, the user can create different projects that can be used to optionally group the tasks. In this section...

Quarkus Dev Services

Quarkus is deeply focused on improving the developer experience for Java – we’ve seen multiple examples so far. Quarkus Dev Services is a step further in this quest for developer joy. Its main feature is to automatically provision services in development and test modes. This means that if your project has an extension configured to provide a database service, a messaging provider, an in-memory datastore, or one of the many other supported services, Quarkus will automatically start and configure this service for your application upon its startup.

Under the hood, Quarkus uses Testcontainers and Docker to provide these services. It’s required to have a Docker-compatible environment for this feature to work.

This means we won’t need to have a local PostgreSQL database available when running our application in development mode. We won’t need to provide a configuration either. Quarkus will start a Docker container with a fresh...

Summary

In this chapter, we’ve seen how to add a fully reactive persistence layer to our application using Hibernate Reactive with Panache and the Reactive PostgreSQL client. We started by adding the required dependencies to our project using the Quarkus Maven plugin and then providing the required configuration for both the production and development environments. Then, we implemented the entities that we’ll be using in our task manager application and went over the different JPA and Hibernate Java annotations we used to configure them. We also examined Quarkus Dev Services and what advantages it provides both for the development mode and when running tests.

You should now be able to provide a basic persistence layer for your applications. In the next chapter, we’ll create the HTTP API that will be consumed by the frontend part of the application. We’ll create the different services that’ll consume the data layer we just developed and implement...

Questions

  1. What is Hibernate and how does it relate to JPA?
  2. Is it possible to implement a fully reactive persistence layer with Quarkus?
  3. How do you implement an entity with the active record pattern in Quarkus?
  4. How do you load initial application data to the database?
  5. What is Quarkus Dev Services?
lock icon The rest of the chapter is locked
You have been reading a chapter from
Full Stack Quarkus and React
Published in: Nov 2022 Publisher: Packt ISBN-13: 9781800562738
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}