我已经开始使用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
提前致谢
答案 0 :(得分:2)
对于休眠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>
答案 1 :(得分:0)
通过修改hibernate.cfg.xml中的属性来禁用缓存,如下所示:
for link in soup.find_all('a'):
Href_q.write(link.get('href')+ '\t' + link.get('target') + '\n')
Href_q.close()
答案 2 :(得分:0)
确保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>
在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>
答案 3 :(得分:0)
应用@Solanki 's answer时。请注意,您会收到以下消息:
class page1 extends React.Component {
render() {
const admin = false;
if (!admin) return <></>
return (
<div>
<button onClick={this.deleteThis}>hello</button>
<button onClick={this.deleteThis}>hello</button>
<button onClick={this.deleteThis}>hello</button>
<button onClick={this.deleteEverything}>hello</button>
</div>
);
}
}
要解决此问题,您需要使用EhCacheRegionFactory。
意思是,您的applicationContext实际上应该像:
HHH020100: The Ehcache second-level cache provider for Hibernate is deprecated. See https://hibernate.atlassian.net/browse/HHH-12441 for details.
在sessionFactoryBean的配置中,删除属性<bean name="ehcacheRegionFactory" class="org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory"/>
...
和hibernate.cache.region.factory_class
。
然后添加:
net.sf.ehcache.hibernate.EhCacheProvider
创建类似这样的内容:
<property name="cacheRegionFactory" ref="ehcacheRegionFactory"/>
答案 4 :(得分:-2)
在hibernate.cfg.xml中禁用二级缓存。
包含以下代码行以禁用二级缓存。
<!-- Disable the second-level cache -->
<property
name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
答案 5 :(得分:-3)
摆脱与缓存相关的三个配置......你应该很好