Announcement Announcement Module
Collapse
No announcement yet.
Method name query derivation on findAll with a sort clause Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Method name query derivation on findAll with a sort clause

    Hello,

    I have a Maven build on a Spring Data JPA project with some integration tests against the MySQL database.

    I have a query to sort all records of a table.

    The following works fine and the integration test is successful:

    @Query("SELECT a FROM Admin a ORDER BY a.lastname ASC, a.firstname ASC")
    public Page<Admin> findAll(Pageable page);

    But if I replace the above finder by the following one:

    public Page<Admin> findAllOrderByLastnameAscFirstnameAsc(Pageable page);

    then I get an error message:

    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'adminRepositoryImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private com.thalasoft.learnintouch.core.jpa.repository.Adm inRepository com.thalasoft.learnintouch.core.jpa.repository.Adm inRepositoryImpl.adminRepository; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'adminRepository': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReference Exception: No property asc found for type java.lang.String

    And I wonder what is happening here...

    Kind Regards,

  • #2
    According to the docs[1], the parsers looks for find...By, and scans for criteria after the by-part, so he uses the by of OrderBy as a delimiter and hence tries to start parsing at Lastname and looks for a property Asc in the field Lastname. Not sure if findAllByOrderByLastnameAscOrderByFirstnameAsc would work ...
    However, if you have your repository extend PagingAndSortingRepository you'll get two methods to which you can supply sort parameters via a Sort instance, either directly or as part of a Pageable.

    [1] http://static.springsource.org/sprin...query-creation

    Edit: added 'All' to findAllByOrderByLastnameAscOrderByFirstnameAsc
    Last edited by Johannes Mockenhaupt; Jun 7th, 2013, 01:27 PM.

    Comment


    • #3
      Hi Johannes,

      Thanks for pointing that doc to me. Indeed I knew about the Sort parameter but wondered if I could use the method name derivation feature for it.

      Cheers,

      Comment

      Working...
      X