Force Hibernate to read database and not return cached entity

43,718

Solution 1

session.refresh(entity) or entityManager.refresh(entity) (if you use JPA) will give you fresh data from DB.

Solution 2

Do the read within a new transaction.

For example:

...
MyDTO myDTO = fetchMyDTOById(daoId);
...
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
private MyDTO fetchMyDTOById(Long dtoId) {
    return repository.findById(dtoId);
}

Solution 3

  • Call refresh entity session.refresh(entity)

    or

  • Open new session then call session2.get(EntityClass.class,id) it will pull the entity from the database

        Session session2=sessionFactory.openSession();                  
        EntityClass entity=(EntityClass) session2.get(EntityClass.class, id);
    

Solution 4

Please call EntityManger.clear() method, and then call repository.find() or repository.findOne() as per your requirement to select the updated data from database.

@PersistentContext EntityManager em;
@Autowired EntityReporisitory rep;
....
....
@Transactional
public void method(){
....
....
em.clear();// This line will clear the current value of entity
Entity e = rep.find(example);// In this line data will be loaded freshly from DB
....
....
}
Share:
43,718
Saif
Author by

Saif

Programmer and Developer. Like to learn programming related technologies. java enthusiast Infos: LinkedIn Id Email: [email protected] Career info

Updated on June 11, 2020

Comments

  • Saif
    Saif almost 4 years

    I am using Hibernate and Spring for my web application.

    In database operation, Hibernate is caching entities and returning them in next request without reading the actual database. I know this will reduce the load on database and improve performance.

    But while this app still under construction, I need to load data from database in every request (testing reason).

    Is there any way to force database read?

    I became sure about the caching from this log4j message.

    Returning cached instance of singleton bean 'HelloController'
    DEBUG [http-bio-8080-exec-42] - Last-Modified value for [/myApp/../somePageId.html] is: -1
    
  • Saif
    Saif over 9 years
    i am not using JPA. so do i need to change session.get() to session.refresh()? or if there is a way to check if it is coming from cache .?
  • Predrag Maric
    Predrag Maric over 9 years
    No, you should use session.refresh() after session.get() because it only works with attached entities. To check if an entity is already loaded, this thread has some suggestions, but nothing reliable imho.
  • Adam Siemion
    Adam Siemion about 8 years
    the trick with this approach is that when the entity does not exist in the database, but exists in the cache, calling refresh() throws org.hibernate.UnresolvableObjectException: No row with the given identifier exists
  • Predrag Maric
    Predrag Maric about 8 years
    @adamsiemion Yes, that is something one should be aware of.
  • danidemi
    danidemi over 7 years
    I had the same problem described in the question. With JPA it seems that entityManager.clear() discard in a single operation the whole cache.
  • Alkanshel
    Alkanshel almost 6 years
    This isn't really satisfactory for retrieving multiple entities--won't refresh on every single one just create a separate query for each? I'd prefer if the initial query itself just bypassed the cache. I'm doing query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS) ... but this doesn't seem to work either. Annoying cache!
  • Dragas
    Dragas over 4 years
    You're using Hibernate, which implements JPA, meaning you use JPA.