0
I’m having to make a SELECT in JPA that contains an ORDER BY by selecting from another table.
Small representation of tables:
When I did by SQL it was normal, but when turning into JPA, it did not work, and if I take out JPA it works normally.
2018-03-28 16:36:20 ERROR stderr - java.lang.Illegalargumentexception: org.hibernate.hql.internal.Ast.Querysyntaxexception: Unexpected AST Ode: query [SELECT v.supplier FROM br.com.souvizinho.modelo.Vinculocategoristhoper v WHERE v.category IN ( SELECT o.category FROM br.com.souvizinho.modelo.Orcamentocategoria o WHERE o.usercad = :usercad AND o.condominio = :condominio AND o.orcamento = 1 AND o. asset = 1) AND v.asset = 1 GROUP BY v.supplier ORDER BY ( SELECT AVG(n.nota) FROM br.com.souvizinho.modelo.Avaliacaofornecedor n WHERE n. supplier = v.supplier ) DESC]
SQL:
SELECT vinculocat11.fornecedor
FROM tbvinculocategoriafornecedor vinculocat11
CROSS JOIN tbfornecedor fornecedor12
WHERE vinculocat11.fornecedor = fornecedor12.nrseq
AND ( vinculocat11.categoria IN (SELECT orcamentoc13.categoria
FROM tborcamentocategoria orcamentoc13
CROSS JOIN tbcategoriafornecedor
categoriaf14
WHERE orcamentoc13.categoria =
categoriaf14.nrseq
AND orcamentoc13.usercad = 56
AND orcamentoc13.condominio = 2
AND orcamentoc13.orcamento = 1
AND orcamentoc13.ativo = 1) )
AND vinculocat11.ativo = 1
GROUP BY vinculocat11.fornecedor
ORDER BY (SELECT Avg(n.nota)
FROM tbavaliacaofornecedor AS n
WHERE n.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();
You could put the data structure of your BD. Only with CREATE methods
– Victor Henrique
Hello tries to name parameters other than columns, or set a prefix/suffix
– Newton Pasqualini Filho