1

Using org.hibernate.resource.jdbc.spi.StatementInspector's inspect method, is it possible to see the actual bound parameters of inspected queries.

Currently my StatementInspector's inspect(String sql) method only intercepts INSERT INTO table (value_one, value_two) VALUES (?, ?) as a SQL string input, but I want it to intercept INSERT INTO table (value_one, value_two) VALUES ('one', 'two').

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.