@PrePersist method is not invoked when using Jakarta Data #55092
Answered
by
mbellade
lordofthejars
asked this question in
Q&A
|
Hi I had a working example using Panache (the normal one with no Jakarta Data integration). Then I decided to update the example to use Jakarta Data instead of Panache. I add the dependencies and the annotation processor. Then I run the example and the The entity is: package com.galaxium.holdservice.entity;
import io.quarkus.hibernate.panache.PanacheRepository;
import jakarta.persistence.*;
import java.time.Instant;
@Entity
@Table(name = "quotes")
public class Quote {
@Id
@Column(name = "quote_id", nullable = false, length = 50)
public String quoteId;
@Column(name = "flight_id", nullable = false)
public Integer flightId;
@Column(name = "seat_class", nullable = false, length = 50)
public String seatClass;
@Column(name = "quantity", nullable = false)
public Integer quantity;
@Column(name = "traveler_id", nullable = false)
public Integer travelerId;
@Column(name = "traveler_name", nullable = false, length = 255)
public String travelerName;
@Column(name = "price_per_seat", nullable = false)
public Long pricePerSeat;
@Column(name = "total_price", nullable = false)
public Long totalPrice;
@Column(name = "expires_at", nullable = false)
public Instant expiresAt;
@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false, length = 50)
public QuoteStatus status;
@Column(name = "created_at", nullable = false, updatable = false)
public Instant createdAt;
public interface Repo extends PanacheRepository.Stateless<Quote, String> {
}
@PrePersist
protected void onCreate() {
if (createdAt == null) {
createdAt = Instant.now();
}
}
public enum QuoteStatus {
CREATED
}
}Is it normal? My guess is that it should work, and I have not seen any information yet saying that this is not supported. Using Quarkus 3.37.0 |
Answered by
mbellade
Jun 25, 2026
Replies: 1 comment 4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not sure it should: Hibernate Data Repositories (the implementation of Jakarta Data 1.0) is backed by
StatelessSessions, which expose theinsert()method to "save" entities and notpersist(), which does not trigger@PrePersistentity lifecycle callbacks (see https://docs.hibernate.org/orm/7.4/repositories/html_single/#programming-model). A@PostPersistmethod would work, but that of course has different semantics (runs after the insert is executed on the database).Jakarta Persistence 4.0 introduces a @PreInsert lifecycle event, providing a stateless alternative, but that will be supported by Hibernate
8.0which is currently not available in Quarkus.