- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已经开始使用 Hibernate 4.3.8 并尝试使用一个基本程序来插入和检索用户。以下是我的文件。
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代码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();
}
}
我得到的错误是
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 的方法,但似乎没有任何效果。我正在使用 Hibernate 4.3.8-Final
提前致谢
最佳答案
对于 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>
将以下内容添加到您的 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>
关于java - 无法创建请求的服务 [org.hibernate.cache.spi.RegionFactory] - Hibernate 4.3.8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29342357/
我正在尝试启用二级缓存,但出现异常。 persistence.xml中二级缓存的代码如下: 异常的堆栈跟
我在使用 Hibernate 3.0 创建的应用程序中遇到此错误(我只能使用此版本) org.eclipse.jetty.servlet.ServletHolder$1: org.springfram
我的域类如下。主键 ID 是 2 个字段 serviceProviderId 和 sportsId 的组合(分别存在于另一个表 serviceProvider 和 Sports 中)。当我启动我的 g
我已经开始使用 Hibernate 4.3.8 并尝试使用一个基本程序来插入和检索用户。以下是我的文件。 hibernate .cfg.xml com.mysql.jdbc.Dr
我试图升级我的 Spring 和 Hibernate 版本。我使用的是 hibernate 3,现在我已经升级到 hibernate 4.3.6。升级后,我在 ehcache 实现中遇到了一个奇怪的问
我正在尝试将 Ehcache (2.6.0) 配置为 Hibernate (3.6.4) 二级缓存。我在 spring 上下文文件中设置了以下属性 true true net.sf.ehcache.h
我正在将我的 spring 项目从 3.0.5 更新到 4.0.3。 一切都很好,但是在启动时,当 spring 尝试创建 bean 时,我遇到了这个错误消息: 类当然不存在于 ehcache-cor
我是一名优秀的程序员,十分优秀!