Skip to content

Commit

Permalink
Fix hibernate count native query (#3244)
Browse files Browse the repository at this point in the history
  • Loading branch information
radovanradic authored Nov 25, 2024
1 parent fa9d9f7 commit 1352324
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,15 @@ class PageSpec extends Specification {
def pageable = Pageable.from(0, 10)
Page<Person> page = personRepository.findByNameLike("A%", pageable)
Page<Person> page2 = crudRepository.findPeople("A%", pageable)
Page<Person> page3 = crudRepository.findPeopleNative("A%", pageable)
Slice<Person> slice = personRepository.queryByNameLike("A%", pageable)

then:"The page is correct"
page.offset == 0
page.pageNumber == 0
page.totalSize == 50
page2.totalSize == page.totalSize
page3.totalSize == page.totalSize
slice.offset == 0
slice.pageNumber == 0
slice.size == 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public interface PersonCrudRepository extends JpaRepository<Person, Long>, Perso
@Transactional
Page<Person> findPeople(String n, Pageable pageable);

@Query(value = "SELECT * FROM person WHERE name LIKE :n",
countQuery = "SELECT COUNT(*) FROM person WHERE name LIKE :n",
nativeQuery = true)
@Transactional
Page<Person> findPeopleNative(String n, Pageable pageable);

@Query("from Person p where p.name = :n")
@Transactional
Person queryByName(String n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ private void processMethodInfo(MethodMatchContext methodMatchContext, MethodMatc
}

builder.member(AnnotationMetadata.VALUE_MEMBER, query);
builder.member(DataMethodQuery.META_MEMBER_NATIVE, element.booleanValue(Query.class,
DataMethodQuery.META_MEMBER_NATIVE).orElse(false));

addQueryDefinition(methodMatchContext,
builder,
Expand Down

0 comments on commit 1352324

Please sign in to comment.