6

I am using Spring data rest and am trying to customize the query for two of the overloaded findAll methods using @Query. However, when I attempt this I receive this error:

java.lang.IllegalStateException: Ambiguous search mapping detected. Both public abstract org.springframework.data.domain.Page courses.CourseRepository.findAll(org.springframework.data.domain.Pageable) and public abstract java.lang.Iterable courses.CourseRepository.findAll(org.springframework.data.domain.Sort) are mapped to /findAll! Tweak configuration to get to unambiguous paths!

When these methods are called the URL does not contain /findAll by convention. A URL for retrieving unsorted courses (but using paging) is

http://localhost:8080/v1/courses

and sorting is

http://localhost:8080/v1/courses?sort=title

Here's the relevant code, which is fairly straightforward:

public interface CourseRepository extends PagingAndSortingRepository<Course, Long> {

  @Override
  @Query("SELECT c FROM Course c WHERE c.visible = 'Yes'")
  Page<Course> findAll(Pageable pageable);

  @Override
  @Query("SELECT c FROM Course c WHERE c.visible = 'Yes'")
  Iterable<Course> findAll(Sort sort);
}

I've also attempted using an @NamedQuery on the Course entity with the same error message.

EDIT:

I have discovered that the findAll(Pageable pageable) method has Sorting 'built-in', however it does not sort the results. When I hit the following URL and attempt to sort by title the results are clearly not sorted by title. However, without the custom @Query the results are sorted.

http://localhost:8080/v1/courses?sort=title

Jaumzera
  • 2,305
  • 1
  • 30
  • 44
acvcu
  • 2,476
  • 10
  • 40
  • 56
  • Do you have issue only for not sorting? can you send the code piece for calling the repository method Page findAll(Pageable pageable);. Actually where you would configure for sorting. – Shaan Mar 23 '16 at 15:46
  • @PremkumarMathivanan since it is Spring Data Rest the call to findAll is behind-the-scenes. I do not have specific code that calls that method. – acvcu Mar 23 '16 at 17:27
  • Can you confirm did I answer you close by? – Shaan Mar 23 '16 at 21:37

2 Answers2

4

Try

http://localhost:8080/v1/courses?sort=title,asc

http://localhost:8080/v1/courses?sort=title,desc

Or if you are using older Spring version, then take a look at this doc:

http://docs.spring.io/spring-data/rest/docs/1.1.x/reference/html/paging-chapter.html

aBnormaLz
  • 809
  • 6
  • 22
2

I guess you are trying overloading findAll method of Spring-Data. This can be achieved only by CustomRepository.

Here is the useful link for you to guide about creating CustomRepository.

You can defined a class with the custom methods for tweaking Spring-Data's method based on the need.

Spring documentation link for the same

Jaumzera
  • 2,305
  • 1
  • 30
  • 44
Shaan
  • 588
  • 1
  • 4
  • 15