How to get the final SQL from JPA using NamedQueries

2

How to get the SQL that is being executed by JPA when we use a createNamedQuery. I would like to have SQL executed to understand where the query problem is:

Calling NameQuery

    EntityManager em = JPAUtil.getEntityManager();
    Query query = em.createNamedQuery("buscarTodosItensAnalisePorCpfCnpjEDataMinima");
    query.setParameter("cpfCnpj", cpf);
    System.out.println("SQL : "  + query.toString());
    query.setParameter("data", dataPeriodo);

NamedQuery used

@NamedQueries({
@NamedQuery(name="buscarTodosItensAnalisePorCpfCnpj",query="SELECT i FROM ItemAnalise i WHERE i.cpfCnpj = :cpfCnpj"),
@NamedQuery(name="buscarTodosItensAnalisePorCpfCnpjEDataMinima",query="SELECT i FROM ItemAnalise i WHERE i.cpfCnpj = :cpfCnpj AND i.dataRegistro >= :data")

})

I'm using:

System.out.println("SQL : "  + query.toString());

The result in the console is:

SQL : EJBQueryImpl(ReadAllQuery(name="buscarTodosItensAnalisePorCpfCnpjEDataMinima" referenceClass=ItemAnalise sql="SELECT id, atividade_id, cpfcnpj, data_atualizacao, data_registro, login, situacao FROM item_analise WHERE ((cpfcnpj = ?) AND (data_registro >= ?))"))

Do I need to see SQL with the "?" with past values!

    
asked by anonymous 15.09.2015 / 02:28

1 answer

0

No persistence.xml

<properties>
    <property name="eclipselink.logging.level" value="FINEST"/>
</properties>

Then just look at your log files

    
15.09.2015 / 04:27