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.

2
  • Post your @Repository code that performs the db operation. Commented Dec 8, 2014 at 6:33
  • 2
    handle this in your dao layer. you don't need to 'catch' the query, but build it taking into account that specific condition. Commented Dec 8, 2014 at 13:38

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

this is deprecated, see also stackoverflow.com/questions/39112308/…
Thanks, good to know implemented a better mechanism in Hybernate 5
onPrepareStatement is deprecated in hibernate 5.6 and removed in 6. How to handle this issue with hibernate 6?

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.