2016-12-27 402 views
1

我是Hibernate的新手,我開始學習Hibernate中的緩存,我想在我的hibernate配置文件中配置EhCache 。我加入的Hibernate的Ehcache-5.2.5.Final.jar到我的構建路徑,這裏是我的hibernate.cfg.xml:如何修復java.lang.NoClassDefFoundError:net/sf/ehcache/CacheException當爲Hibernate配置EhCache時5.2.5

<?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」,但是我得到了同樣的錯誤。

回答

2

你似乎有一個類加載器的問題。 Hibernate可能被加載到沒有看到Ehcache的類加載器中。假設Ehcache jar確實在類路徑中。因爲在類路徑中同時需要hibernate-ehcacheehcache

-1

添加到classpath的ehcache.jar和休眠,ehcache.jar你找到檔案:

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

從www.ehcache.org添加最新的ehcache.jar不會做的伎倆。

相關問題