Question

We have triggers for some of our tables such that they update the inserted/modified entity after every put operation.

How can I instruct Hibernate to read the entity immediately after a put () operation so that the application always has access to the most accurate data read from the DB (including the data modified by triggers). For the sake of simplicity, assume that triggers fire immediately after the put() operation.

I am looking for an annotation like select-after-update or select-after-insert. Even better solution would be if I can specify the columns to read after insert/update.

Update:

@Generated annotation does not work in my case because I am using @ColumnTransformer annotation for modifiedDate field as described here.

@Generated fails here completely as it leads Hibernate to believe that database would generate value for it. So the custom-write-SQL specified over columns is ignored during insert/update.

I want the reload functionality from the @Generated annotation but without the ignoring field part during insert/update.

No correct solution

OTHER TIPS

You're looking for the @Generated annotation.

I don't think it can be done in a single operation. As you said @Generated will simply ignore the column and @PostPersist and @PostUpdate cannot invoke EntityManager or queries (see documentation https://docs.jboss.org/hibernate/core/4.0/hem/en-US/html_single/#d0e2985)

Morever, if you do it in the same transaction, what guarantees you the execution of the trigger is finished when you try to load the data again?

As I see it, your only way out is a refresh() after the save or update on the object, in an other transaction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top