admin管理员组

文章数量:1353326

While trying to save the entity i am getting the following Exception

.springframework.orm.ObjectOptimisticLockingFailureException: Row was updated or deleted by another transaction

These are my Entity and i don't have version field and we cannot add it

@Entity
@Table(name = "customer")
class Customer (

    @Id
    @GeneratedValue
    @Column(name = "uuid", updatable = false)
    val uuid: UUID,

    @Column(name = "first_name", nullable = false)
    var firstName: String
)

and i create entity Object like this

return Customer(
            uuid = UUID.randomUUID(),
            firstName = validatedCreationRequest.firstName?.value?: "",
)
val createdCustomer = customerRepository.save(Object)

Starting hibernate 6.6 their were come changes but which says not to set the @id field but how to set the UUID to null, should i make it null able ? UUID?

While trying to save the entity i am getting the following Exception

.springframework.orm.ObjectOptimisticLockingFailureException: Row was updated or deleted by another transaction

These are my Entity and i don't have version field and we cannot add it

@Entity
@Table(name = "customer")
class Customer (

    @Id
    @GeneratedValue
    @Column(name = "uuid", updatable = false)
    val uuid: UUID,

    @Column(name = "first_name", nullable = false)
    var firstName: String
)

and i create entity Object like this

return Customer(
            uuid = UUID.randomUUID(),
            firstName = validatedCreationRequest.firstName?.value?: "",
)
val createdCustomer = customerRepository.save(Object)

Starting hibernate 6.6 their were come changes but which says not to set the @id field but how to set the UUID to null, should i make it null able ? UUID?

Share Improve this question asked Apr 1 at 9:04 KuldeepKuldeep 66914 silver badges32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Problem Explaination:

.springframework.orm.ObjectOptimisticLockingFailureException: Row was updated or deleted by another transaction occurs in a Spring application when using Optimistic Locking with JPA/Hibernate.
This exception is thrown when two or more transactions try to update the same entity concurrently, and one of the transactions detects that the entity has been modified or deleted by another transaction before it could complete its own update.

Solution:

Use a @Version at Field in Your Entity
Ensure that your entity has a @Version field. This field is used by JPA/Hibernate to track changes to the entity and detect concurrent modifications.

Example:



import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Version;

@Entity
public class MyEntity {

    @Id
    private Long id;

    private String name;

    @Version
    private Integer version; // Version field for optimistic locking

    
}
  • The @Version field is automatically incremented by Hibernate each time the entity is updated.

  • When a transaction tries to update the entity, Hibernate checks if the version in the database matches the version in the entity. If they doesn't match, an ObjectOptimisticLockingFailureException will thrown.

本文标签: javaorgspringframeworkormObjectOptimisticLockingFailureException While Saving in KotlinStack Overflow