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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With