I have a versioned entity that I optimistically lock whenever I read it from the database using @Lock(LockModeType.OPTIMISTIC_FORCE_INCREMENT)
.
I was originally using spring boot 2.7.11 and upgraded to 3.2.0. Everything was working fine before and ObjectOptimisticLockingFailureException
was thrown whenever a conflict occurs, however when I upgraded to spring boot 3.2.0 and hibernate version was upgraded to 6.3.1, I get org.hibernate.AssertionFailure: cannot force version increment on non-versioned entity
here is the entity definition:
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.CreationTimestamp;
import jakarta.persistence.*;
import java.util.Collection;
import java.util.Date;
@Entity
public class Case {
@Version
private int version;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(nullable = false)
private Merchant merchant;
@Column(name = "case_creation_date", nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreationTimestamp
private Date caseCreationDate;
@ManyToOne
@JsonIgnore
private Agent agent;
@Column(length = 2000, nullable = true)
private String note;
@ManyToMany
@JoinTable(name = "case_justification")
private Collection<Justification> caseResolutionJustifications;
// constructors
// setters and getters
}
and here is the read query:
@Repository
public interface CasesRepository extends JpaRepository<Case, Long> {
@Transactional
@Override
@Lock(LockModeType.OPTIMISTIC_FORCE_INCREMENT)
Optional<Case> findById(Long caseId);
}
I thought it may be a mapping issue, I dropped the table and let hibernate create the table again, still have the same issue.