如何在为Hibernate 5.2.5配置EhCache时修复java.lang.NoClassDefFoundError:net / sf / ehcache / CacheException

时间:2016-12-27 14:21:59

标签: java hibernate caching ehcache second-level-cache

我是Hibernate的新手,我开始在Hibernate中学习缓存,我想在我的hibernate配置文件中配置EhCache。我已将hibernate-ehcache-5.2.5.Final.jar添加到我的构建路径中,这里是我的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>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>

    <!-- JDBC connection pool -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Enable the second level cache -->
    <property name="hibernate.cache.use_second_level_cache">true</property> 
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and recrete the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>

</session-factory>

</hibernate-configuration>

以下是我配置会话工厂的主要类:

package hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Main { 
    public static void main(String[] args) {
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
}

运行此代码后,我收到错误消息:

Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getConstructor(Unknown Source)
at org.hibernate.cache.internal.StrategyCreatorRegionFactoryImpl.create(StrategyCreatorRegionFactoryImpl.java:38)
at org.hibernate.cache.internal.StrategyCreatorRegionFactoryImpl.create(StrategyCreatorRegionFactoryImpl.java:23)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:198)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:161)
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:67)
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:28)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:257)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:231)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
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:654)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at hibernate.Main.main(Main.java:31)
Caused by: java.lang.ClassNotFoundException: net.sf.ehcache.CacheException
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 20 more

你能帮我解决这个问题还是告诉我我做错了什么?我在Eclipse IDE中使用Hibernate 5.2.5,MySQL DBMS和简单的java项目。正如我已经说过的,我已经添加了&#34; hibernate-ehcache-5.2.5.Final.jar&#34;到我的构建路径,这个jar包含org.hibernate.cache.ehcache.EhCacheRegionFactory类。我还尝试使用一些早期版本的ehcache jar,例如&#34; hibernate-ehcache-5.0.2.Final.jar&#34;或者&#34; ehcache-3.2.0.jar&#34;但我得到了同样的错误。

3 个答案:

答案 0 :(得分:2)

您似乎遇到了类加载器问题。 Hibernate可能加载在没有看到Ehcache的类加载器中。假设Ehcache jar确实在classpath中。因为类路径中需要hibernate-ehcacheehcache

答案 1 :(得分:0)

hibernate-ehcache(5.x)引用了ehcache-2.x,据说不能与ehcache-3.x一起正常工作。

您可能要使用hibernate-jcache,因为ehcache-3.x与jcache兼容。参见this question

答案 2 :(得分:-1)

在类路径中添加您找到的ehcache.jar和hibernate-ehcache.jar存档:

hibernate-release-5.2.9.Final\lib\optional\ehcache

从www.ehcache.org添加最新的ehcache.jar是行不通的。