4

I have a Java Entity with a field with the annotation @Formula, in which is executed an SQL query containing some specific function for Firebird database. Now I have to migrate to Oracle database, and I need to replace the SQL code inside that @Formula. Is there a way to achieve this ? Can I extend in some way the Hibernate @Formula in order to change the annotation's value at runtime ? Thanks

Andrea Caloni
  • 135
  • 2
  • 6

1 Answers1

9

You can achieve this slightly different way.

You can place in the @Formula a placeholder "{TO_BE_REPLACED}" and add a Hibernate Interceptor to change onPrepareStatement. There you can can change SQL generated by hibernate. JUst check the SQL string and replace the {TO_BE_REPLACED} with your real value.

See how to add interceptor here

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • This is exactly what I was looking for. Thank you ! – Andrea Caloni Apr 27 '17 at 06:04
  • The idea is to replace the place holder with the proper value (e.g. schema name). Not too sure about the impact on the performance since all the queries will be intercepted with something like a string contains check in the interceptor. If that is the issue, then make sure to have a light weight check in the interceptor to avoid running it for all the queries. The complete solution is described in thetopsites.net/article/51562859.shtml – Youness Sep 18 '20 at 17:16