Results 1 to 3 of 3

Thread: MongoDB orderBy with findAll in CrudRepository

  1. #1
    Join Date
    Aug 2005
    Location
    Halifax, NS, Canada
    Posts
    50

    Default 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?
    Don Laidlaw
    Infor Global Solutions

  2. #2
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    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.

  3. #3
    Join Date
    Aug 2005
    Location
    Halifax, NS, Canada
    Posts
    50

    Thumbs up 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!!
    Don Laidlaw
    Infor Global Solutions

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •