3

I have Oracle DB and Java project which is connected to the DB using JPA/Eclipselink.
The problem is that when some data updated in the database (manually using Oracle SQL developer), this data is not visible via JPA, only old values.
What can cause such problems?

kardanov
  • 575
  • 3
  • 13
  • 25

1 Answers1

3

Please try em.refresh()
JPA's EntityManager will be unaware of any changes you made outside its transaction till it reloads the data. Above command will make it reload the data.

If it is a List (populated using select), then you'll need to reload it.

Padmarag
  • 7,067
  • 1
  • 25
  • 29
  • This works, but for me it looks strange, cause at first I select some entity data from the DB,and then I need to refresh it before usage. What do you think @Padmarag , is it common practice? Thanks. – kardanov Aug 24 '11 at 12:43
  • This'll work same even if you use JDBC. Once you've fired a select, we fetch the resultset, the transaction is over and connection will be closed. While you process the fetched data, it is possible that new records have been added in database. So to get the new records you'll need to fire select again. JPA also follows similar logic. – Padmarag Aug 24 '11 at 12:49
  • so the good idea could be to refresh all the data obtained via JPA before further usage? But shouldn't the resultset obtained by each JPA select call be up-to-date? Cause I actually do two selects - one before manual update and another after it. – kardanov Aug 24 '11 at 12:53
  • Even after update, it is possible that the data is not loaded from database. After you update, you may need to call em.flush() which'll cause EntitManager to write all updates to database and then call em.refresh() to reload updated data. Please go through EntityManager documentation as this is little tricky to understand at first. – Padmarag Aug 25 '11 at 05:15