如何将Hibernate Criteria转换为Oracle SQL?调试时如何查看生成的SQL?
Absolutely, you can translate that Hibernate Criteria query into an Oracle SQL statement, and there are straightforward ways to view Hibernate's generated SQL in Eclipse during debugging. Let's break this down:
1. Converting the Criteria to Oracle SQL
Your Criteria query filters Student records based on three conditions:
- The
STD_IDmatches a specific value (stdId) - The
START_DATEis less than or equal todateOfService - The
END_DATEis greater than or equal todateOfService
Assuming your Student entity maps directly to a table named STUDENT (with column names matching your entity fields as defined in Hibernate mappings), the equivalent Oracle SQL would look like this:
SELECT * FROM STUDENT WHERE STD_ID = :stdId AND START_DATE <= :dateOfService AND END_DATE >= :dateOfService;
Note: If your entity uses custom table/column names (via @Table or @Column annotations), replace STUDENT, STD_ID, etc., with the actual names from your mappings.
2. Viewing Generated SQL in Eclipse Debugging
There are a few reliable methods to see exactly what SQL Hibernate generates:
Enable Hibernate logging: Configure your logging framework (like Log4j or Logback) to set the
org.hibernate.SQLlogger toDEBUGlevel. This will print the full generated SQL to Eclipse's Console view whenever the query runs. To see parameter values alongside the SQL, setorg.hibernate.typetoTRACElevel.Use VM arguments: Add these arguments to your Eclipse run/debug configuration to output SQL directly to the console:
-Dhibernate.show_sql=true -Dhibernate.format_sql=trueshow_sqlprints the raw SQL, whileformat_sqlformats it for readability. For parameter values, you'll still need the logging setup mentioned above.Inspect during debugging: If you have Hibernate Tools installed in Eclipse, set a breakpoint at the line where
criteria.list()is called. Hover over thecriteriaobject or inspect it in the Debug view—you can often find the generated SQL in the object's internal state, or use the Hibernate Query Editor to preview it.
内容的提问来源于stack exchange,提问作者Ramesh

