Don’t want to read? You can watch it here!

Solution:

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.