2016-12-27 11 views
1

Я новичок в Hibernate, и я начал изучать кэширование в Hibernate, я хочу настроить EhCache в моем конфигурационном файле hibernate , Я добавил гибернации-Ehcache-5.2.5.Final.jar на мой путь сборки и вот мой hibernate.cfg.xml:Как исправить java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException при настройке EhCache для Hibernate 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 

Можете ли вы помочь мне исправить это или сказать мне, что я сделал не так? Я использую Hibernate 5.2.5, СУБД MySQL и простой Java-проект в Eclipse IDE. Как я уже сказал, я добавил «hibernate-ehcache-5.2.5.Final.jar» в мой путь сборки, и этот баннер содержит класс org.hibernate.cache.ehcache.EhCacheRegionFactory. Я также попытался использовать некоторые более ранние версии ehcache jar, такие как «hibernate-ehcache-5.0.2.Final.jar» или «ehcache-3.2.0.jar», но я получал ту же ошибку.

ответ

2

У вас, похоже, проблема с загрузчиком класса. Hibernate, вероятно, загружается в загрузчик классов, который не видит Ehcache. Предполагая, что бачок Ehcache действительно находится в пути к классам. Потому что вам нужны как hibernate-ehcache, так и ehcache в пути к классам.

-1

Добавить к классам в ehcache.jar и зимовать-ehcache.jar архивы, которые вы найдете в:

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

Добавление Lastest ehcache.jar из www.ehcache.org не будет делать трюк.

 Смежные вопросы

  • Нет связанных вопросов^_^