liveBook · Manning

Active record pattern

As explained in Quarkus in Action

The active record pattern is a persistence pattern in which a data record object encapsulates the operations for storing, updating, deleting, and querying itself.

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

How Quarkus in Action covers it

How deep it goes
Works through extension setup, entity definition, resource changes, transactions, and persistence verification without cataloging every Panache operation.
Examples
Uses Car method calls, a PostgreSQL-backed Reservation resource, POST/GET and psql checks, and a MongoDB Rental entity.
What it assumes
Assumes a Java Quarkus service while introducing the required Panache extensions, database configuration, and entity base class.
What you can do after
Build it from scratch with Panache entities and call their persistence and query methods from a Quarkus service.
How it connects
Connects Panache to Hibernate ORM, the repository pattern, PostgreSQL, MongoDB, and Hibernate Reactive.

Also explained in

Compare how 3 books explain active record pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quarkus in Action. The section links point to the parts of the book it draws on.