@PrePersist is a JPA lifecycle callback annotation. A method marked with it runs automatically just before the persistence provider inserts a new entity, which makes it the right place to set creation timestamps, default values and generated identifiers.
It belongs to the callback family defined by Jakarta Persistence (jakarta.persistence.PrePersist). The callback fires when persist() is called on a new entity, not when the SQL INSERT is flushed, and it can live on the entity itself or in a separate entity listener.
JPA defines callbacks for each stage of an entity's life. @PrePersist is the earliest one for a new entity.
| Annotation | Runs |
|---|---|
@PrePersist | Before EntityManager.persist() makes a new entity managed |
@PostPersist | After the INSERT has been executed |
@PreUpdate | Before an UPDATE of a managed entity |
@PostUpdate | After the UPDATE |
@PreRemove | Before the entity is removed |
@PostRemove | After the DELETE |
@PostLoad | After an entity is loaded from the database |
Two details matter in practice:
persist() is invoked, or when persist cascades to the entity from a parent. It does not wait for the transaction commit or the flush.@GeneratedValue(strategy = GenerationType.IDENTITY) the identifier is not yet assigned inside @PrePersist, because the database creates it during the insert. With sequence-based generation the id is usually available, but do not rely on that across providers.The most common use is an automatic creation date. In Spring Boot 3 and later the package is jakarta.persistence; older projects used javax.persistence.
Use Instant (or OffsetDateTime) for audit fields rather than LocalDateTime, so the stored value does not depend on the server's time zone. The callback method may be private, package-private, protected or public, must return void, takes no arguments when defined on the entity, and must not be static or final.
Callbacks can fill fields the caller left empty, which keeps invariants in one place.
Generating a public identifier here guarantees every new row has one, no matter which service layer path created it.
When several entities need the same behaviour, move it into a listener class and attach it with @EntityListeners. A listener callback receives the entity as its single parameter.
Every entity that extends BaseEntity now gets its creation time without repeating code. Listener methods run in a defined order: default listeners, then listeners declared on superclasses, then the entity's own listeners, and finally callbacks declared on the entity class itself.
If you use Spring Data JPA, the built-in auditing support covers the common cases without hand-written callbacks. Add @EnableJpaAuditing, annotate the entity with @EntityListeners(AuditingEntityListener.class), and mark fields with @CreatedDate, @LastModifiedDate, @CreatedBy and @LastModifiedBy. Internally this is itself a @PrePersist listener. Choose plain @PrePersist for custom logic and Spring Data auditing for standard audit columns and user tracking.
EntityManager operations, query the database, or touch other entities inside a callback. The specification forbids it and providers behave differently.INSERT, native SQL, or JDBC batch statements that bypass the persistence context.@PreUpdate, not @PrePersist.@CreationTimestamp, which sets a timestamp at insert time and is a lighter alternative when you only need the date.@PrePersist runs before the entity becomes managed and before the insert, so you can still change field values that will be written. @PostPersist runs after the insert, when generated identifiers are available.
No. Updates trigger @PreUpdate. Use both when you maintain createdAt and updatedAt.
Yes. save() calls persist() for a new entity, which fires the callback. For an existing entity it calls merge() and @PrePersist does not run.
Use a database default or trigger when rows can be inserted outside your application. Use @PrePersist when the value must exist in the entity right after persist().
@PrePersist lets you prepare a new entity just before it is inserted: set audit timestamps, defaults and public identifiers in one place. Put it on the entity for local logic or in an entity listener for shared logic, use the jakarta.persistence package on current stacks, keep the callback free of database access, and prefer Spring Data auditing for standard audit columns.