Hibernate Tips is a 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.
The database administrator set up a trigger to generate the value of a database column. How can map this column with Hibernate so that I can use the generated value within my application?
Don’t want to read? You can watch it here!
You can annotate an entity attribute with @Generated(GenerationTime value), to tell Hibernate that the database will generate the value of the attribute. The GenerationTime enum tells Hibernate when the database will generate the value. It can either do this NEVER, only on INSERT or ALWAYS (on insert and update). Hibernate will then execute an additional query to retrieve the generated value from the database.
The following code snippet shows an example for such an entity mapping.
As you can see in the following log output, Hibernate now performs an additional query for each insert and update statement to retrieve the generated value.
Get this Hibernate Tip as a printable PDF!Join the free Thoughts on Java Library to get access to lots of member-only content, like a printable PDF for this post, lots of cheat sheets and 2 ebooks about Hibernate.
Hibernate Tips Book
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.