mardi 14 avril 2015

Spring Data - Page Request - order by function

I am trying to implement pagination in Java application based on Spring 4.1.1.RELEASE and Hibernate 4.2.16.Final.


I have a following code:



PageRequest pageRequest = new PageRequest(pageNumber, pageSize, new Sort(new Order(Direction.DESC, "name"))
Page page = myRepository.findAll(specification, pageRequest);


and everything works fine. However now I would like to order by sql function value, for example: ORDER BY CHAR_LENGTH(name).


It works perfectly when I input such clause in @org.springframework.data.jpa.repository.Query, for example:



public class MyRepository {
@Query("select e from MyEntity e order by CHAR_LENGTH(e.name)")
public List<MyEntity> findAllOrderedByNameLength();
}


unfortunately I am not able to do it with PageRequest. When I pass: ORDER BY CHAR_LENGTH(name) to PageRequest as a property:



PageRequest pageRequest = new PageRequest(pageNumber, pageSize, new Sort(new Order(Direction.DESC, "ORDER BY CHAR_LENGTH(name)"))
Page page = myRepository.findAll(specification, pageRequest);


the following exception is being thrown:



Caused by: org.springframework.data.mapping.PropertyReferenceException: No property CHAR found for type MyEntity!

Aucun commentaire:

Enregistrer un commentaire