0

Having the entity class:

@Entity
class Foo {

    @Id
    Long id;

    @Version
    java.time.Instant ts;
}

I need to persist it with the explicit value of ts field in DB. However if I set the value of ts explicitly, it's overwritten by the JPA automatically.

I've seen the solution for the update, however it's not working for me, as I need it on insert already: Is it possible to turn off hibernate version increment for particular update?

I can't remove the @Version as it's in the parent class of my entity.

Moreover, I can't disable the behaviour in general. Would need it for the specific use-case only.

Peter Butkovic
  • 11,143
  • 10
  • 57
  • 81

1 Answers1

1

According to the hibernate documentation:

Your application is forbidden from altering the version number set by Hibernate. To artificially increase the version number, see the documentation for properties LockModeType.OPTIMISTIC_FORCE_INCREMENT or LockModeType.PESSIMISTIC_FORCE_INCREMENT check in the Hibernate Entity Manager reference documentation.

If the version number is generated by the database, such as a trigger, use the annotation @org.hibernate.annotations.Generated(GenerationTime.ALWAYS) on the version attribute.

Community
  • 1
  • 1
SternK
  • 11,649
  • 22
  • 32
  • 46