在使用 Hibernate 进行数据库操作的时候,在启动 Tomcat 服务器后,Console 控制台可能会打印出这样的异常:java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
一、异常描述:
1 严重: Context initialization failed 2 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 3 'mysessionFactory' defined in class path resource [spring/applicationContext.xml]: 4 Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: 5 javax/persistence/EntityListeners 6 at 7 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory. 8 initializeBean(AbstractAutowireCapableBeanFactory.java:1628) 9 at 10 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean 11 (AbstractAutowireCapableBeanFactory.java:555) 12 at 13 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean 14 (AbstractAutowireCapableBeanFactory.java:483) 15 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject 16 (AbstractBeanFactory.java:306) 17 at 18 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton 19 (DefaultSingletonBeanRegistry.java:230) 20 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean 21 (AbstractBeanFactory.java:302) 22 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean 23 (AbstractBeanFactory.java:197) 24 at 25 org.springframework.beans.factory.support.DefaultListableBeanFactory. 26 preInstantiateSingletons(DefaultListableBeanFactory.java:742) 27 at 28 org.springframework.context.support.AbstractApplicationContext. 29 finishBeanFactoryInitialization(AbstractApplicationContext.java:866) 30 at org.springframework.context.support.AbstractApplicationContext.refresh 31 (AbstractApplicationContext.java:542) 32 at 33 org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext 34 (ContextLoader.java:443) 35 at org.springframework.web.context.ContextLoader.initWebApplicationContext 36 (ContextLoader.java:325) 37 at org.springframework.web.context.ContextLoaderListener.contextInitialized 38 (ContextLoaderListener.java:107) 39 at org.apache.catalina.core.StandardContext.listenerStart 40 (StandardContext.java:5068) 41 at org.apache.catalina.core.StandardContext.startInternal 42 (StandardContext.java:5584) 43 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) 44 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899) 45 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875) 46 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) 47 at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1260) 48 at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:2002) 49 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 50 at java.util.concurrent.FutureTask.run(FutureTask.java:266) 51 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 52 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 53 at java.lang.Thread.run(Thread.java:745) 54 Caused by: java.lang.NoClassDefFoundError: javax/persistence/EntityListeners 55 at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults 56 (JPAMetadataProvider.java:96) 57 at 58 org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults 59 (JavaReflectionManager.java:226) 60 at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1385) 61 at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1375) 62 at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory 63 (LocalSessionFactoryBean.java:660) 64 at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet 65 (AbstractSessionFactoryBean.java:191) 66 at 67 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory. 68 invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) 69 at 70 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory. 71 initializeBean(AbstractAutowireCapableBeanFactory.java:1624) 72 ... 25 more 73 Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListeners 74 at org.apache.catalina.loader.WebappClassLoaderBase.loadClass 75 (WebappClassLoaderBase.java:1858) 76 at org.apache.catalina.loader.WebappClassLoaderBase.loadClass 77 (WebappClassLoaderBase.java:1701) 78 ... 33 more 79 五月 08, 2017 7:31:51 下午 org.apache.catalina.core.StandardContext listenerStart 80 严重: Exception sending context initialized event to listener instance of class 81 org.springframework.web.context.ContextLoaderListener 82 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 83 'mysessionFactory' defined in class path resource [spring/applicationContext.xml]: 84 Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: 85 javax/persistence/EntityListeners 86 at 87 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory. 88 initializeBean(AbstractAutowireCapableBeanFactory.java:1628) 89 at 90 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean 91 (AbstractAutowireCapableBeanFactory.java:555) 92 at 93 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean 94 (AbstractAutowireCapableBeanFactory.java:483) 95 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject 96 (AbstractBeanFactory.java:306) 97 at 98 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton 99 (DefaultSingletonBeanRegistry.java:230) 100 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean 101 (AbstractBeanFactory.java:302) 102 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean 103 (AbstractBeanFactory.java:197) 104 at 105 org.springframework.beans.factory.support.DefaultListableBeanFactory. 106 preInstantiateSingletons(DefaultListableBeanFactory.java:742) 107 at 108 org.springframework.context.support.AbstractApplicationContext. 109 finishBeanFactoryInitialization(AbstractApplicationContext.java:866) 110 at org.springframework.context.support.AbstractApplicationContext.refresh 111 (AbstractApplicationContext.java:542) 112 at 113 org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext 114 (ContextLoader.java:443) 115 at org.springframework.web.context.ContextLoader.initWebApplicationContext 116 (ContextLoader.java:325) 117 at org.springframework.web.context.ContextLoaderListener.contextInitialized 118 (ContextLoaderListener.java:107) 119 at org.apache.catalina.core.StandardContext.listenerStart 120 (StandardContext.java:5068) 121 at org.apache.catalina.core.StandardContext.startInternal 122 (StandardContext.java:5584) 123 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) 124 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899) 125 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875) 126 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) 127 at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1260) 128 at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:2002) 129 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 130 at java.util.concurrent.FutureTask.run(FutureTask.java:266) 131 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 132 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 133 at java.lang.Thread.run(Thread.java:745) 134 Caused by: java.lang.NoClassDefFoundError: javax/persistence/EntityListeners 135 at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults 136 (JPAMetadataProvider.java:96) 137 at 138 org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults 139 (JavaReflectionManager.java:226) 140 at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1385) 141 at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1375) 142 at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory 143 (LocalSessionFactoryBean.java:660) 144 at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet 145 (AbstractSessionFactoryBean.java:191) 146 at 147 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory. 148 invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) 149 at 150 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory. 151 initializeBean(AbstractAutowireCapableBeanFactory.java:1624) 152 ... 25 more 153 Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListeners 154 at org.apache.catalina.loader.WebappClassLoaderBase.loadClass 155 (WebappClassLoaderBase.java:1858) 156 at org.apache.catalina.loader.WebappClassLoaderBase.loadClass 157 (WebappClassLoaderBase.java:1701) 158 ... 33 more
二、错误分析:
Hibernate 需要 jpa 支持,检查 WEB-INF/lib 下是否缺少 jpa 的 jar 包 hibernate-jpa-2.0-api-1.0.1.Final.jar ,
导入即可。