How to increment the parent entity version whenever a child entity gets modified with JPA and Hibernate


StackOverflow and the Hibernate forum are gold mines. Yesterday, I bumped on the following question on our forum:

Usually, the rationale behind clustering objects together is to form a transactional boundary inside which business invariants are protected. I’ve noticed that with the OPTIMISTIC locking mode changes to a child entity will not cause a version increment on the root. This behavior makes it quite useless to cluster objects together in the first place.

Is there a way to configure Hibernate so that any changes to an object cluster will cause the root object’s version to increment? I’ve read about OPTIMISTIC_FORCE_INCREMENT but I think this does increment the version regardless of if entities were changed or not. Since reads shouldn’t be conflicting with other reads in most scenarios, this doesn’t seem so useful either.

I could always increment the version inside every mutating behavior of the root, but that is quite error-prone. I’ve also thought of perhaps using AOP to do this, but before looking into it, I wanted to know if there were any easy way to do that. If there were a way to check if an object graph is dirty, then it would make it quite easy to implement as well.

What a brilliant question! This post is going to demonstrate how easy you can implement such a requirement when using Hibernate.

Continue reading “How to increment the parent entity version whenever a child entity gets modified with JPA and Hibernate”


High-Performance Java Persistence – Part Three

100% done

The third part of High-Performance Java Persistence book is out. It’s been one year and two months since I started writing this book, and nine months since I published the first part.

Continue reading “High-Performance Java Persistence – Part Three”

JPA providers market share in 2016

The survey

Java Persistence API is a standard. Hence, there are multiple options to choose from:

  • Hibernate
  • EclipseLink
  • OpenJPA

Some applications choose not to use an ORM framework at all. For this reason, I decided to run a one-day survey on Twitter to get a glimpse on the JPA providers market share.

Continue reading “JPA providers market share in 2016”

Fluent API entity building with JPA and Hibernate


The JHipster development team wants to expose Fluent Interface entity building methods for their JPA entities, so they asked me if this is going to work with JPA and Hibernate. While JPA is rather strict about entity getters and setter, Hibernate is more lenient in this regard.

Continue reading “Fluent API entity building with JPA and Hibernate”