I am using Spring for DI and Hibernate for data access on a mySQL database. I have code inside a transaction that inserts a record in a table and then executes a view that queries that table and performs some aggregate calculations. The problem I see is that the record I just inserted during the same transaction, is not included in the view’s calculated values. I run the same view in mySQL workbench and the inserted value is included in the view. Does anyone know what is causing this?
Share
Ultimately, I had to call entityManager.refresh(Object entity) to refresh the entity for the view record I wanted updated. I think the problem resides in the fact that Hibernate cannot recognize that the view needs to be updated since it does not know that it is dependent (at the database level) upon the original entity that was updated. I presume that Hibernate is caching the records from the view and does not know that they need to get updated, even after a flush().
Hibernate sees the original table and the view as completely unrelated, when in reality the view is dependent upon the table and should be made “dirty” whenever the table changes. I don’t know how to get Hibernate to recognize that.