Updating entities without version increment

Hi,

I was wondering if there was a way to update an entity without having its version incremented when using @Version.

I found some ideas on stackoverflow, e.g., jpa - Is it possible to turn off hibernate version increment for particular update? - Stack Overflow, evicting the entity then using replicate with ReplicationMode.LATEST_VERSION, but it’s using a deprecated API that does not seem to have a replacement so I’m thinking maybe it’s not such a good idea to use that.

Thanks

You could use HQL DML i.e. update MyEntity e set e.attribute = :param where e.id = :id.

Ah but of course, I completely forgot that DML HQL was not versioned by default! Thanks :slight_smile:

I suspect that to achieve the same thing with dirty checking, managed entities and updates on flush is not possible out of the box? Most certainly because it’s way too complex to control when flushes happen, etc?

I was thinking defining a Generator on the Version field and if a certain transient field was set on the entity, then version would not be incremented. But this seems hacky and very hard to reason about.

If your state transformation is not idempotent, you should really consider using optimistic locking, otherwise you might write data that doesn’t fit the assumptions based on the load statement before.