Skip to content

printing TypedQuery in console

How to sysout the TypedQuery in eclipse console. Am trying with .toString() and its not working

Please find my java code below.

TypedQuery<PartFactor> query = entityManager
        .createQuery("from PART where partNo = :partNo ", Part.class);
    query.setParameter("partNo", partNo);
    System.out.println("isPartNoExists -->"+query.toString());


You need to enable logging for the the following categories:

org.hibernate.SQL   - set to debug to log all SQL DML statements as they are executed
org.hibernate.type - set to trace to log all JDBC parameters

So a log4j configuration could look like:

# logs the SQL statements 

# Logs the JDBC parameters passed to a query 

The first is equivalent to hibernate.show_sql=true legacy property, the second prints the bound parameters among other things.

Another solution (non hibernate based) would be to use a JDBC proxy driver like P6Spy.

In case of spring boot is being used , just config this :


        org.hibernate.SQL: DEBUG
        org.hibernate.type: TRACE

and nothing more.

Your log will be something like this:

2020-11-01 | DEBUG | o.h.SQL:127 - insert into Person (id, name, title, id) values (?, ?, ?, ?)