【问题标题】:How to fix java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException when configuring EhCache for Hibernate 5.2.5为 Hibernate 5.2.5 配置 EhCache 时如何修复 java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
【发布时间】:2017-05-11 20:12:27
【问题描述】:

我是 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 项目。正如我已经说过的,我已将“hibernate-ehcache-5.2.5.Final.jar”添加到我的构建路径中,并且此 jar 包含 org.hibernate.cache.ehcache.EhCacheRegionFactory 类。我还尝试使用一些早期版本的 ehcache jar,例如“hibernate-ehcache-5.0.2.Final.jar”或“ehcache-3.2.0.jar”,但我遇到了同样的错误。

【问题讨论】:

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


    【解决方案1】:

    您似乎遇到了类加载器问题。 Hibernate 可能是在一个看不到 Ehcache 的类加载器中加载的。假设 Ehcache jar 确实在类路径中。因为你在类路径中同时需要hibernate-ehcacheehcache

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

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

        从 www.ehcache.org 添加最新的 ehcache.jar 将无法解决问题。

        【讨论】:

          猜你喜欢
          • 2014-01-26
          • 1970-01-01
          • 2012-08-07
          • 2015-10-21
          • 2012-03-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多