Spring 3 + Hibernate 4 + C3P0: java.lang.NoSuchMethodError: org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.addAnnotatedClass

Spring 3 + Hibernate 4 + C3P0: java.lang.NoSuchMethodError: org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.addAnnotatedClass

我的网络应用运行良好,现在我想将 C3P0 添加到我的网络应用中以管理连接池。一切似乎都正常,但我收到以下错误导致一堆异常:

java.lang.NoSuchMethodError: org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration;
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.addAnnotatedClasses(LocalSessionFactoryBuilder.java:165)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:341)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
... 86 more

这是我的配置文件:

spring-config.xml

    <?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:tx="http://www.springframework.org/schema/tx" 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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="com.mycompany.myproject.business" />
    <context:annotation-config />

    <!-- Beans Declaration -->
    <bean id="InfantInfo" class="com.mycompany.myproject.dataaccess.model.InfantInfo"/>
    <bean id="User" class="com.mycompany.myproject.dataaccess.model.User"/>

    <!-- Service Declaration -->
    <bean id="InfantInfoBoImpl" class="com.mycompany.myproject.business.bo.impl.InfantInfoBoImpl">
        <property name="infantInfoDao" ref="InfantInfoDaoImpl" />
    </bean>
    <bean id="UserBoImpl" class="com.mycompany.myproject.business.bo.impl.UserBoImpl">
        <property name="userDao" ref="UserDaoImpl" />
    </bean>

    <!-- DAO Declaration -->
    <bean id="InfantInfoDaoImpl" class="com.mycompany.myproject.dataaccess.dao.impl.InfantInfoDaoImpl">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>
    <bean id="UserDaoImpl" class="com.mycompany.myproject.dataaccess.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>
</beans>   

休眠-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/WEB-INF/database.properties</value>
        </list>
    </property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.user}" />
    <property name="password" value="${jdbc.password}" />
    <!-- these are C3P0 properties -->
    <property name="initialPoolSize" value="${initialPoolSize}"/>
    <property name="acquireIncrement" value="${acquireIncrement}" />
    <property name="minPoolSize" value="${minPoolSize}" />
    <property name="maxPoolSize" value="${maxPoolSize}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.mycompany.myproject.dataaccess.model.InfantInfo</value>
            <value>com.mycompany.myproject.dataaccess.model.User</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

database.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;characterEncoding=utf-8
jdbc.user=user
jdbc.password=password

initialPoolSize=10
acquireIncrement=10
minPoolSize=10
maxPoolSize=50

pom.xml

<properties>
    <mysql.version>5.1.25</mysql.version>
    <log4j.version>1.2.17</log4j.version>
    <jpa.version>2.0.Beta-20090815</jpa.version>
    <spring.version>3.2.3.RELEASE</spring.version>
    <commons-dbcp.version>1.4</commons-dbcp.version>
    <hibernate.version>4.2.2.Final</hibernate.version>
    <commons-codec.version>1.9</commons-codec.version>
    <hibernate-c3p0.version>3.6.3.Final</hibernate-c3p0.version>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <hibernate-commons-annotations.version>3.3.0.ga</hibernate-commons-annotations.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.glassfish.metro</groupId>
        <artifactId>webservices-rt</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>${commons-dbcp.version}</version>
    </dependency>
    <!--Spring MVC-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!--Hibernate-->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.java-persistence</groupId>
        <artifactId>jpa-api</artifactId>
        <version>${jpa.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>${hibernate-c3p0.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>${hibernate-commons-annotations.version}</version>
    </dependency>
    <!--MySQL-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>
    <!--APACHE COMMONS CODEC-->
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>${commons-codec.version}</version>
    </dependency>
    <!--LOG4J-->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>
</dependencies>

这有点令人困惑... 不知道是什么原因....

感谢您的评论。

编辑:

Maven 依赖关系树:

    com.mycompany:myproject:war:1.0-SNAPSHOT
+- org.glassfish.metro:webservices-rt:jar:2.3:compile
|  \- org.glassfish.metro:webservices-api:jar:2.3:compile
|     \- javax.annotation:javax.annotation-api:jar:1.2-b03:runtime
+- javax:javaee-web-api:jar:7.0:provided
+- org.springframework:spring-core:jar:3.2.3.RELEASE:compile
|  \- commons-logging:commons-logging:jar:1.1.1:compile
+- org.springframework:spring-context:jar:3.2.3.RELEASE:compile
|  +- org.springframework:spring-aop:jar:3.2.3.RELEASE:compile
|  \- org.springframework:spring-beans:jar:3.2.3.RELEASE:compile
+- org.springframework:spring-jdbc:jar:3.2.3.RELEASE:compile
+- org.springframework:spring-orm:jar:3.2.3.RELEASE:compile
|  \- aopalliance:aopalliance:jar:1.0:compile
+- org.springframework:spring-tx:jar:3.2.3.RELEASE:compile
+- org.springframework:spring-expression:jar:3.2.3.RELEASE:compile
+- commons-dbcp:commons-dbcp:jar:1.4:compile
|  \- commons-pool:commons-pool:jar:1.5.4:compile
+- org.springframework:spring-web:jar:3.2.3.RELEASE:compile
+- org.springframework:spring-webmvc:jar:3.2.3.RELEASE:compile
+- org.hibernate:hibernate-core:jar:4.2.2.Final:compile
|  +- antlr:antlr:jar:2.7.7:compile
|  +- org.jboss.logging:jboss-logging:jar:3.1.0.GA:compile
|  +- dom4j:dom4j:jar:1.6.1:compile
|  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:jar:1.0.1.Final:compile
|  +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
|  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.2.Final:compile
|  \- org.javassist:javassist:jar:3.15.0-GA:compile
+- org.hibernate.java-persistence:jpa-api:jar:2.0.Beta-20090815:compile
+- org.hibernate:hibernate-c3p0:jar:3.6.3.Final:compile
|  \- org.slf4j:slf4j-api:jar:1.6.1:compile
+- org.hibernate:hibernate-commons-annotations:jar:3.3.0.ga:compile
|  +- org.hibernate:hibernate:jar:3.2.1.ga:compile
|  |  +- net.sf.ehcache:ehcache:jar:1.2.3:compile
|  |  +- javax.transaction:jta:jar:1.1:compile
|  |  +- asm:asm-attrs:jar:1.5.3:compile
|  |  +- cglib:cglib:jar:2.1_3:compile
|  |  +- asm:asm:jar:1.5.3:compile
|  |  \- commons-collections:commons-collections:jar:2.1.1:compile
|  \- javax.persistence:persistence-api:jar:1.0:compile
+- c3p0:c3p0:jar:0.9.1.2:compile
+- mysql:mysql-connector-java:jar:5.1.25:compile
+- commons-codec:commons-codec:jar:1.9:compile
\- log4j:log4j:jar:1.2.17:compile

您正在混合使用 Hibernate 依赖项:

  1. spring-orm 带有休眠版本(例如 3.3.2.GA)
  2. hibernate-core 是 4.2.2.Final
  3. hibernate-c3p0 是 3.6.3.Final
  4. JPA版本为2.0,仅supported by Hibernate 3.5+

运行 a mvn:dependency:tree 并确保您使用单个 Spring 版本(例如 3.2.3.RELEASE)和单个 Hibernate 版本(例如 3.6.3.Final).

我使用了错误的 c3p0 依赖项。现在,使用以下配置,它就像一个魅力一样工作。我希望这对面临同样问题的人有用。

对了,我把"hibernate-config.xml"和"spring-config.xml"合并了,去掉了"database.properties"。我宁愿保持简单 :D

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany</groupId>
    <artifactId>MyProject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>MyProject</name>

    <properties>
        <cp30.version>0.9.1.2</cp30.version>
        <log4j.version>1.2.17</log4j.version>
        <mysql.version>5.1.25</mysql.version>
        <jpa.version>1.0.0.Final</jpa.version>
        <spring.version>4.1.5.RELEASE</spring.version>
        <hibernate.version>4.3.8.Final</hibernate.version>
        <commons-codec.version>1.9</commons-codec.version>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.metro</groupId>
            <artifactId>webservices-rt</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Spring MVC-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Hibernate-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>${jpa.version}</version>
        </dependency>
        <!--C3P0 Connection Pooling-->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>${cp30.version}</version>
        </dependency>
        <!--MySQL-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!--APACHE COMMONS CODEC-->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>${commons-codec.version}</version>
        </dependency>
        <!--LOG4J-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

spring-config.xml

    <?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:tx="http://www.springframework.org/schema/tx" 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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="com.mycompany.myproject.business" />
    <context:annotation-config />

    <!-- Beans Declaration -->
    <bean id="InfantInfo" class="com.mycompany.myproject.dataaccess.model.InfantInfo"/>
    <bean id="User" class="com.mycompany.myproject.dataaccess.model.User"/>

    <!-- Service Declaration -->
    <bean id="InfantInfoBoImpl" class="com.mycompany.myproject.business.bo.impl.InfantInfoBoImpl">
        <property name="infantInfoDao" ref="InfantInfoDaoImpl" />
    </bean>
    <bean id="UserBoImpl" class="com.mycompany.myproject.business.bo.impl.UserBoImpl">
        <property name="userDao" ref="UserDaoImpl" />
    </bean>

    <!-- DAO Declaration -->
    <bean id="InfantInfoDaoImpl" class="com.mycompany.myproject.dataaccess.dao.impl.InfantInfoDaoImpl">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>
    <bean id="UserDaoImpl" class="com.mycompany.myproject.dataaccess.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>

    <!--Hibernate-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/behdasht?useUnicode=true&amp;characterEncoding=utf-8"/>
        <property name="user" value="user"/>
        <property name="password" value="password"/>
        <property name="minPoolSize" value="10"/>
        <property name="maxPoolSize" value="20"/>
        <property name="maxIdleTime" value="30"/>
        <property name="acquireIncrement" value="3"/>
    </bean>
    <bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.mycompany.myproject.dataaccess.model.InfantInfo</value>
                <value>com.mycompany.myproject.dataaccess.model.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="txManager" />
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>
</beans>