Java 无法创建请求的服务[org.hibernate.cache.spi.RegionFactory]-hibernate 4.3.8
我已经开始使用Hibernate4.3.8,并尝试使用一个基本程序来插入和检索用户。下面是我的档案 hibernate.cfg.xmlJava 无法创建请求的服务[org.hibernate.cache.spi.RegionFactory]-hibernate 4.3.8,java,hibernate,Java,Hibernate,我已经开始使用Hibernate4.3.8,并尝试使用一个基本程序来插入和检索用户。下面是我的档案 hibernate.cfg.xml <?xml version='1.0' encoding='utf-8'?> 我得到的错误是 INFO: HHH000397: Using ASTQueryTranslatorFactory Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable t
<?xml version='1.0' encoding='utf-8'?>
我得到的错误是
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
我尝试过初始化SessionFactory的不同方法,但似乎没有任何效果。我使用的是Hibernate4.3.8-Final
提前感谢删除与缓存相关的三个配置…您应该很好在hibernate.cfg.xml中禁用二级缓存 包括以下代码行以禁用二级缓存
<!-- Disable the second-level cache -->
<property
name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
org.hibernate.cache.NoCacheProvider
用于hibernate 5.X+
添加以下依赖项:
<!-- 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>
org.ehcache
ehcache
3.4.0
org.hibernate
休眠ehcache
5.2.8.最终版本
将以下内容添加到hibernate.cfg.xml文件中
<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>
true
org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
net.sf.ehcache.hibernate.EhCacheProvider
通过修改hibernate.cfg.xml中的属性禁用缓存,如下所示:
<property name="hibernate.cache.use_query_cache">false</property>
false
确保您在pom.xml文件中使用的hibernate和hibernate ehcache版本相同。请参阅下文:
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.10.Final</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.4.10.Final</version>
</dependency>
org.hibernate
冬眠核心
5.4.10.最终版本
org.hibernate
休眠ehcache
5.4.10.最终版本
在hibernate.cfg文件中,使用以下属性
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheRegionFactory</property>
true
org.hibernate.cache.ehcache.internal.EhcacheRegionFactory
应用时。请注意,您是否收到以下消息:
HHH020100: The Ehcache second-level cache provider for Hibernate is deprecated. See https://hibernate.atlassian.net/browse/HHH-12441 for details.
要解决这个问题,您需要使用EhCacheRegionFactory
也就是说,您的应用程序上下文实际上应该是:
<bean name="ehcacheRegionFactory" class="org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory"/>
...
创建如下内容:
<bean name="ehcacheRegionFactory" class="org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" destroy-method="destroy">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="cacheRegionFactory" ref="ehcacheRegionFactory"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
</props>
</property>
</bean>
真的
当您启用二级缓存时,请确保正确配置它。!!我评论了这句话。。还是不行根据问题,他也想使用缓存。是的,这很有效。请注意,此处使用的属性引用的是不推荐使用的类。为了解决这个问题,请参见
<bean name="ehcacheRegionFactory" class="org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory"/>
...
<property name="cacheRegionFactory" ref="ehcacheRegionFactory"/>
<bean name="ehcacheRegionFactory" class="org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" destroy-method="destroy">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="cacheRegionFactory" ref="ehcacheRegionFactory"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
</props>
</property>
</bean>