Quarkus JPA CRUD Example (2024)
In this tutorial, we'll demonstrate how to create a Quarkus
CRUD example + MYSQL using Panache
library with below rest api's.
POST
- Create Employee RecordGET
- List all employeesGET
- Get employees by it's idPUT
- Update/Edit selected employee detailsDELETE
- Remove selected employee record
Q: What is Panache?
Ans:
Panache is a Quarkus-specific library that makes it easier to create persistent layers based on Hibernate. The majority of the repetitive boilerplate code is handled by Panache, much like Spring Data JPA does.
Create Quarkus application
Create Quarkus application from Code Quarkus.
Project Structure
Add Dependencies
Add resteasy-jsonb
, quarkus-hibernate-orm-panache
for jpa and mysql-connector-java
dependencies.
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.techgeeknext</groupId>
<artifactId>quarkus-jpa-example</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<maven.compiler.release>11</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>2.11.1.Final</quarkus.platform.version>
<skipITs>true</skipITs>
<surefire-plugin.version>3.0.0-M7</surefire-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-mysql</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<skipITs>false</skipITs>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>
Take a look at our suggested posts:
Application Properties
Add database connection details in application.properties
file.
quarkus.datasource.jdbc.url=jdbc:mysql://localhost/employeetestdb?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false
quarkus.datasource.username=root
quarkus.datasource.password=root
quarkus.datasource.db-kind=mysql
quarkus.hibernate-orm.database.generation=update
Data Model
Create Employee
class, contains id
, name
and
role
.
package com.techgeeknext.model;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "employees")
public class Employee extends PanacheEntity {
@Column(name = "name")
private String name;
@Column(name = "role")
private String role;
public Employee(String name, String role) {
this.name = name;
this.role = role;
}
public Employee() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
Employee Controller to handle CRUD rest endpoints
package com.techgeeknext.controller;
import com.techgeeknext.model.Employee;
import javax.enterprise.context.ApplicationScoped;
import javax.transaction.Transactional;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Optional;
@Path("/employee")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApplicationScoped
public class EmployeeController {
@GET
@Path("/hello")
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Quarkus Example";
}
@GET
@Path("{id}")
public Employee findEmployeeById(@PathParam("id") Long id) {
return (Employee) Employee.findByIdOptional(id).orElseThrow(NotFoundException::new);
}
@GET
public List<Employee> getAllEmployee() {
return Employee.listAll();
}
@POST
@Transactional
public Employee addEmployee(Employee employee) {
Employee.persist(employee);
return employee;
}
@PUT
@Path("/{id}")
@Transactional
public Employee updateEmployee(@PathParam("id") Long id, Employee employee) {
Optional<Employee> employeeById = Optional.ofNullable(findEmployeeById(id));
employeeById.orElseThrow(NotFoundException::new);
Employee empToUpdate = employeeById.get();
empToUpdate.setName(employee.getName());
empToUpdate.setRole(employee.getRole());
Employee.getEntityManager().merge(empToUpdate);
return employee;
}
@DELETE
@Path("/{id}")
@Transactional
public void deleteEmployee(@PathParam("id") Long id) {
Employee.deleteById(id);
}
}
Test Quarkus JPA CRUD Example
- Start the Quarkus Application by running
compile quarkus:dev
for Development mode. - Or we can run the application from Intellj or any IDE as below:
-
Quarkus Application will start at
8080
port by default.__ ____ __ _____ ___ __ ____ ______ --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \ --\___\_\____/_/ |_/_/|_/_/|_|\____/___/ 15:00:18,315 INFO [io.quarkus] (Quarkus Main Thread) quarkus-jpa-example 1.0.0-SNAPSHOT on JVM (powered by Quarkus 2.11.1.Final) started in 8.781s. Listening on: http://localhost:8080 15:00:18,321 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated. 15:00:18,323 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [agroal, cdi, hibernate-orm, hibernate-orm-panache, jdbc-mysql, narayana-jta, resteasy, resteasy-jsonb, smallrye-context-propagation, vertx]
Create New Employee
Open Postman, use POST method with end point http://localhost:8080/employee and provide Employee details to create new employee record.List Employees
Use GET method with end point http://localhost:8080/employee to get all employees.Get Employee
Use GET method with end point http://localhost:8080/employee/1 to get employee by id.Edit Employee
Use PUT method with end point http://localhost:8080/employee/2 where2
is theid
of the employee and provide employee details as body in Postman to edit.Delete Employee
Use DELETE method with end point http://localhost:8080/employee/2 where2
is theid
of the employee to be deleted.
Download Source Code
The full source code for this article can be found below.
- Download it here - Quarkus JPA CRUD Example