Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate @Formula set value at runtime

Tags:

hibernate

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

like image 523
Andrea Caloni Avatar asked Jan 04 '23 02:01

Andrea Caloni


1 Answers

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

like image 138
StanislavL Avatar answered Jan 13 '23 07:01

StanislavL