欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate 5.2.17 二级缓存配置异常

程序员文章站 2022-04-12 20:59:10
...

异常

org.hibernate.service.spi.ServiceException: Unable to create requested
service [org.hibernate.cache.spi.RegionFactory]
Caused by:
org.hibernate.boot.registry.selector.spi.StrategySelectionException:
Unable to resolve name [org.hibernate.cache.internal.EhCacheRegionFactory] as strategy
[org.hibernate.cache.spi.RegionFactory]

org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.<init>(MetadataBuilderImpl.java:662)
    at org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:126)
    at org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:93)
    at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:125)
    at org.hibernate.boot.MetadataSources.buildMetadata(MetadataSources.java:179)
    at top.sicso.util.HbmUtils.getSessionFactory(HbmUtils.java:21)
    at top.sicso.select.test.TestApp2.<init>(TestApp2.java:21)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.cache.internal.EhCacheRegionFactory] as strategy [org.hibernate.cache.spi.RegionFactory]
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:126)
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:194)
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:161)
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:67)
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:28)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
    ... 31 more

hibernate.cfg.xml配置


<!-- 开启二级缓存默认 不开起 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>

<!-- 设置二级缓存实现源 -->
<property name="hibernate.cache.region.factory_class">
    org.hibernate.cache.internal.EhCacheRegionFactory
</property>

  此处的二级缓存实现源是根据hibernate.properties文件中的说明配置的。
hibernate4及以后的版本都是:
   hibernate.cache.region.factory_class 属性来配置缓存实现源

#hibernate.cache.region.factory_class org.hibernate.cache.internal.EhCacheRegionFactory

这样的配置导致了上面的异常。


原因:
 hibernate 5.2提供的ehcache的jar包并没有

  org.hibernate.cache.internal.EhCacheRegionFactory



optional中ehcache下的jar包
Hibernate 5.2.17 二级缓存配置异常

  hibernate-ehcache-5.2.17.Final.jar
中只有
   org.hibernate.cache.ehcache.EhCacheRegionFactory

因此将配置文件中的 org.hibernate.cache.internal.EhCacheRegionFactory

改为 org.hibernate.cach.EhCacheRegionFactory 即可

<property name="hibernate.cache.region.factory_class">
    org.hibernate.cach.EhCacheRegionFactory
</property>

测试:

@Test
    public void testHbm2(){

        Session session = sessionFactory.openSession();
        Order order = session.get(Order.class, 1);
        System.out.println(order);
        Order order2 = session.get(Order.class, 1);
        System.out.println(order2);
        session.close();

        System.out.println();

        Session session2 = sessionFactory.openSession();
        Order order3 = session2.get(Order.class, 1);
        System.out.println(order3);
        session2.close();

    }

输出 :

Hibernate: 
    select
        order0_.sid as sid1_3_0_,
        order0_.price as price2_3_0_,
        order0_.cus_id as cus_id3_3_0_,
        customer1_.cid as cid1_1_1_,
        customer1_.cname as cname2_1_1_ 
    from
        t_order order0_ 
    left outer join
        customer customer1_ 
            on order0_.cus_id=customer1_.cid 
    where
        order0_.sid=?
Order [sid=1, price=0, customerId=1]
Order [sid=1, price=0, customerId=1]

Order [sid=1, price=0, customerId=1]

结果:
  只执行一次sql查询,第三次输出是从二级缓存中取得的数据
二级缓存配置成功。