Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Simplify Java persistence using Quarkus and Hibernate Reactive

January 6, 2022
Daniel Oh
Related topics:
Event-drivenJavaKubernetesQuarkusSystem design
Related products:
Red Hat OpenShift

    This tutorial shows how you can simplify reactive Java applications that persist data using the Hibernate ORM with Panache extension in Quarkus.

    Business applications preserve valuable business data in persistence stores such as relational databases. The application's presentation layer usually showcases the data for multiple uses, such as inventory, shopping, subscription, and monitoring. Java provides a core feature, the Java Persistence API (JPA), to manage persistent objects using object-relational mapping (ORM) with databases such as PostgreSQL, MySQL, and Microsoft's SQL Server. However, even when using JPA annotations, you must implement the JPA specifications to handle data transactions.

    Hibernate ORM with Panache

    Hibernate ORM is designed to solve this problem by providing an object-relational mapping framework between Java domain bean classes and schemas in a relational database. The Quarkus framework provides the Hibernate ORM with Panache extension to simplify the persistence layer implementation.

    Benefits

    Hibernate ORM with Panache offers the following benefits over previous options:

    • ID auto-generation
    • No need for getters/setters
    • Static methods provided for access, such as listAll, findById, and find
    • No need for custom queries for basic operations

    Hibernate Reactive

    With the rise of the event-driven architecture, Java developers also need to implement these JPA capabilities in a reactive way to improve application performance and scalability on the event-driven architecture. Hibernate Reactive has recently released version 1.0 with a reactive API for Hibernate ORM support, performing non-blocking interactions with relational databases. The Hibernate Reactive API also lets you manage reactive streams using the SmallRye Mutiny programming library.

    Note: The code for this tutorial is stored in my GitHub repository.

    Add reactive extensions to a Quarkus project

    Run the following Maven command in your project directory to add reactive extensions:

    $ ./mvnw quarkus:add-extension -Dextensions=resteasy-reactive,resteasy-reactive-jackson,hibernate-reactive-panache,reactive-pg-client

    Make sure that the extensions are added to the pom.xml file in the project directory.

    Develop a reactive application with Hibernate Reactive

    The Quarkus dev service for PostgreSQL will pull a PostgreSQL container image and run it automatically in your local environment. Be sure to start a container engine before running Quarkus in Dev Mode. Then, to use the live coding feature, run Quarkus Dev Mode through the following Maven command in the project directory:

    $ ./mvnw quarkus:dev

    The following log messages appear when Quarkus Dev Mode is ready for live coding:

    --
    
    Tests paused
    
    Press [r] to resume testing, [o] Toggle test output, [h] for more options>

    Create a Fruit.java class file in the src/main/java/{Package path} directory. Add the following code to create a new entity bean using Hibernate ORM with Panache:

    @Entity
    @Cacheable
    public class Fruit extends PanacheEntity {
    
        public String name;
    
        public Fruit() {}
    
        public Fruit(String name) {
            this.name = name;
        }
    
    }

    Create a new resource class file (e.g., ReactiveGreetingResource) or update an existing resource class file by adding the following code:

        @GET
        public Uni<List<Fruit>> listAll() { //<1>
            return Fruit.listAll();
        }
    
        @GET
        @Path("{id}")
        public Uni<Fruit> findById(@RestPath Long id) { //<2>
            return Fruit.findById(id);
        }
    
        @POST
        @ReactiveTransactional
        public Uni<Fruit> create(Fruit fruit) { //<3>
            return Fruit.persist(fruit).replaceWith(fruit);
        }
    
        @DELETE
        @Path("{id}")
        @ReactiveTransactional
        public Uni<Void> delete(@RestPath Long id) { //<4>
            return Fruit.deleteById(id).replaceWithVoid();
        }

    Explanations of the annotated lines follow:

    1. Retrieve all Fruit data from the PostgreSQL database using Panache, then return a reactive return stream (Uni).
    2. Retrieve a particular Fruit using Panache, then return a reactive stream (Uni).
    3. Create a new Fruit using a reactive Panache persistence method.
    4. Delete an existing Fruit using a reactive Panache persistence method.

    Add a few test data items to an import.sql file in the src/main/resources directory:

    INSERT INTO fruit(id, name) VALUES (nextval('hibernate_sequence'), 'Cherry');
    INSERT INTO fruit(id, name) VALUES (nextval('hibernate_sequence'), 'Apple');
    INSERT INTO fruit(id, name) VALUES (nextval('hibernate_sequence'), 'Banana');

    Go to the terminal where you are running Quarkus in Dev Mode. Press d to access the Quarkus Dev UI, shown in Figure 1. Take a look at the Hibernate ORM extension box on the right side.

    The Quarkus Dev UI offers a Hibernate ORM extension.
    Figure 1. The Quarkus Dev UI offers a Hibernate ORM extension.

    Click Persistence Units in the extension box. The Dev UI displays the SQL statements that can create or drop the data, as shown in Figure 2.

    You can display the SQL statements that implement persistence.
    Figure 2. You can display the SQL statements that implement persistence.

    Test the reactive application

    Open a new terminal window locally. Invoke the reactive API endpoint using a cURL command or the HTTPie tool. HTTPie could be used as follows:

    $ http :8080/fruits

    The output should look like this:

    HTTP/1.1 200 OK
    Content-Type: application/json
    content-length: 75
    [
        {
            "id": 1,
            "name": "Cherry"
        },
        {
            "id": 2,
            "name": "Apple"
        },
        {
            "id": 3,
            "name": "Banana"
        }
    ]

    Add new data using the following HTTPie command:

    ​​​​​$ echo '{"name":"Orange"}' | http :8080/fruits

    The output should look like this:

    HTTP/1.1 201 Created
    Content-Type: application/json
    content-length: 24
    {
        "id": 4,
        "name": "Orange"
    }

    Now the Orange appears in the fruits data:

    $ http :8080/fruits                           
    HTTP/1.1 200 OK
    Content-Type: application/json
    content-length: 100
    [
        {
            "id": 1,
            "name": "Cherry"
        },
        {
            "id": 2,
            "name": "Apple"
        },
        {
            "id": 3,
            "name": "Banana"
        },
        {
            "id": 4,
            "name": "Orange"
        }
    ]

    Where to learn more

    This guide has shown how Quarkus lets you simplify JPA implementation through the Hibernate ORM with Panache extension. Quarkus also provides awesome features to improve developers’ productivity through continuous testing, the Quarkus command-line interface (CLI), the Dev UI, and Dev Services. Find additional resources at the following articles:

    • Boost throughput with RESTEasy Reactive in Quarkus 2.2
    • Red Hat build of Quarkus 2.2: Simplified Kubernetes-native Java
    • Getting started with Red Hat build of Quarkus
    Last updated: October 18, 2023

    Related Posts

    • Explore Java 17 language features with Quarkus

    • Boost throughput with RESTEasy Reactive in Quarkus 2.2

    • Red Hat build of Quarkus 2.2: Simplified Kubernetes-native Java

    • Why should I choose Quarkus over Spring for my microservices?

    • Build an API using Quarkus from the ground up

    Recent Posts

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.