hibernate show_sql value - java

this is example of how sql is showed when using show_sql=true
Hibernate:
select
propertyse0_.entity_name as entity1_3_0_,
propertyse0_.entity_id as entity2_3_0_,
propertyse0_.entity_key as entity3_3_0_,
propertyse0_.key_type as key4_3_0_,
propertyse0_.boolean_val as boolean5_3_0_,
propertyse0_.double_val as double6_3_0_,
propertyse0_.string_val as string7_3_0_,
propertyse0_.long_val as long8_3_0_,
propertyse0_.int_val as int9_3_0_,
propertyse0_.date_val as date10_3_0_
from
OS_PROPERTYENTRY propertyse0_
where
propertyse0_.entity_name=?
and propertyse0_.entity_id=?
and propertyse0_.entity_key=?
possible to show value to gather with the sql rather than '?'

Set your logging leven to "TRACE".
In your log4j.properties (assuming your using Log4J):
log4j.logger.org.hibernate=TRACE
Will result in lots of logging tough...

You need to set up your logging framework to log this level of details. See here for the various loggers that Hibernate uses, and how to use them.
The particular one that you want is:
org.hibernate.type - Log all JDBC parameters

not directly. you can use log4jdbc to log all the data that is sent over jdbc. It has a logger that inlines the prepared statement values.

Related

spring-boot-data-source-decorator with p6spy are not logging insert statements

I am using p6spy to log the sql statements.we are using springboot/hibernate for Java ORM mapping.I see only select statements are getting loggged in spy.log.when insert statemnets are executing in the code I see only commmit is coming in the log but insert statements are not coming.
|connection|commit||
|connection|statement | select * from emp_id where id=1234
https://github.com/gavlyukovskiy/spring-boot-data-source-decorator
# Register P6LogFactory to log JDBC events
decorator.datasource.p6spy.enable-logging=true
# Use com.p6spy.engine.spy.appender.MultiLineFormat instead of com.p6spy.engine.spy.appender.SingleLineFormat
decorator.datasource.p6spy.multiline=true
# Use logging for default listeners [slf4j, sysout, file, custom]
decorator.datasource.p6spy.logging=file
# Log file to use (only with logging=file)
decorator.datasource.p6spy.log-file=spy.log
# Class file to use (only with logging=custom). The class must implement com.p6spy.engine.spy.appender.FormattedLogger
decorator.datasource.p6spy.custom-appender-class=my.custom.LoggerClass
# Custom log format, if specified com.p6spy.engine.spy.appender.CustomLineFormat will be used with this log format
decorator.datasource.p6spy.log-format=
# Use regex pattern to filter log messages. If specified only matched messages will be logged.
decorator.datasource.p6spy.log-filter.pattern=
# Report the effective sql string (with '?' replaced with real values) to tracing systems.
# NOTE this setting does not affect the logging message.
decorator.datasource.p6spy.tracing.include-parameter-values=true

Inspect limit parameter values for SQLServer2008Dialect

I have an hibernate query with paging. I want to see parameter values related to the paging. I use SQLServer2008Dialect so my query looks like:
WITH query AS (/* criteria query */ select
ROW_NUMBER() OVER (
order by
this_.event_id desc)
...
...
) SELECT
*
FROM
query
WHERE
__hibernate_row_nr__ BETWEEN ? AND ?
I set
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.use_sql_comments = true
in hibernate config used by my application.
I enabled also logging query parameters in log4j by setting
org.hibernate.type.descriptor.sql.BasicBinder to TRACE level.
This works fine for fine for all other parameters but I can't see parameter values related to the row number limit. Is there any way to inspect which are the current limit parameter values?
In the past I've always used that configuration with hibernate, but recently I've switched to use BoneCP which allows to me log the exact query being executed to avoid having the same problem you are facing.
If you can use BoneCP, you could configure your data source to enable logging statements, and set a log4j logger for com.jolbox.bonecp to debug, and you are set.

How can I check what parameter is finally passed with the query?

Is there any way I can check, how is the query being framed or what values are being passed ? I want to check for this query :
String hql = "from Scheduled where stime <= current_time()"; // QUERY
List list = session.createQuery(hql).list();
I want to know what value of current_time() is being sent ?
current_time() is not replaced with a specific time in hibernate - it is passed as part of the SQL to the database and is evaluated there. Therefore, current_time() will be whatever the current time is on the database server at the time the statement is executed.
You can enable Hibernate logging and these two params should be of help to you:
org.hibernate.SQL - Log all SQL DML statements as they are
executed
org.hibernate.type - Log all JDBC parameters
If your underlying database is SQL Server, then another option is SQL Profiler.
Here are entries from log4j.properties which works for me to print hibernate queries and parameters.
log4j.logger.org.hibernate=DEBUG, stdout
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE,stdout

change log4j Logging level from INFO to DEBUG

In my java code, the following line
getHibernateTemplate().save(billingCompany);
is printing a logger message
2013-11-11 14:05:20,962 INFO [STDOUT] Hibernate: insert into
billing_log_company (COMPANY_ID, BILLING_LOG_ID) values (?, ?)
But actually I need DEBUG in place of INFO like this:
2013-11-11 14:05:20,962 DEBUG [STDOUT] Hibernate: insert into
billing_log_company (COMPANY_ID, BILLING_LOG_ID) values (?, ?)
I mean that we are not writing logger.info. logger.debug etc..
getHibernateTemplate().save(billingCompany) is printing that logger msg in INFO mode.
Hibernate messages it self is in hibernate source code, you can't change that. However, you can change Hibernate log level to warn or debug. but this will fill your console with a lot of logs
In your log properties file change
log4j.logger.org.hibernate=info
to
log4j.logger.org.hibernate=debug
or
log4j.logger.org.hibernate=warn
I don't understand your question. If you want DEBUG rather than INFO in the spring hibernate template then you'll have to change their source code. If you want to use DEBUG in our own code, just use
logger.debug(...)
Benjamin was clear in his approach.
You must be using some api to log like log4j, promatter...
most logger api has different methods for logging different scenarios.
Take log4j for example,
static Logger loggerfile = Logger.getLogger(ThisClass.class.getName());//creating logger instance
loggerFile.debug("debug");
loggerFile.info("info");
loggerFile.error("erre");

Query from PreparedStatement

Is there any way to get the Oracle query from PreparedStatement .I know its not possible as such I can't use log4jdbc or p6spy as it is a secured application and using this will create bigger problems..toString won't work as I am using Oracle? I can't change PreparedStatement to Statement either.
If only need for debug time then You can use DebuggableStatement follow this article
I don't think you should be doing it this way, as there is no officially documented API for this.
If you can mess with the code, why cannot you use log4jdbc ?
Oracle JDBC also supports java.util.logging, which you could try to enable.
If you are just interested in the SQL itself, you can turn on session tracing on the Oracle server.
Or maybe you can put your code to where the statement is being prepared (using something like #pinichi is suggesting)?
But just for fun, poking around with the debugger, with my version of Oracle JDBC, I can do
if (stmt instanceof oracle.jdbc.driver.OraclePreparedStatement) {
String x = ((oracle.jdbc.driver.OraclePreparedStatement) stmt)
.getOriginalSql();
System.out.println(x);
}
If you just want to check SQL statement you can also go straight to the database and check v$sql table.
There you can find all sqls and other information about query. More info: http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/dynviews_2113.htm

Categories