1

I have a Hibernate query that is configured to execute with Spring Data (the @Repository) annotation. I want to catch the query before it is submitted to the DB and when a specific condition is met, modify it a little bit.

What is the best option to do so? I prefere a JPA solution rather then a Hibernate solution, but a Hibernate solution will also work.

Yuval Zilberstein
  • 175
  • 1
  • 4
  • 11

1 Answers1

4

If you want to modify the query after the prepared statement, you can do it into an Hibernate Interceptor, and register it during Spring DB Configuration.

Your MyInterceptor either should implements Hibernate Interceptor.class or extends EmptyInterceptor.class. The method you are looking for is:

public String onPrepareStatement(String sql);

If you need something more, you could give a try with the Event Listeners, but I am not sure there is one for your case.

Here some documentation:

Hibernate 4.0 Interceptors and events How to integrate an Interceptor with Spring

Community
  • 1
  • 1
R.Litto
  • 385
  • 3
  • 14
  • this is deprecated, see also https://stackoverflow.com/questions/39112308/how-i-can-configure-statementinspector-in-hibernate – Alex R Jun 05 '19 at 05:31
  • Thanks, good to know implemented a better mechanism in Hybernate 5 – R.Litto Aug 12 '19 at 07:50