Why is my Hibernate Query returning stale data?

谁都会走 提交于 2019-12-03 09:13:08

After CwmService.get().flushChanges(); // commits the transaction do an explicit commit. flush() only flushes the changes to db but does not commit it. I am not sure about flushChanges() though.

I discovered the problem, but it introduces something else.

Basically, when modifying a Book object's List<PageContent> field, Hibernate does three things:

  1. Expires the TimeStamp cache entry for both Book and PageContent
  2. Does many queries to reset the pageNum field on each PageContent object
  3. Removes the Book object from the Second Level Cache.

This ensures that subsequent queries will search for new objects, etc. However:

  1. Hibernate fails to remove each renumbered PageContent object from the Second Level Cache

As a result, any query for the list of pages will run properly, but then will fall back on stale Second Level Cache values for the actual data.

I presume this is because Hibernate feels a pageNum change is not a change in data but a change in behind-the-scenes management. However, that is the data that I would like to read and display.

The solution is to manually refresh every page after the insertion/deletion has occurred.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!