Pessoal,
Estou fazendo uma consulta com NamedQuery e Order By que está me retornando o seguinte erro:
14:21:25 WARN [JDBCExceptionReporter] SQL Error: 1008, SQLState: S0001
14:21:25 ERROR [JDBCExceptionReporter] The SELECT item identified by the ORDER BY number 1 contains a variable as part of the expression identifying a column position. Variables are only allowed when ordering by an expression referencing a column name.
Hibernate: select tbletqweb0_.cvCodEtqWeb as cvCodEtq1_11_, tbletqweb0_.ccNomEtqWeb as ccNomEtq2_11_, tbletqweb0_.ccVarEtqWeb as ccVarEtq3_11_ from tblEtqWeb tbletqweb0_ where ?=? order by ?
@Entity
@Table(name = "tblEtqWeb")
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@NamedQueries({
@NamedQuery(name = "etqWeb.where", query = "select t from TblEtqWeb t where ccVarEtqWeb = ?",
hints = { @QueryHint(name = "org.hibernate.cacheable", value = "true") }),
@NamedQuery(name = "etqWeb2.where", query = "select t from TblEtqWeb t where ? = ? order by ?",
hints = { @QueryHint(name = "org.hibernate.cacheable", value = "true") })
})
public class TblEtqWeb implements Serializable, InterfaceValidaAtributo {
O erro acontece na NemedQuery “etqWeb2.where”, pelo que eu entendi, acontece porque estou passando o parametro “ccVarEtqWeb” e o hibernate está interpretando como uma célula da coluna, e não como a coluna toda…
É isso mesmo? Como posso resolver?
Chamada da NamedQuery:
DAO<TblEtqWeb> dao = new DAO<TblEtqWeb>(TblEtqWeb.class);
List<TblEtqWeb> lista = dao.buscaQuery("etqWeb2.where", param1, param2, EtqWeb.nomOrd);
DAO:
@SuppressWarnings("unchecked")
public List<T> buscaQuery(String select, String... param) {
List<T> t;
try {
EntityManager em = new JPAUtil().getEntityManager();
Query query = em
.createNamedQuery(select);
int count = 1;
for (String aux : param) {
query.setParameter(count++, aux);
}
t = (List<T>) query.getResultList();
} catch (NoResultException e) {
t = null;
}
return t;
}
O erro só acontece com o “Order By”, sem ele funciona normal…