6

My setup is Spring JPA / Hibernate / Oracle.

There's a trigger in the DB that performs some basic operation on multiple fields in a row (e.g. perform rounding on a decimal saved as string).

Naturally, when I save said row from the ORM, it doesn't pick up the changes made by the trigger, unless I do an em.refresh(myRow).

Since I am using interfaces extending JpaRepository, I can only do that wherever the repos are being used (or implement this particular repository and override the save methods).

There is also a @Generated annotation from Hibernate which might work, although it's more like a hack, since that's not what it was made for.

Can it be done in any other way?

Deroude
  • 1,052
  • 9
  • 24
  • +1 Interesting question. Unfortunately, you cannot use JPA lifecycle callbacks and listeners, since you [cannot use the EM in them](http://stackoverflow.com/questions/12951701/how-to-get-entity-manager-or-transaction-in-jpa-listene). – kostja Jul 25 '14 at 12:14
  • 1
    Another JPA provider has a returning policy eliminating the need for refreshes in many cases: http://wiki.eclipse.org/EclipseLink/FAQ/JPA#Are_stored_procedures_and_triggers_supported.3F . Hibernate might have something similar though – Chris Jul 25 '14 at 13:43
  • Thanks Chris, I've been trying to suggest using EclipseLink for a variety of other reasons as well. It's a bit late for this project though. – Deroude Jul 25 '14 at 15:18
  • Have you discovered a solution to this? I just posted a question that's a duplicate of this, apparently. I'm hoping that, a year on, someone at Spring woke up to this limitation... – user1944491 Aug 13 '15 at 12:47
  • @Modifying(clearAutomatically=true) on top of a JpaRepository method should work. Still testing some corner cases though (such as changes affecting other entities and concurrent modification) – Deroude Oct 30 '15 at 08:39

0 Answers0