I was able to accomplish this by creating a component that implements HibernatePropertiesCustomizer, and passing in "CRITERIA_VALUE_HANDLING_MODE" as "ValueHandlingMode.INLINE".
However, this will (as the name says) "inline" the parameters into the SQL statement. I'm not sure if Hibernate does any sanitizing on the parameters, so it may open you up to SQL injection.
In my case, this was not an issue, because I only use it for testing purposes, so I did not dig deeper.
Here is the code I used:
@Component
@ActiveProfiles("test")
public class CurrentSQL implements HibernatePropertiesCustomizer {
private List<String> sqls = new ArrayList<>();
public List<String> getSqls() {
return sqls;
}
public void clearStatements() {
this.sqls.clear();
}
@Override
public void customize(Map<String, Object> hibernateProperties) {
hibernateProperties.put(AvailableSettings.CRITERIA_VALUE_HANDLING_MODE, ValueHandlingMode.INLINE);
hibernateProperties.put(AvailableSettings.STATEMENT_INSPECTOR, (StatementInspector) sql -> {
this.sqls.add(sql);
return sql;
});
}
}
Then, in my tests, I do something like this:
currentSQL.clearStatements();
repository.runSomeMethodThatGeneratesSQL();
var sql = currentSQL.getSqls().get(0);