How to add programmatically Ehcache configuration file to programmatically written hibernate Configuration file

Hibernate Configuration file

public class HibernateConfigurationUtils {

private static SessionFactory sessionFactory ;

static {
    Configuration configuration = new Configuration();
    configuration.addPackage("com.pawan.cacheConfiguration");
    configuration.addAnnotatedClass (Employee.class);
    configuration.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
    configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/springbatch");                                
    configuration.setProperty("hibernate.connection.username", "*************");     
    configuration.setProperty("hibernate.connection.password", "*************");
    configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
    configuration.setProperty("hibernate.hbm2ddl.auto", "update");
    configuration.setProperty("hibernate.show_sql", "true");
    configuration.setProperty(" hibernate.connection.pool_size", "10");
    configuration.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
    configuration.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
    configuration.setProperty("hibernate.cache.use_second_level_cache","true");
    configuration.setProperty("hibernate.cache.use_query_cache","true");
    configuration.setProperty("cache.use_structured_entries", "true");
    configuration.setProperty("hibernate.current_session_context_class", "thread");
    //configuration.setProperty("net.sf.ehcache.configurationResourceName","com.pawan.cacheConfiguration.CachingConfigurationUtils");

    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    System.out.println("Builder :: "+ builder);
    sessionFactory = configuration.buildSessionFactory(builder.build());
    System.out.println("sessionFactory ::  "+ sessionFactory);
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

}


Ehcache Configuration-

public class CachingConfigurationUtils{

public CacheManager ehCacheManager() {
	System.out.println("CachingConfigurationUtils called ");
    CacheConfiguration cacheConfiguration = new CacheConfiguration();
    CacheManager manager = new CacheManager();//manager.getCache("sampleCache");
    
    cacheConfiguration.setName("com.pawan.model.Employee");
    manager.getCacheNames();
    System.out.println("manager.getCacheNames() :: " + manager.getCacheNames());
    //cacheConfiguration.setName("org.hibernate.cache.internal.StandardQueryCache");
    //cacheConfiguration.setName("org.hibernate.cache.spi.UpdateTimestampsCache");
    cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
    cacheConfiguration.setMaxEntriesLocalHeap(1000);
    cacheConfiguration.maxElementsInMemory(1000);
    cacheConfiguration.eternal(false);
    cacheConfiguration.timeToIdleSeconds(2000);
    cacheConfiguration.timeToLiveSeconds(100000);
    cacheConfiguration.overflowToDisk(true);
    //PersistenceConfiguration persistenceConfiguration  = new PersistenceConfiguration();
    //persistenceConfiguration.
    //cacheConfiguration.addPersistence(persistenceConfiguration);("com.pkprawah.hibernateCfgByJava.HibernateConfigurationUtils");
    Configuration config = new Configuration();
    config.addCache(cacheConfiguration);
    System.out.println("config ::"+config);
    System.out.println("CacheManager.newInstance(config) ::"+CacheManager.newInstance(config));
    return CacheManager.newInstance(config);
}

}

Now doubt is , How to add "CachingConfigurationUtils " class to "HibernateConfigurationUtils " .

If we are adding configuration.setProperty(“net.sf.ehcache.configurationResourceName”,“com.pawan.cacheConfiguration.CachingConfigurationUtils”); line to HibernateConfigurationUtils class we are getting Exception.

Request you please guide me how to do move on.

As far as I understand the code, you can use this configuration:

configuration.setProperty("hibernate.cache.ehcache.cache_manager", ehCacheManager());