java 无法创建请求的服务 [org.hibernate.cache.spi.RegionFactory] - Hibernate 4.3.8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29342357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to create requested service [org.hibernate.cache.spi.RegionFactory] - Hibernate 4.3.8
提问by parvezp
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 4.3.8 并且正在尝试一个基本的程序来插入和检索用户。下面是我的文件。
hibernate.cfg.xml
休眠文件.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
Java 代码 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
我尝试了不同的初始化 SessionFactory 的方法,但似乎没有任何效果。我正在使用 Hibernate 4.3.8-Final
Thanks in advance
提前致谢
回答by Solanki Vaibhav
for hibernate 5.X + add following dependency:
对于 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>
add following into you hibernate.cfg.xml file
将以下内容添加到您的 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>
回答by user3471175
Disable the caching as shown below by modifying the property in hibernate.cfg.xml:
通过修改 hibernate.cfg.xml 中的属性禁用缓存,如下所示:
<property name="hibernate.cache.use_query_cache">false</property>
回答by Revathi Balasubramanian
Make sure that you are using the same version for hibernate and hibernate-ehcache in pom.xml file. Please refer below:
确保您在 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>
In hibernate.cfg file use the below properties
在 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>
回答by Ayush Kumar
Disable the second level cache in hibernate.cfg.xml.
在 hibernate.cfg.xml 中禁用二级缓存。
Include the below line of code to disable the second level cache .
包括以下代码行以禁用二级缓存。
<!-- Disable the second-level cache -->
<property
name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
回答by Srinivas Maheedhar
Get rid of three configs related to cache...you should be good
摆脱三个与缓存相关的配置......你应该很好