Announcement Announcement Module
Collapse
No announcement yet.
Default sorting on CrudRepository by annotation? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Default sorting on CrudRepository by annotation?

    Hi,

    I know it is possible to provide a method signature with parameter @SortDefault to provide a default sorting.
    But can I also apply this sorting annotation at method level somehow, so that I do not have to pass any arguments and force some kind of default sorting?

    Code:
    interface SortedRepository extends CrudRepository<User, Long> {
        @SortDefault(sort = "lastname")
        @PageableDefault(size = 20)
        List<User> findAll();
    }
    So that whenever I use
    Code:
    repo.findAll();
    I don't have to supply two extra parameters just to make use of the default sorting and pagination?

    Further question, if this is not possible on method level: how can I actually call a method that has default annotations as parameter? Do I have to pass "null" argument to use the default values?
    Code:
       List<User> findAll(@SortDefault(sort = "lastname"), @PageableDefault(size = 20));
    
       //findAll(null, null);???
    Last question: how can I at all combine SortDefault and PageableDefault with @Param signature?
    Because, the following will throw an exception:
    Code:
        @Query("SELECT u FROM user u WHERE u.name = :name")
       List<User> test(@Param("name") String name, @PageableDefault(size = 10), Pageable pageable, @SortDefault(sort = "name", direction = Direction.DESC) Sortable sortable);
    Result:
    Code:
    Caused by: java.lang.IllegalArgumentException: Either use @Param on all parameters except Pageable and Sort typed once, or none at all!
        at org.springframework.util.Assert.isTrue(Assert.java:65)
        at org.springframework.data.repository.query.Parameters.assertEitherAllParamAnnotatedOrNone(Parameters.java:268)
        at org.springframework.data.repository.query.Parameters.<init>(Parameters.java:85)
        at org.springframework.data.jpa.repository.query.JpaParameters.<init>(JpaParameters.java:43)
        at org.springframework.data.jpa.repository.query.JpaQueryMethod.createParameters(JpaQueryMethod.java:267)
        at org.springframework.data.jpa.repository.query.JpaQueryMethod.createParameters(JpaQueryMethod.java:48)
        at org.springframework.data.repository.query.QueryMethod.<init>(QueryMethod.java:70)
        at org.springframework.data.jpa.repository.query.JpaQueryMethod.<init>(JpaQueryMethod.java:62)
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:304)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:161)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:84)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
        ... 47 more
    Last edited by membersound; Apr 7th, 2014, 07:00 AM.
Working...
X