NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed. (sponsored)
In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.
Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.
Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Hibernate, in the end, is all about dispatching SQL statements. SQL is at the heart of communication with an RDBMS system,
and it is extremely important when struggling through performance problems or other bugs, that you know what is going on.
After all, knowing the executed SQL allows you to determine the number of queries to complete an O/R mapping task, not
to mention that seeing SQL queries is critical to understanding how to optimize the queries via indices and other database
settings.
There are two ways (due to the legacy of Hibernate) to enable SQL logging. The first is to simply enable SQL logging in the
Hibernate configuration properties by setting the
hibernate.show_sql
property to true:
SessionFactory sf = new Configuration()
.setProperty("hibernate.show_sql", "true")
// ...
.buildSessionFactory();
This is a nice quick and dirty solution, but it is relatively inflexible. SQL statements are always logged to
System.out
when that property is enabled, and on some servers, System.out isn't accessible by the
average developer; or is cluttered with a million other log statements.
Alternatively, Hibernate uses the
Apache-Jakarta Commons Logging
package, which means that it will utilize
log4j
,
java.util.logging
, or potentially another logging framework. Typically, log messages
are sent to commons logging, and then they are dispatched to one of these logging frameworks. Then, those logging frameworks
determine where the message should be sent (log files, sockets, emails, database records, system out, system err, etc).
It is easy to see how using these log frameworks will increase the flexibility of log statements. With Hibernate, simply
setting the
org.hibernate.SQL
logger to 'DEBUG' or 'ALL' will ensure that all SQL statements are logged to the
console. Here is an example of the log4j configuration for this:
Note that in this case I have turned 'additivity' off for the
org.hibernate.SQL
logger. I like using this
setting because it ensures that log messages aren't bubbled up to parent handlers - it is, of course, optional depending
on your logging preference.
Hibernate uses prepared statements internally, so it doesn't ever have the SQL in a format where they values would be embedded. That means that to produce SQL statements with the values embedded, it would have to generate them just for logging.
While it may exist, I'm fairly certain that form of logging is not available in Hibernate directly.
Set up a log4j category for org.hibernate.type. Get it to write out to the same log file as the org.hibernate.SQL
The type one will list the parameters for you after the SQL e.g.
> Set up a log4j category for org.hibernate.type. Get
> it to write out to the same log file as the
> org.hibernate.SQL
> The type one will list the parameters for you after
> the SQL e.g.
> 2006-07-28 09:57:12,082 DEBUG
> org.hibernate.type.LongType - binding '180030' to
> parameter: 8
Note: Hibernate actually uses commons logging "trace" level, so with latest log4j versions (supporting "trace" as well) this level must be enabled. Older versions treated trace just as debug, so enabling debug was enough.
So how can we get parameterized values in Sql Statement while SQL statements are enabled in Configuration file.
There certainly have been performance issues with Java.
We've been working really hard on them.
The primary way we've attacked the problem is with advanced virtual machines. The performance
has been getting very nice. --James Gosling, 1999.
Hibernate: Log SQL Statements
At 11:06 PM on Aug 21, 2005, R.J. Lorimer wrote:
Fresh Jobs for Developers Post a job opportunity
Hibernate, in the end, is all about dispatching SQL statements. SQL is at the heart of communication with an RDBMS system, and it is extremely important when struggling through performance problems or other bugs, that you know what is going on. After all, knowing the executed SQL allows you to determine the number of queries to complete an O/R mapping task, not to mention that seeing SQL queries is critical to understanding how to optimize the queries via indices and other database settings.
There are two ways (due to the legacy of Hibernate) to enable SQL logging. The first is to simply enable SQL logging in the Hibernate configuration properties by setting the
hibernate.show_sqlproperty to true:SessionFactory sf = new Configuration() .setProperty("hibernate.show_sql", "true") // ... .buildSessionFactory();This is a nice quick and dirty solution, but it is relatively inflexible. SQL statements are always logged to
System.outwhen that property is enabled, and on some servers, System.out isn't accessible by the average developer; or is cluttered with a million other log statements.Alternatively, Hibernate uses the Apache-Jakarta Commons Logging package, which means that it will utilize
log4j,java.util.logging, or potentially another logging framework. Typically, log messages are sent to commons logging, and then they are dispatched to one of these logging frameworks. Then, those logging frameworks determine where the message should be sent (log files, sockets, emails, database records, system out, system err, etc). It is easy to see how using these log frameworks will increase the flexibility of log statements. With Hibernate, simply setting theorg.hibernate.SQLlogger to 'DEBUG' or 'ALL' will ensure that all SQL statements are logged to the console. Here is an example of the log4j configuration for this:Note that in this case I have turned 'additivity' off for the
org.hibernate.SQLlogger. I like using this setting because it ensures that log messages aren't bubbled up to parent handlers - it is, of course, optional depending on your logging preference.Until next time,
R.J. Lorimer
Contributing Editor -rj -at- javalobby.orgAuthor -http://www.coffee-bytes.comSoftware Consultant -http://www.crosslogic.com9 replies so far (
Post your own)
Re: Hibernate: Log SQL Statements
Is it possible to get Hibernate to output the unparameterized SQL?By default, it seems to output SQL like "where assessment0_.id=?". I'd like to be able to see what value it is passing for ?.
Re: Hibernate: Log SQL Statements
Hibernate uses prepared statements internally, so it doesn't ever have the SQL in a format where they values would be embedded. That means that to produce SQL statements with the values embedded, it would have to generate them just for logging.While it may exist, I'm fairly certain that form of logging is not available in Hibernate directly.
Re: Hibernate: Log SQL Statements
Set up a log4j category for org.hibernate.type. Get it to write out to the same log file as the org.hibernate.SQLThe type one will list the parameters for you after the SQL e.g.
2006-07-28 09:57:12,061 DEBUG org.hibernate.SQL - insert into BASKET_LINE_ALLOC (LAST_UPDATED, QUANTITY, CUSTOMER_REF, NOTES, BRANCH_ID, FUND_ID, TEMPLATE_ID,
BASKET_LINE_ALLOC_ID) values (?, ?, ?, ?, ?, ?, ?, ?)
2006-07-28 09:57:12,081 DEBUG org.hibernate.type.TimestampType - binding '2006-07-28 09:57:12' to parameter: 1
2006-07-28 09:57:12,081 DEBUG org.hibernate.type.IntegerType - binding '3' to parameter: 2
2006-07-28 09:57:12,082 DEBUG org.hibernate.type.StringType - binding '' to parameter: 3
2006-07-28 09:57:12,082 DEBUG org.hibernate.type.StringType - binding '' to parameter: 4
2006-07-28 09:57:12,082 DEBUG org.hibernate.type.LongType - binding '511' to parameter: 5
2006-07-28 09:57:12,082 DEBUG org.hibernate.type.LongType - binding '512' to parameter: 6
2006-07-28 09:57:12,082 DEBUG org.hibernate.type.LongType - binding null to parameter: 7
2006-07-28 09:57:12,082 DEBUG org.hibernate.type.LongType - binding '180030' to parameter: 8
Re: Hibernate: Log SQL Statements
> Set up a log4j category for org.hibernate.type. Get> it to write out to the same log file as the
> org.hibernate.SQL
> The type one will list the parameters for you after
> the SQL e.g.
> 2006-07-28 09:57:12,082 DEBUG
> org.hibernate.type.LongType - binding '180030' to
> parameter: 8
Note: Hibernate actually uses commons logging "trace" level, so with latest log4j versions (supporting "trace" as well) this level must be enabled. Older versions treated trace just as debug, so enabling debug was enough.
Re: Hibernate: Log SQL Statements
So how can we get parameterized values in Sql Statement while SQL statements are enabled in Configuration file.Re: Hibernate: Log SQL Statements
This is a good trickRe: Hibernate: Log SQL Statements
I just want to say that your posts are consistently among the most useful things I find when I Google random Hibernate issues. Thanks!Re: Hibernate: Log SQL Statements
Use p6spy jdbc driver. Works great.Re: Hibernate: Log SQL Statements
Good tip, can be useful