Skip to content

Lock is not taking into account when generating aotSources #4152

@ditogam

Description

@ditogam

Having repository definition

public interface UserRepository extends JpaRepository<UserEntity, Long> {
..........
  @Lock(LockModeType.PESSIMISTIC_WRITE)
  @QueryHints({@QueryHint(name = "jakarta.persistence.lock.timeout", value = "10000")})
  Optional<UserEntity> findByIdAndIdIsNotNull(Long id);
...................

./gradlew clean nativeCompile

generates UserRepositoryImpl__AotRepository

public Optional<UserEntity> findByIdAndIdIsNotNull(Long id) {
   String queryString = "SELECT u FROM UserEntity u WHERE u.id = :id AND u.id IS NOT NULL";
   Query query = this.entityManager.createQuery(queryString);
   query.setHint("jakarta.persistence.lock.timeout", "10000");

   query.setParameter("id", id);

   return Optional.ofNullable((UserEntity) convertOne(query.getSingleResultOrNull(), false, UserEntity.class));
 }

and not sets lock mode to query (query.setLockMode(LockModeType.PESSIMISTIC_WRITE))

I think it should be added in JpaRepositoryContributor where other annotations are processed like:

MergedAnnotation<NativeQuery> nativeQuery = context.getAnnotation(NativeQuery.class);
MergedAnnotation<QueryHints> queryHints = context.getAnnotation(QueryHints.class);
MergedAnnotation<EntityGraph> entityGraph = context.getAnnotation(EntityGraph.class);
MergedAnnotation<Modifying> modifying = context.getAnnotation(Modifying.class);

adding MergedAnnotation<Lock> lock = context.getAnnotation(Lock.class); and pass CodeBlock.Builder for processing

The only way to resolve issue, to disable code generation with:

spring:
  aot:
    repositories:
      enabled: false

steps to reproduce

  1. Clone:
    https://github.com/ditogam/TestJPAAOT

  2. Run:
    ./gradlew nativeCompile

  3. Inspect generated code:
    build/generated/aotSources/.../UserRepositoryImpl__AotRepository.java
    or the compiled class.

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions