Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create requested service [org.hibernate.cache.spi.RegionFactory] - Hibernate 4.3.8

Tags:

java

hibernate

I have started using Hibernate 4.3.8 and was trying out a basic program to insert and retrieve a user. Below are my files.

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/mywork</property>
    <property name="connection.username">****</property>
    <property name="connection.password">*****</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">
        org.hibernate.dialect.MySQL5Dialect
    </property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <property name="cache.use_query_cache">true</property>
    <property name="cache.use_second_level_cache">true</property>
    <property name="cache.use_structured_entries">true</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>

    <mapping class="com.parvez.hibernate.model.User"/>

</session-factory>

Java Code HB2Test.java

package com.parvez.hibernate.model;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;


public class HB2Test {

    private static SessionFactory sessionFactory;
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        User myUser = new User();
        myUser.setUserId(1);
        myUser.setUserName("Superman");

        /*StandardServiceRegistryBuilder builder = new    StandardServiceRegistryBuilder();

        //create session factory
        Configuration configuration = new Configuration();
        SessionFactory sessionFactory = configuration.configure().
                    buildSessionFactory(builder.applySettings(configuration.getProperties()).build());

        Session session = sessionFactory.openSession();
     */

    Configuration configuration = new Configuration().configure("hibernate.cfg.xml");

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
            configuration.getProperties()).build();

        sessionFactory =     configuration.buildSessionFactory(serviceRegistry);

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        session.save(myUser);

        session.getTransaction().commit();
        session.close();
    }

}

The error that I get is

INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:261)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:225)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:295)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2444)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2440)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
at com.parvez.hibernate.model.HB2Test.main(HB2Test.java:37)
Caused by: org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.EhCacheRegionFactory]
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:101)
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:46)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:105)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:251)
... 7 more
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.cache.EhCacheRegionFactory] as strategy [org.hibernate.cache.spi.RegionFactory]
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:128)
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:87)
... 10 more

I have tried different ways of initializing the SessionFactory, but nothing seems to work. I am using Hibernate 4.3.8-Final

Thanks in advance

like image 865
parvezp Avatar asked Oct 31 '22 07:10

parvezp


1 Answers

for hibernate 5.X + add following dependency:

 <!-- provide second level caching functionality -->
    <!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>3.4.0</version>
    </dependency>


    <!-- provide ehcache integration with hibernate -->
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>5.2.8.Final</version>
    </dependency>


add following into you hibernate.cfg.xml file

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>
like image 78
Solanki Vaibhav Avatar answered Nov 15 '22 05:11

Solanki Vaibhav