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?
1 Answer
Reset to default 0Problem 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.
版权声明:本文标题:java - org.springframework.orm.ObjectOptimisticLockingFailureException While Saving in Kotlin - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743898206a2558183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论