package de.ivu.elect.business.system.control;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;

public class HibernateCacheReset extends DefaultCacheReset {

  public static final HibernateCacheReset INSTANCE = new HibernateCacheReset();

  private static final Logger LOG = LoggerFactory.getLogger(HibernateCacheReset.class);

  @Override
  @Deprecated // nicht direkt verwenden
  public void reset(EntityManager entityManager) {
    super.reset(entityManager);
    LOG.info("Cache reset");
    Session hibernateSession = (Session) entityManager.getDelegate();

    SessionFactory sf = hibernateSession.getSessionFactory();

    sf.getCache().evictQueryRegions();
    sf.getCache().evictDefaultQueryRegion();
    sf.getCache().evictCollectionRegions();
    sf.getCache().evictEntityRegions();
    sf.getCache().evictAllRegions();
  }
}
