0

I extended the CrudRepository to make a class called TaskDao. I thought taskDao.findall() would pull values from the database. For some reason taskDao.findall() actually returns the newTask containing the comment "spiffy" even though newTask was saved to the database before the comment was changed to "spiffy". It's like it knows it just stuck newTask into the database so instead of bothering to pull the newest value from the database it tries to save time by using newTask itself. This is bad.

taskDao.save(newTask);
newTask.setComment("spiffy");
model.addAttribute("comments", taskDao.findAll());

2 Answers2

0

okay i got it to work sort of like the dzone article. Except the answer was clear(), not flush(). In the dzone article it passes that parkruncourse around a couple times. i passed no parameter whatsoever. I think because I just want to clear the cache from the entitiymanager? i don't really understand this stuff but i get the idea hibernate or whatever is cacheing values like my newTask and using them to save time instead of running the SQL to get the REAL value like I need it to. So I passed no parameters unlike the dzone article and then instead of running em.refresh(parkruncourse) i just did a em.clear(). As far as I can tell all the dzone article helped me to was attach an EntityManager object to all the stuff I was working with so I could clear() it. All of this makes no sense to me whatsoever but hopefully it keeps working now.

0

Have you tried refreshing the database session?

session.refresh(entity) or entityManager.refresh(entity) (if you're using JPA) will give you fresh data from the database.

Source

Viktor
  • 2,623
  • 3
  • 19
  • 28
sujan
  • 1
  • 2