Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.query.sqm.sql.internal.InstantiationException: Error performing dynamic instantiation : br.com.alura.lojaoficial.vo.RelatorioDeVendasVo
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:166)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:193)
at org.hibernate.query.spi.AbstractQuery.list(AbstractQuery.java:1373)
at org.hibernate.query.Query.getResultList(Query.java:131)
at br.com.alura.lojaoficial.dao.PedidoDAO.relatorioDeVendas(PedidoDAO.java:48)
at br.com.alura.lojaoficial.testes.CadastroDePedido.main(CadastroDePedido.java:55)
Caused by: org.hibernate.query.sqm.sql.internal.InstantiationException: Error performing dynamic instantiation : br.com.alura.lojaoficial.vo.RelatorioDeVendasVo
at org.hibernate.sql.results.graph.instantiation.internal.DynamicInstantiationAssemblerConstructorImpl.assemble(DynamicInstantiationAssemblerConstructorImpl.java:58)
at org.hibernate.sql.results.internal.StandardRowReader.readRow(StandardRowReader.java:93)
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:66)
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:24)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.doExecuteQuery(JdbcSelectExecutorStandardImpl.java:228)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.executeQuery(JdbcSelectExecutorStandardImpl.java:146)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.list(JdbcSelectExecutorStandardImpl.java:74)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.lambda$new$0(ConcreteSqmSelectQueryPlan.java:80)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:248)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:191)
at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:513)
at org.hibernate.query.spi.AbstractQuery.list(AbstractQuery.java:1364)
... 3 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.hibernate.sql.results.graph.instantiation.internal.DynamicInstantiationAssemblerConstructorImpl.assemble(DynamicInstantiationAssemblerConstructorImpl.java:52)
... 14 more
Codigo:
package br.com.alura.lojaoficial.dao;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import br.com.alura.lojaoficial.modelo.Pedido;
import br.com.alura.lojaoficial.vo.RelatorioDeVendasVo;
public class PedidoDAO {
private EntityManager em;
// Recebendo o entitymanger
public PedidoDAO(EntityManager em) {
this.em = em;
}
// Usar o entity manager
public void cadastrar(Pedido pedido) {
em.persist(pedido);
}
// Buscar por um ID
public Pedido buscarPorId(Long id) {
return em.find(Pedido.class, id);
}
// Valor total de todos os pedidos - Consulta com função de agregação
public BigDecimal valorTotalVendido() {
String jqpl = "SELECT SUM(p.valorTotal) FROM Pedido p";
return em.createQuery(jqpl, BigDecimal.class)
.getSingleResult();
}
// Relatorio de pedidos de 3 informações distintas
// Cada linha é um array de object
public List<RelatorioDeVendasVo> relatorioDeVendas(){
String jpql = "SELECT new br.com.alura.lojaoficial.vo.RelatorioDeVendasVo("
+ "produto.nome,"
+ "SUM(item.quantidade),"
+ "MAX(pedido.data)) "
+ "FROM Pedido pedido "
+ "JOIN pedido.itens item "
+ "JOIN item.produto produto "
+ "GROUP BY produto.nome "
+ "ORDER BY item.quantidade DESC";
return em.createQuery(jpql, RelatorioDeVendasVo.class).getResultList();
}
}