Hibernate Tips: How to automatically set an attribute before persisting it

By Thorben Janssen

Mapping

Hibernate Tips is a new series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question you like me to answer, please leave a comment below.

Question:

I want to initialize an entity attribute automatically before it gets persisted. How can I execute custom code before Hibernate persists an entity?

Solution:

The JPA specification defines a set of callback annotations to trigger method calls for certain lifecycle events. If you want to initialize an entity attribute before it gets persisted, you just have to do 2 things:

  1. Add a method to the entity which initializes the attribute.
  2. Annotate this method with @PrePersist so that Hibernate calls it before it persists the entity.

You can see an example of such a method in the following code snippet.
Hibernate will call this method when it persists a new Author entity and trigger the initialization of the createdAt attribute. You can see that in the following log output. Hibernate calls the initializeCreatedAt method before it performs the SQL statements to get the primary key value and to persist the entity.

Learn More:

I get into more details about entity lifecycle events and the more advanced EntityListeners, in my Advanced Hibernate Training. I’m happy to see you there if you want to dive deeper into this topic.

The JPA specification also defines callback annotation for other entity lifecycle events. You can, for example, use the @PreRemove annotation to execute custom code before Hibernate deletes an entity, as I do in this post: How to implement a soft delete with Hibernate.
 

Hibernate Tips Book


Get more recipes like this one in my new book Hibernate Tips: More than 70 solutions to common Hibernate problems.

It gives you more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching and statically and dynamically defined queries.

Get it now as a paperback, ebook or PDF.


Tags

Mapping


About the author

Thorben is an independent consultant, international speaker, and trainer specialized in solving Java persistence problems with JPA and Hibernate.
He is also the author of Amazon’s bestselling book Hibernate Tips - More than 70 solutions to common Hibernate problems.

Books and Courses

Coaching and Consulting

Leave a Repl​​​​​y

Your email address will not be published. Required fields are marked

This site uses Akismet to reduce spam. Learn how your comment data is processed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}