liveBook · Manning

Entity

As explained in Quarkus in Action

A persistence entity is a Java object whose state is mapped to and managed as records in a database through object-relational mapping.

An entity is a Java object whose state is persisted in a database through object-relational mapping. In Quarkus, mark the class with `@Entity`, then choose where persistence operations belong: on the entity with Panache’s active record pattern, in a repository, or in code using JPA’s `EntityManager`. section 7.1.2 section 7.2.1 section 7.3

The entity definition changes with the access pattern:

  • For active record, extend `PanacheEntity`. The reservation entity inherits an ID and can define a static query method such as `findByCar(carId)` alongside its fields and business behavior. section 7.1.2
  • For a repository, a plain JPA entity is enough. The car entity declares its own `@Id` and `@GeneratedValue` field; an `@ApplicationScoped` class implementing `PanacheRepository<Car>` handles persistence access. section 7.2.1
  • For traditional JPA access, define entities as usual and inject `EntityManager` into a CDI bean. Panache is not required for this approach. section 7.3
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 class shows `@Entity` on a `PanacheEntity` subclass with application fields and an overlap-checking method. section 7.1.2

Listing 7.13 The Car entity
package org.acme.inventory.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Car {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String licensePlateNumber;
    private String manufacturer;
    private String model;
    // getters, setters, toString, equals, and hashcode omitted
}

The car class shows the alternative: a plain JPA entity with an explicitly declared, generated primary key. section 7.2.1

To persist these entities, add the Hibernate ORM extension for the chosen approach and a JDBC driver for the database, then configure the datasource in `application.properties`. The examples use PostgreSQL for reservations and MySQL for inventory. Their `drop-and-create` schema setting drops and recreates the database schema at startup, so it is not a setting to carry over unexamined to production. section 7.1.2 section 7.2.1 section 7.3

How Quarkus in Action covers it

How deep it goes
Works through entity annotations, ID choices, and access patterns, but does not develop a pure-JPA service.
Examples
Shows a reservation with an overlap method, a car with a generated ID, and an EntityManager persist call.
What it assumes
Assumes familiarity with Java classes while showing the annotations, ID declarations, and extensions needed for persistence.
What you can do after
Build it from scratch as a Panache or plain JPA entity, then access it through a repository or EntityManager.
How it connects
Connects entities to object-relational mapping, active record, repositories, primary keys, CDI, and EntityManager.

Also explained in

Compare how 12 books explain entity →

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.