Let's assume we need a CrudRepository with two methods:
- findById with default behaviour
- findByIdForUpdate with LockModeType.PESSIMISTIC_WRITE
Naive way to implement it might be:
@Repository
interface MyRepository : CrudRepository<MyEntity, Long> {
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("from MyEntity where id = :id")
fun findByIdForUpdate(id: Long): Optional<MyEntity>
}
However, it produces two queries if called two times in the same transaction.
This can be avoided using Custom Repository Implementations https://docs.spring.io/spring-data/jpa/reference/repositories/custom-implementations.html and EntityManager.find(Class, Object, LockModeType).
What do you think about an out-of-box method such as CrudRepository.findById(ID, LockModeType) to support this?
Let's assume we need a CrudRepository with two methods:
Naive way to implement it might be:
However, it produces two queries if called two times in the same transaction.
This can be avoided using Custom Repository Implementations https://docs.spring.io/spring-data/jpa/reference/repositories/custom-implementations.html and
EntityManager.find(Class, Object, LockModeType).What do you think about an out-of-box method such as
CrudRepository.findById(ID, LockModeType)to support this?