今天刚搭建的JPA(hibernate4.1)+spring 3.1+struts2.3.8环境,遇到了下面异常,查看对比了hibernate3和hibernate4的区别,发现hibernate4是删除了util包,所以说应该hibernate4的话,应该不用下面那个类,求解释!
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="cn.huanglei_jmr">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClass" value="${mysql.driverClassName}"/>
<property name="jdbcUrl" value="${mysql.url}"/>
<property name="user" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
<!--连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="5" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="30" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="10" />
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="0" />
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
</bean>
<!--
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="huanglei_jmr"/>
</bean> -->
<!-- 类工厂由spring管理 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- <property name="dataSource" ref="dataSource"/> --><!-- 注入数据源bean到实体管理工厂bean -->
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
<!-- <property name="loadTimeWeaver">运行时植入
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property> -->
<property name="persistenceUnitName" value="huanglei_jmr"/>
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!--注册关于@Transactional的解析器 -->
<tx:annotation-driven transaction-manager="txManager"/>
<context:annotation-config />
</beans>
错误信息:
三月 16, 2013 3:23:04 下午 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'buyerServiceBean': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:342)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.
..............
Caused by: java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2904)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1173)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1681)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at org.hibernate.cfg.Configuration.reset(Configuration.java:325)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:264)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:268)
at org.hibernate.ejb.Ejb3Configuration.<clinit>(Ejb3Configuration.java:150)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:71)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:257)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 39 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.util.DTDEntityResolver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
... 55 more
三月 16, 2013 3:23:04 下午 org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
三月 16, 2013 3:23:04 下午 org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/MyWeb01] startup failed due to previous errors
三月 16, 2013 3:23:04 下午 org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
网上有说:hibernate.annotation.jar不需要,但是我也没导入啊,值导入了:hibernate.common-annotation.jar这个应该没问题!
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" >
<persistence-unit name="huanglei_jmr" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- JPA驱动提供者 -->
</persistence-unit>
</persistence>
请大神指教!!!谢谢
------解决思路----------------------
Caused by: java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver
少包了
------解决思路----------------------
------解决思路----------------------
请问LZ这个问题怎么解决的?
------解决思路----------------------
你把不需要的包统统删掉就好了!!!!!
那是因为用了JPA(hibernate4.1)+spring 3.1+struts2.3.8 之前版本的包。我直接覆盖原来低版本的包,竟是包错。还不如搭建新环境了。
------解决思路----------------------
找这个类org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
所在jar版本是不是不兼容
------解决思路----------------------
hibernate4 的org.springframework.orm.hibernate4.LocalSessionFactoryBean是直接支持annotation的所以你不需要导入hibernate.annotation.jar如果你导入了这个包就会包这个错误了。hibernate4需要导入的包:
antlr-2.7.7.jar
c3p0-0.9.2.1.jar(因为我使用的c3p0去连接数据库的)
dom4j-1.6.1.jar
hibernate-c3p0-4.2.3.Final.jar(使用c3p0去连接数据库连接池)
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.3.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
mchange-commons-java-0.2.3.4.jar