Hibernate - 将上下文销毁事件发送到侦听器实例的异常

时间:2012-06-26 00:35:30

标签: hibernate configuration listener

我有一个配置监听器,当用户点击应用程序时会创建一个Hibernate会话。我从contextDestroyed方法中收到错误。谁能帮我这个?以下是导致问题的方法。

public void contextDestroyed(ServletContextEvent arg0) {
    this.sessionFactory.close();
}

以下是我的控制台上的错误消息。

SEVERE: Exception sending context initialized event to listener instance of class com.configuration.ConfigurationListener
java.lang.NoClassDefFoundError: org/apache/commons/collections/SequencedHashMap
    at org.hibernate.mapping.Table.<init>(Table.java:32)
    at org.hibernate.cfg.Mappings.addTable(Mappings.java:120)
    at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:251)
    at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:236)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:152)
    at org.hibernate.cfg.Configuration.add(Configuration.java:362)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:400)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:449)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1263)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1235)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1184)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
    at com.configuration.ConfigurationListener.contextInitialized(ConfigurationListener.java:19)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3830)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4337)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.SequencedHashMap
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
    ... 30 more
Jun 25, 2012 5:21:59 PM org.apache.catalina.core.StandardContext start
SEVERE: Error listenerStart
Jun 25, 2012 5:21:59 PM org.apache.catalina.core.StandardContext start
SEVERE: Context [/ghs1986] startup failed due to previous errors
Jun 25, 2012 5:21:59 PM org.apache.catalina.core.StandardContext listenerStop
SEVERE: Exception sending context destroyed event to listener instance of class com.configuration.ConfigurationListener
java.lang.NullPointerException
    at com.configuration.ConfigurationListener.contextDestroyed(ConfigurationListener.java:25)
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:3869)
    at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4503)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4367)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

1 个答案:

答案 0 :(得分:3)

主要问题不在于contextDestroy方法。问题出在你的类路径中。你错过了类路径中的apache commons collections jar。 Hibernate依赖于它而失败

java.lang.ClassNotFoundException: org.apache.commons.collections.SequencedHashMap

这会导致稍后调用侦听器的contextDestoryed方法。该方法尝试close()sessionFactory。但sessionFactory从未初始化,因为Hibernate由于前面提到的ClassNotFoundException而从未成功。

全部写在粘贴的控制台日志中。