Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

Is it necessary to execute refresh after an respository save?

Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have made a CRUD as most tutorials. This was working in my opinion good. The database data was manupulated, records added, deleted etc.

My code is refactored and the add record, update record, a 'refresh' is added, with a 'special' repository..., which I do not understand.

Here is the special repository:

Here is my refactored add record method with the refresh call and the colleague note:

I think and expect, that if everything is well coded, the save is updating the database and the persistence context as well, or..?.
I expect that this is default implemented by Hibernate or...?
(I have asked my colleague who refactored it a few weeks ago on the teams chat. The answer was it is hard to explain in a few words and takes to long... And honestly is must say I have not asked it again)

But now I must do an investigations about CQRS, because the idea was to use this instead of CRUD  for a relative simple app.

With this investigation I came across this webpage:

This is a refresh used to update a materialized view, which I understand if the refresh is therefore used.
I have test it, and if I execute a new order than the materialized view is updated with totals as well.

My question at the end is, with a simple/normal- straight CRUD - save via the repository, is it required to execute a refresh for update persistence layer?
And are there maybe special cases when you should do that?
Saloon Keeper
Posts: 25470
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've moved your question over to our ORM forum, since that's a better place to ask this stuff. Also, by the way, it's probably more than a JPA general question than merely a Hibernate JPA one.

I'm not sure. The actual EntityManager refresh() method is intended to bring the application up to date with possible updates to the database from other applications. It works on attached entity objects - for detached objects, you'd have to use merge().

But you're dealing with something beyond basic entities here, you're dealing with Views, so perhaps I'd better do a quick refresh (no pun intended) on what Views are.

A View is essentially a Virtual Table in SQL. It's a shorthand for a SELECT. But it looks like a table and JPA allows you to map Entities onto Views as though they were tables. From JPA's perspective there's no difference between a View and a concrete Table.

But there are Views and then there are Views. A Materialized View is different. With a regular view, you have, as I said, essentially a shorthand SQL SELECT and thus as the data in the table(s) that feed that View change, the View also changes. In a materialized view, however, that view is a snapshot of the database at the time the view materialized and doesn't change unless you re-materialize it.

Which is what that "refresh" method is doing. Calling a stored procedure to re-materialize the view.

The critical thing here is you don't want to get your internal objects out of sync. If you change a table, then you may need to refresh any View entities you have on that table - the EntityManager refresh() method goes out to the database and pulls the latest data values.

You actually probably need an EntityManager refresh less on a materialized view than on a regular view, since it's less likely to change behind your back, but that does depend on your actual usage.
It's feeding time! Give me the food you were going to give to this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic