2
respostas

Problema na Aula 06 - Pesquisas orientadas a objeto com JPQL JPA Hibernate

package br.com.keyloncorp.financas.teste;

import java.util.List;

import javax.persistence.EntityManager; import javax.persistence.Query;

import br.com.keyloncorp.financas.modelo.Conta; import br.com.keyloncorp.financas.modelo.Movimentacao; import br.com.keyloncorp.financas.util.JPAUtil;

public class TesteJPQL {

public static void main(String[] args) {
    EntityManager em = new JPAUtil().getEntityManager();
    em.getTransaction().begin();

    Conta conta = new Conta();
    conta.setId(2);

    String jpql = "select m Movimentacao m where m.conta.id = :pConta";
    Query query = em.createQuery(jpql);
    query.setParameter("pConta", conta);


    List<Movimentacao> resultados = query.getResultList();

    for (Movimentacao movimentacao : resultados) {
        System.out.println("Descrição: " + movimentacao.getDescricao());
        System.out.println("Conta: " + movimentacao.getConta().getId());
    }

    em.getTransaction().commit();
    em.close();
}

}

2 respostas

Oi William

Qual é o problema?

set 23, 2018 3:34:24 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation INFO: HHH000204: Processing PersistenceUnitInfo [ name: financas ...] set 23, 2018 3:34:25 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.3.5.Final} set 23, 2018 3:34:25 PM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found set 23, 2018 3:34:25 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {5.0.4.Final} set 23, 2018 3:34:26 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) set 23, 2018 3:34:26 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/financas] set 23, 2018 3:34:26 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {provider_disables_autocommit=true, user=root, password=****} set 23, 2018 3:34:26 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false set 23, 2018 3:34:26 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections INFO: HHH000115: Hibernate connection pool size: 20 (min=1) Sun Sep 23 15:34:26 GMT-03:00 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. set 23, 2018 3:34:27 PM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect set 23, 2018 3:34:29 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@712ca57b] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. set 23, 2018 3:34:29 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService INFO: HHH000397: Using ASTQueryTranslatorFactory Exception in thread "main" java.lang.IllegalArgumentException: Parameter value [br.com.keyloncorp.financas.modelo.Conta@1698d7c0] did not match expected type [java.lang.Integer (n/a)] at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54) at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:27) at org.hibernate.query.internal.QueryParameterBindingImpl.validate(QueryParameterBindingImpl.java:90) at org.hibernate.query.internal.QueryParameterBindingImpl.setBindValue(QueryParameterBindingImpl.java:55) at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:493) at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:106) at br.com.keyloncorp.financas.teste.TesteJPQL.main(TesteJPQL.java:24)