views:

51

answers:

1

Is there a way to convert a Persistable object into an SQL Insert Statement using Hibernate? This has to be possible because thats what Hibernate does behind the scenes, But I just need to write out the SQL Stmt to a log file for error handling purposes in cases where the Database goes down and the saving of the Hibernate Object failed.

+1  A: 

This is a build in feature. You are able to configure hibernate logging to do that:

log4j.logger.org.hibernate.SQL=DEBUG, SQL_APPENDER
log4j.additivity.org.hibernate.SQL=false

see here.
Edit: If you want to see the parmeter values maybe you can use this. I haven't tried but if it is usefull please post your experience.

zoidbeck
But this gives me SQL Statements in a PreparedStatement Format with "?" instead of the params. I am looking for something that will write a ready-to-execute SQL out to log, that can be then picked up and executed.
udit
I wouldn't really call this a "built-in feature", as it's just something that is logged for debug purposes. A built-in feature would have Hibernate exposing this programatically for you to access.
matt b
+1 for P6Spy link. This should show the statements with actual values.
Tushar Tarkas