@PrePersist and @PostPersist are JPA entity lifecycle callbacks. A method annotated with @PrePersist runs just before a new entity is made persistent, which makes it the right place to set defaults such as timestamps or IDs. A method annotated with @PostPersist runs after the entity has been inserted, when generated values such as an identity key are available.
Both annotations live in the jakarta.persistence package in Jakarta Persistence 3.x (Hibernate 6 and Spring Boot 3). In older code they were in javax.persistence with the same behaviour.
| Callback | Triggered | State of the entity |
|---|---|---|
@PrePersist | When persist() is called (and for cascaded persist) | Not yet inserted; identity ID not assigned |
@PostPersist | After the INSERT statement is executed | Row inserted; generated ID is set |
The INSERT itself may be delayed until flush or commit, so @PostPersist can run later than the persist() call, except for identity-generated keys where Hibernate inserts immediately to get the ID. Nothing is guaranteed to be committed yet, so a later rollback can still undo the row.
Saving a new Article with a null createdAt fills it automatically, and @PostPersist can read the generated id. Callback methods on an entity must return void, take no arguments and may have any visibility. They must not call EntityManager or query operations, or modify other entities' relationships.
When many entities need the same behaviour, put it in a listener class and register it with @EntityListeners:
A listener callback receives the entity as its single argument. The same annotated methods in a @MappedSuperclass are inherited by subclasses, which is another clean way to reuse them. In Spring Data JPA, @CreatedDate with @EntityListeners(AuditingEntityListener.class) and @EnableJpaAuditing is the ready-made version of this pattern.
Callbacks need a real persistence provider, so test them with an integration test such as @DataJpaTest:
The persistAndFlush call forces the INSERT, so both callbacks have run before the assertions.
@PrePersist also fires when merge() saves a new, detached instance as a new row.@PostPersist. The transaction may still roll back. Use @TransactionalEventListener(phase = AFTER_COMMIT) in Spring instead.@PreUpdate, @PostUpdate, @PreRemove, @PostRemove and @PostLoad complete the lifecycle.@PrePersist runs before the entity is inserted and is used to prepare data. @PostPersist runs after the insert, when generated values are available.
No. Updates trigger @PreUpdate and @PostUpdate.
Yes. save() calls persist() for new entities, so the callback fires. For existing entities it calls merge() and @PreUpdate applies instead.
Yes, in the persistence context of the current transaction. Commit happens later.
@PrePersist prepares an entity just before insertion, and @PostPersist reacts after the insert with generated values available. Use them or a shared entity listener for timestamps and defaults, avoid side effects outside the database, and remember bulk operations skip callbacks.