Andrea Caloni
Andrea Caloni

Reputation: 145

Hibernate @Formula set value at runtime

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

Upvotes: 5

Views: 4775

Answers (1)

StanislavL
StanislavL

Reputation: 57381

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

Upvotes: 10

Related Questions