How to get the final JPA SQL using Namedqueries

Asked

Viewed 165 times

2

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

Call from the Namequery

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

Used namedquery

@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 on 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 >= ?))"))

I need to see SQL with the parameters "?" with the values passed!

  • Qual JPA preview are using? If you need for more than one, list them.

  • Bruno, I’m wearing it org.eclipse.persistence.jpa.PersistenceProvider JPA 1.0 pure

1 answer

0

No persistence.xml

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

then just look at your log files

  • Samuel is already so in persistence.xml. I am using JPA pure and the log is being displayed the same as the console.

  • Samuel I switched to value="ALL" and in the log it printou the [bind] and I could see that the date being passed really would not result in any record. Another important detail is that it should be included query.setParameter("data",data,TemporalType.DATE) so that the format is the same as defined in the attribute. Thank you. Due to my profile I cannot yet vote in this reply, but thank you!

  • No problems, at your disposal!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.