无法在hibernate 4.x中配置二级缓存

时间:2013-12-23 12:03:05

标签: java hibernate second-level-cache

我正在使用hibernate 4.3MySQL 5.0.我正在尝试在我的示例中配置二级缓存,如下所示。

我的hibernate.cfg.xml

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

我的实体类

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Cacheable
public class Employee { ...}

我正在接受以下异常

Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/service/jndi/JndiNameException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.jboss.logging.Logger$1.run(Logger.java:2252)
at java.security.AccessController.doPrivileged(Native Method)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2227)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2214)
at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.<clinit>(AbstractEhcacheRegionFactory.java:77)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:242)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:125)
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:87)
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:46)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:83)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:223)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:197)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:295)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2442)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2438)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1855)
at com.example.hibernate.Hibernate4Main.main(Hibernate4Main.java:33)
Caused by: java.lang.ClassNotFoundException: org.hibernate.service.jndi.JndiNameException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 22 more

我的配置缺少什么?我知道hibernate 4.x中的二级缓存配置与3.x不同。我已经搜索过了,但没有为我找到合适的答案。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您获得java.lang.NoClassDefFoundError的原因的可能原因 1)Java Classpath中没有类 2)您可能正在使用jar命令运行程序,并且未在清单文件的ClassPath属性中定义类 3)任何启动脚本都会覆盖Classpath环境变量。

4)因为NoClassDefFoundError是java.lang.LinkageError的子类,所以如果其中一个依赖项(如本机库)可能不可用,它也会出现。

4)检查日志文件中的java.lang.ExceptionInInitializerError。由于静态初始化失败而导致的NoClassDefFoundError非常普遍。

5)如果你在J2EE环境中工作,那么多个类加载器中的Class的可见性也会导致java.lang.NoClassDefFoundError。