2

Is it possible to get the native SQL query while executing an UPDATE from Hibernate, using Java language?

For example, I have the

session.update(something);

statement, and I'd like to retrieve a String (or something else) containing the SQL query behind the UPDATE action.


I'd like to have the effective query that Hibernate "sends" to MySQL.

JustTrying
  • 592
  • 4
  • 9
  • 23

3 Answers3

3

You can. Hibernate is using log4j so you must just set appropriate categories. Those are:

org.hibernate.SQL to debug

org.hibernate.type to trace

See also here: How to print a query string with parameter values when using Hibernate

Community
  • 1
  • 1
Danubian Sailor
  • 1
  • 38
  • 145
  • 223
2

You have two choices...

  • You can either set <property name="show_sql">true</property> in your persistance.cfg.xml, but this won't show the values in the parameters, just ?

  • You could (my preferred option) use SQL profiler (if you're using MS SQL) to see exactly what SQL was being ran against the database.

David
  • 19,577
  • 28
  • 108
  • 128
1

It's not possible to get the queries programmatically (or at least not possible without coding for a specific database): the SQL query depends on the database you are using hibernate to interact with and is generally not directly accessible through hibernate, as it depends on the database drivers.

The best way to see the actual SQL statements is to look at the log files for your database.

RoryB
  • 1,047
  • 7
  • 28