title: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given JPA+hibernate+Spring
body: <p>Hi this question has been asked so many times but still In my application second level cache is not able to configure. I am using JPA+hibernate+Spring , I have tried the possible ways to configure but unsuccessful.</p>
  
  <p>These are the links found for configuration</p>
  
  <p><a href="https://stackoverflow.com/questions/3674697/how-to-configure-jpa-2-0-with-hibernate-3-5-2-to-use-ehcache-as-a-level-2-cache">How to configure JPA 2.0 with Hibernate 3.5.2 to use EHCache as a Level 2 cache and query cache?</a></p>
  
  <p>This is my error</p>
  
  <pre><code> Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext-persistence.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
      at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:139)
      at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:93)
      at com.test.Test.main(Test.java:16)
  Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
      at org.hibernate.cache.internal.NoCachingRegionFactory.buildEntityRegion(NoCachingRegionFactory.java:83)
      at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:364)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
      at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
  </code></pre>
  
  <p>This is my persistence.xml</p>
  
  <pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
  http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
      version="2.0"&gt;
  
  
      &lt;persistence-unit name="ornament" transaction-type="RESOURCE_LOCAL"&gt;
  
          &lt;mapping-file&gt;domain/orm/Category.orm.xml&lt;/mapping-file&gt;
          &lt;!-- &lt;mapping-file&gt;META-INF/product.orm.xml&lt;/mapping-file&gt; --&gt;
          &lt;class&gt;com.ornamentbazzar.common.catalog.entity.Category&lt;/class&gt;
          &lt;class&gt;com.ornamentbazzar.common.catalog.entity.CategoryMapper&lt;/class&gt;
          &lt;class&gt;com.ornamentbazzar.common.catalog.entity.CategoryMapperPK&lt;/class&gt;
          &lt;class&gt;com.ornamentbazzar.common.catalog.entity.CategoryAttribute&lt;/class&gt;
          &lt;class&gt;com.ornamentbazzar.common.catalog.entity.CategoryMapper&lt;/class&gt;
          &lt;class&gt;com.ornamentbazzar.common.assets.entity.StaticAsset&lt;/class&gt;
          &lt;class&gt;com.ornamentbazzar.common.assets.entity.StaticAssetDescription&lt;/class&gt;
          &lt;class&gt;com.ornamentbazzar.common.assets.entity.ImageStaticAsset&lt;/class&gt;
  
          &lt;properties&gt;
              &lt;property name="hibernate.transaction.flush_before_completion" value="false" /&gt;
              &lt;property name="hibernate.connection.autocommit" value="true" /&gt;
               &lt;property name="hibernate.cache.use_second_level_cache" value="true"/&gt;
              &lt;property name="hibernate.cache.use_query_cache" value="true"/&gt;
  
               &lt;property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/&gt;    
              &lt;property name="hibernate.cache.provider_configuration" value="category-ehcache.xml" /&gt; 
              &lt;property name="hibernate.generate_statistics" value="true" /&gt;
              &lt;property name="hibernate.id.new_generator_mappings" value="true" /&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="create-drop" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>applicationContext-persistence.xml</p>
  
  <pre><code>&lt;bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/test" /&gt;
      &lt;property name="username" value="***" /&gt;
      &lt;property name="password" value="***" /&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager"
      proxy-target-class="true" /&gt;
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
  &lt;/bean&gt;
  &lt;bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="persistenceXmlLocation" value="classpath:* META-INF/persistence.xml" /&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="persistenceUnitName" value="ornament" /&gt;
      &lt;property name="packagesToScan" value="com.ornamentbazzar.*" /&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="showSql" value="true" /&gt;
              &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>This is my pom.xml</p>
  
  <pre><code>&lt;properties&gt;
      &lt;spring.version&gt;4.1.6.RELEASE&lt;/spring.version&gt;
      &lt;spring.security.version&gt;3.1.3.RELEASE&lt;/spring.security.version&gt;
      &lt;hibernate.version&gt;4.3.9.Final&lt;/hibernate.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
  
      &lt;!-- Hibernate Libraries --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-envers&lt;/artifactId&gt;
          &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
          &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;type&gt;jar&lt;/type&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;type&gt;jar&lt;/type&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-ehcache&lt;/artifactId&gt;
          &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
          &lt;version&gt;4.3.1.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-jpa-2.1-api&lt;/artifactId&gt;
          &lt;version&gt;1.0.0.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;net.sf.ehcache&lt;/groupId&gt;
          &lt;artifactId&gt;ehcache-core&lt;/artifactId&gt;
          &lt;version&gt;2.6.11&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;dependencies&gt;
  </code></pre>
  
  <p>Class which is used for cache</p>
  
  <pre><code>import org.hibernate.annotations.Cache;
   import org.hibernate.annotations.CacheConcurrencyStrategy;
  @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "categories")
  public class Category implements Serializable {
  
      private static final long serialVersionUID = 1L;
      private static final Log LOG = LogFactory.getLog(Category.class);
  
      @Id
      @Column(name = "CATEGORY_ID")
      @GeneratedValue(generator = "CategoryId")
      @GenericGenerator(name = "CategoryId", strategy = "com.ornamentbazzar.common.util.CustomTableGenerator", parameters = {
              @Parameter(name = "segment_value", value = "CategoryImpl"),
              @Parameter(name = "entity_name", value = "com.ornamentbazzar.common.catalog.entity.Category") })
      protected Long id;
  
      @Column(name = "NAME", nullable = false)
      @Index(name = "CATEGORY_NAME_INDEX", columnNames = { "NAME" })
      protected String name;
  
      @Column(name = "URL")
      @Index(name = "CATEGORY_URL_INDEX", columnNames = { "URL" })
      protected String url;
  
      @Column(name = "URL_KEY")
      @Index(name = "CATEGORY_URLKEY_INDEX", columnNames = { "URL_KEY" })
      protected String urlKey;
  
      @Column(name = "DESCRIPTION")
      protected String description;
  }
  </code></pre>
  
  <p>Main class for testing</p>
  
  <pre><code>public class Test {
      public static void main(String[] args) {
          ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                  new String[] { "applicationContext-persistence.xml" });
          CategoryDao categoryDao = (CategoryDaoImpl) applicationContext
                  .getBean("CategoryDao");
          Category category = categoryDao.findCategoryByName("jewellery");
          System.out.println(category.getDescription());
  }
  }
  </code></pre>
  
  <p>directory</p>
  
  <p><a href="https://i.stack.imgur.com/WX7lE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WX7lE.png" alt="enter image description here"></a></p>