quinta-feira, 27 de abril de 2017

JPA: ORA-01791: não é uma expressão de SELECT (Order By)

Problema: não é uma expressão de SELECT

TypedQuery<CheckList> q = em.createQuery("select distinct"  
                       + " checkList "  
                       + " from " + CheckList.class.getName() + " checkList"  
                       + " where"  
                       + " checkList.dataCadastro = :dataPesquisa "  
                       + " order by checkList.usuarioResponsavel.nome", CheckList.class);  

Cause:
There is an incorrect ORDER BY item. The query is a SELECT DISTINCT query with an ORDER BY clause. In this context, all ORDER BY items must be constants, SELECT list expressions, or expressions whose operands are constants or SELECT list expressions.


Solução: adicionar um join e fazer a ordem através dele.
TypedQuery<CheckList> q = em.createQuery("select distinct"  
           + " checkList "  
           + " from " + CheckList.class.getName() + " checkList"  
           + " left join fetch checkList.usuarioResponsavel usuarioResponsavel"  
           + " where"  
           + " checkList.dataCadastro = :dataPesquisa "  
           + " order by usuarioResponsavel.nome", CheckList.class);