Entity Class : Sport
import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Version; import java.util.UUID; @Entity public class Sport { @Id private UUID id; private String name; private String description; @Version private Long version; // getter and setter omitted } I have one IT named as RepositoryIT with one Test case :
@Rollback(value = false) @Test public void validate_version_increment(){ // GIVEN final Sport sport = new Sport().setId(UUID.randomUUID()).setDescription("Cricket").setName("Cricket"); final Sport sport1 = sportRepository.save(sport); // as per my understanding version should be 0 sport1.setDescription("Awesome"); final Sport sport2 = sportRepository.save(sport1); // as per my understanding version should be 1 sport2.setDescription("Too many rules for beginners"); final Sport sport3 = sportRepository.save(sport2); // now version should be 2 ? or not1 sport3.setDescription("Too many1 rules for beginners"); final Sport sport4 = sportRepository.save(sport3); sport4.setDescription("Too many2 rules for beginners"); final Sport sport5 = sportRepository.save(sport4); sport5.setDescription("Too many3 rules for experts"); sportRepository.save(sport5); } version is always 1 but it does update description. After saving sport1 version increments to 1 and then it does not care.
and this is the hibernate logs :
Hibernate: insert into sport (description, name, version, id) values (?, ?, ?, ?) 2020-12-04 20:23:19.350 DEBUG 13961 --- [ main] n.t.d.l.l.SLF4JQueryLoggingListener : Name:DS-Proxy, Connection:3, Time:3, Success:True, Type:Prepared, Batch:False, QuerySize:1, BatchSize:0, Query:["insert into sport (description, name, version, id) values (?, ?, ?, ?)"], Params:[(Cricket,Cricket,0,86aad234-6493-413d-83c1-89927be91948)] Hibernate: update sport set description=?, name=?, version=? where id=? and version=? 2020-12-04 20:23:19.353 DEBUG 13961 --- [ main] n.t.d.l.l.SLF4JQueryLoggingListener : Name:DS-Proxy, Connection:3, Time:1, Success:True, Type:Prepared, Batch:False, QuerySize:1, BatchSize:0, Query:["update sport set description=?, name=?, version=? where id=? and version=?"], Params:[(Too many3 rules for experts,Cricket,1,86aad234-6493-413d-83c1-89927be91948,0)] I can not understand, what is the rule of @Version increment ? I know Hibernate Dirty check will check for changes for managed entity and I am changing something in every update but maybe save() does not play any role and after only transaction ends on commit it increases the version.