gpt4 book ai didi

java - 如何修复java.lang.NoClassDefFoundError : net/sf/ehcache/CacheException when configuring EhCache for Hibernate 5. 2.5

转载 作者:行者123 更新时间:2023-11-30 06:58:41 24 4
gpt4 key购买 nike

我是 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>

这是我配置 session 工厂的主类:

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”,但我遇到了相同的错误。

最佳答案

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

关于java - 如何修复java.lang.NoClassDefFoundError : net/sf/ehcache/CacheException when configuring EhCache for Hibernate 5. 2.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41346890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com