Announcement Announcement Module
Collapse
No announcement yet.
MongoDB orderBy with findAll in CrudRepository Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MongoDB orderBy with findAll in CrudRepository

    I have been using the Repository feature of spring-data-mongodb with some success. However, I have a problem trying to define a method name that will find all and sort. There is no selection, so a method like findAllOrderByNameAsc(). would be perfect. However, this fails with

    Caused by: org.springframework.data.mapping.PropertyReference Exception: No property asc found for type java.lang.String

    I have also tried findCountriesOrderByNameAsc(). I also tried leaving off the Asc from the method name, but that results in a different error at runtime.

    Any thoughts?

  • #2
    The prefix of a finder method has to be determinated by a "By" as we support e.g. "Distinct" in the prefix. So what you could try is "findByOrderByNameAsc()". This doesn't read very well and I am not sure this will parse correctly but should do the trick if it does. Beyond that you could simply use findAll(Sort sort) of PagingAndSortingRepository and pipe in a new Sort(Direction.ASC, "name") into it.

    Comment


    • #3
      A solution

      Well, findByOrderByNameAsc() does not work as a method name. That gives an exception at runtime ...

      Code:
      SEVERE: Servlet.service() for servlet [rest] in context with path [/itr] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
      java.lang.NullPointerException
      	at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentPropertyPath(AbstractMappingContext.java:218)
      	at org.springframework.data.mongodb.repository.query.MongoQueryCreator.create(MongoQueryCreator.java:100)
      	at org.springframework.data.mongodb.repository.query.MongoQueryCreator.create(MongoQueryCreator.java:47)
      	at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109)
      	at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88)
      	at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73)
      	at org.springframework.data.mongodb.repository.query.PartTreeMongoQuery.createQuery(PartTreeMongoQuery.java:69)
      	at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:75)
      	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:313)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
      	at $Proxy219.findByOrderByNameAsc(Unknown Source)
      However, converting to a PagingAndSortingRepository and using the generated findAll(Sort) works well. Unfortunately, this does require passing the name of the property to sort on, and is therefore a more delicate, but it works. I suppose I can add the method to the repository myself though, if I really want to.

      Thanks!!

      Comment

      Working...
      X