Achava que apenas o meu lança a exceção, mas quanto tentei com a versão do instrutor, o mesmo erro ocorreu:
(String nome,
** public List<Produto> buscarPorParametrosSemCriteriaINSTRUTOR(String nome,
BigDecimal preco, LocalDate dataCadastro) {**
String jpql = "SELECT p FROM Produto p WHERE 1=1 ";
if (nome != null && !nome.trim().isEmpty()) {
jpql = " AND p.nome = :nome ";
}
if (preco != null) {
jpql = " AND p.preco = :preco ";
}
if (dataCadastro != null) {
jpql = " AND p.dataCadastro = :dataCadastro ";
}
TypedQuery<Produto> query = em.createQuery(jpql, Produto.class);
if (nome != null && !nome.trim().isEmpty()) {
query.setParameter("nome", nome);
}
if (preco != null) {
query.setParameter("preco", preco);
}
if (dataCadastro != null) {
query.setParameter("dataCadastro", dataCadastro);
}
return query.getResultList();
}
Exceção:
(null, ?, ?, ?, ?, ?)
mai. 17, 2024 6:38:08 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:2: unexpected token: AND
mai. 17, 2024 6:38:08 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:2: unexpected token: AND
line 1:2: unexpected token: AND
at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:212)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:294)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:162)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:604)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:716)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:816)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
at dao.ProdutoDao.buscarPorParametrosSemCriteriaINSTRUTOR(ProdutoDao.java:100)
at testes.TesteCriteria.main(TesteCriteria.java:22)
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: AND near line 1, column 2 [ AND p.nome = :nome ]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:725)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:816)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
at dao.ProdutoDao.buscarPorParametrosSemCriteriaINSTRUTOR(ProdutoDao.java:100)
at testes.TesteCriteria.main(TesteCriteria.java:22)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: AND near line 1, column 2 [ AND p.nome = :nome ]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74)
at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:301)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:162)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:604)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:716)