The active record pattern puts database operations on the object that represents a record. With Quarkus Panache, an entity can persist or delete itself through instance methods, while static methods query or modify records more generally. This replaces the need to inject an EntityManager for these operations and reduces data-access boilerplate. section 7.1.1
Listing 7.1 Panache example of active record manipulation
// Assume we have an entity named Car
// persisting into the database called on the object instance
car.persist();
// deleting an entity
car.delete();
// listing all Cars from the database called as a static method
Car.listAll();
// find Car by ID
Car.findById(carId);
// list all blue Cars
Car.list("color", Color.Blue);
// count all cars
Car.count();
// delete Car by ID
Car.deleteById(carId);
The Car example shows the distinction: car.persist() and car.delete() act on one object; Car.listAll(), findById(), list(), count(), and deleteById() operate through the entity class. section 7.1.1
For a PostgreSQL-backed Reservation service, add quarkus-hibernate-orm-panache and quarkus-jdbc-postgresql, configure the data source, and define an @Entity extending PanacheEntity. The entity can also hold application-specific behavior, such as a reservation-overlap check or a static findByCar query. Panache queries can use parts of Hibernate Query Language and Java Persistence Query Language. section 7.1.2 section 7.1.1
The resource can then call reservation.persist() instead of an injected repository; the method that persists it is marked @Transactional. Other resource methods call static Reservation operations. In development mode, an unconfigured database URL lets Quarkus start a temporary PostgreSQL container; a production connection must be configured separately. A POST followed by a GET—and a direct psql query—demonstrates that the reservation is stored rather than held in a stub list. section 7.1.3 section 7.1.2
Active record is a choice, not a requirement of Panache: the repository pattern is another supported approach. For MongoDB, the same entity-centered approach uses PanacheMongoEntity and the quarkus-mongodb-panache extension. Hibernate Reactive Panache retains a similar model but returns asynchronous Uni results and requires reactive extensions and a reactive database client. section 7.6.1 section 7.7.1 section 7.9


