A Panache entity is a database-backed Java class that extends `PanacheEntity`. It uses Panache’s active record pattern: persistence operations are available through the entity class, while the class can also hold application-specific behavior. `PanacheEntity` supplies an inherited `id`, so the entity does not need to declare one in the basic setup. section 7.1.2
To use a Panache entity with PostgreSQL in a Quarkus service:
- Add `quarkus-hibernate-orm-panache` and the database driver, `quarkus-jdbc-postgresql`. section 7.1.2
- Configure the datasource kind and credentials in `application.properties`, along with the JDBC URL for production. section 7.1.2
- Annotate the Java class with `@Entity` and extend `PanacheEntity`. section 7.1.2
Listing 7.5 The Reservation entity in the Reservation service
package org.acme.reservation.entity;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Entity;
import java.time.LocalDate;
@Entity
public class Reservation extends PanacheEntity {
public Long carId;
public String userId;
public LocalDate startDay;
public LocalDate endDay;
/**
* Check if the given duration overlaps with this reservation
* @return true if the dates overlap with the reservation, false
* otherwise
*/
public boolean isReserved(LocalDate startDay, LocalDate endDay) {
return (!(this.endDay.isBefore(startDay) ||
this.startDay.isAfter(endDay)));
}
@Override
public String toString() {
return "Reservation{" +
"id=" + id +
", carId=" + carId +
", userId='" + userId + '\'' +
", startDay=" + startDay +
", endDay=" + endDay +
'}';
}
}
The Reservation entity shows the annotation, base class, inherited `id`, public data fields, and an application-specific date-overlap method together. section 7.1.2
A query on the entity
A static `findByCar(Long carId)` method can return `list("carId", carId)`. This keeps a car-specific lookup alongside the Reservation entity rather than requiring a separate repository for this active record example. section 7.1.2
A Panache entity can also be the basis for generated CRUD REST endpoints. With the Hibernate ORM REST Data extension, an interface extending `PanacheEntityResource<Reservation, Long>` exposes operations for the entity; `@ResourceProperties` can set its root path. The JDBC-based REST Data extension is identified as stable, while the MongoDB and Hibernate Reactive variants are identified as experimental. section 7.4
Listing 7.22 The code of ReservationCrudResource
package org.acme.reservation.rest;
import io.quarkus.hibernate.orm.rest.data.panache.PanacheEntityResource;
import org.acme.reservation.entity.Reservation;
public interface ReservationCrudResource extends
PanacheEntityResource<Reservation, Long> {
}
The resource interface shows how little code is needed to connect a Panache entity to generated CRUD endpoints. section 7.4
