java 无法在Hibernate 5.1.0.Final中配置hibernate-ehcache-4.3.5.Final.jar的二级缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35574067/
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 configure second level cache of hibernate-ehcache-4.3.5.Final.jar in Hibernate 5.1.0.Final
提问by Syed
I'm trying to achieve second level cache in my Java EE project using Hibernate. My Hibernate version is 5.1.0.Final. I've created a table employee
in my MySQL database. I've included hibernate-ehcache-4.3.5.Final.jar
in my project, but it's throwing Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
error. Here is the hibernate.cfg.xml
:
我正在尝试使用 Hibernate 在我的 Java EE 项目中实现二级缓存。我的 Hibernate 版本是 5.1.0.Final。我employee
在我的 MySQL 数据库中创建了一个表。我已经包含hibernate-ehcache-4.3.5.Final.jar
在我的项目中,但它抛出Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
错误。这是hibernate.cfg.xml
:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/intu</property>
<property name="hibernate.connection.username">****</property>
<property name="hibernate.connection.password">****</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.dom.Employee"/>
</session-factory>
</hibernate-configuration>
My Employee
class:
我的Employee
班级:
package com.intu;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Table(name = "employee")
public class Employee {
private int id;
private String name;
public Employee() {
}
public Employee(String name, float salary) {
super();
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
My Code:
我的代码:
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.intu.Employee;
import com.util.HibernateUtil;
public class Ehcache {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(org.hibernate.Version.getVersionString());
SessionFactory factory = HibernateUtil.getSessionFactory();
Session session1=factory.openSession();
Employee emp1=(Employee)session1.load(Employee.class,121);
System.out.println(emp1.getId()+" "+emp1.getName());
session1.close();
Session session2=factory.openSession();
Employee emp2=(Employee)session2.load(Employee.class,121);
System.out.println(emp2.getId()+" "+emp2.getName());
session2.close();
}
}
The error is
错误是
eb 23, 2016 3:10:49 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
Feb 23, 2016 3:10:49 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 23, 2016 3:10:49 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:20)
at com.util.HibernateUtil.<clinit>(HibernateUtil.java:7)
at com.test.Ehcache.main(Ehcache.java:14)
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.<init>(MetadataBuilderImpl.java:663)
at org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:127)
at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:135)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:655)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at com.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 2 more
Caused by: org.hibernate.HibernateException: could not instantiate RegionFactory [net.sf.ehcache.hibernate.EhCacheRegionFactory]
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactor yInitiator.java:84)
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactor yInitiator.java:29)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
I'm not sure what exactly is missing. What should be added more to get it working?
我不确定到底缺少什么。应该添加什么才能让它工作?
回答by Gergely Bacso
You are using incompatible versions of EhCache and Hibernate, that leads to initialization issues.if you are using Hibernate 5.1, better to use the corresponding ehcache version to go with it:
您使用的 EhCache 和 Hibernate 版本不兼容,这会导致初始化问题。如果您使用的是 Hibernate 5.1,最好使用相应的 ehcache 版本来配合它:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.1.0.Final</version>
</dependency>
回答by Kishan Dhongadi
--Add the below line at hibernate.cfg.xml
-- 在 hibernate.cfg.xml 中添加以下行
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheRegionFactory</property>
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheRegionFactory</property>
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>
The above one would solve the issue.
上面的就解决了这个问题。