I'm having to do a SELECT in JPA that contains an ORDER BY by selecting from another table.
Small representation of tables:
WhenIdiditforSQLitwasnormal,butwhenIturneditonJPA,itdidnotwork,andifItakeouttheJPAitworksnormally.
2018-03-2816:36:20ERRORstderr- java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException:unexpectedAST node:query[SELECTv.providerFROM br.com.souvizinho.modelo.VinculoCategoriaCorporatevWHERE v.categoryIN(SELECTo.categoryFROM br.com.souvizinho.modelo.OrcamentoCategoriaoWHEREo.usercad= :usercadANDo.condominio=:condominiumANDo.orcamento=1AND o.active=1)ANDv.active=1GROUPBYv.derderORDERBY(SELECT AVG(n.nota)FROMbr.support.model.AvaliationSupportnWHERE n.seller=v.seller)DESC]
SQL:
SELECTvinculocat11.fornecedorFROMtbvinculocategoriafornecedorvinculocat11CROSSJOINtbfornecedorfornecedor12WHEREvinculocat11.fornecedor=fornecedor12.nrseqAND(vinculocat11.categoriaIN(SELECTorcamentoc13.categoriaFROMtborcamentocategoriaorcamentoc13CROSSJOINtbcategoriafornecedorcategoriaf14WHEREorcamentoc13.categoria=categoriaf14.nrseqANDorcamentoc13.usercad=56ANDorcamentoc13.condominio=2ANDorcamentoc13.orcamento=1ANDorcamentoc13.ativo=1))ANDvinculocat11.ativo=1GROUPBYvinculocat11.fornecedorORDERBY(SELECTAvg(n.nota)FROMtbavaliacaofornecedorASnWHEREn.fornecedor=vinculocat11.fornecedor)DESC
JPA:
fornecedores=this.entityManager.createQuery("SELECT v.fornecedor FROM VinculoCategoriaFornecedor v WHERE v.categoria IN "
+ " ( SELECT o.categoria FROM OrcamentoCategoria o WHERE o.usercad = :usercad "
+ " AND o.condominio = :condominio AND o.orcamento = 1 AND o.ativo = 1) "
+ " AND v.ativo = 1 GROUP BY v.fornecedor ORDER BY "
+ " ( SELECT AVG(n.nota) FROM AvaliacaoFornecedor n WHERE n.fornecedor = v.fornecedor ) DESC")
.setParameter("usercad", usercad).setParameter("condominio", condominio).getResultList();