Caused by: java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration;

前端 未结 1 853
长情又很酷
长情又很酷 2021-01-16 03:09

I am developing Spring MVC + POI + Hibernate example by taking a reference from link : https://krams915.blogspot.in/2011/02/spring-3-apache-poi-hibernate-creating.html?showC

相关标签:
1条回答
  • 2021-01-16 03:26

    A lots of modification needs to be done in order to use the latest dependencies. I used Spring version 4.3.1.RELEASE and Hibernate version 4.3.11.Final and also used latest version of dependencies for others too.

    Here you need to change below in hibernate-context.xml

    For hibernate 3.0

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
                     p:dataSource-ref="dataSource"
                     p:configLocation="${hibernate.config}"
                     p:packagesToScan="org.krams.tutorial"/>
    

    For Hibernate 4.0.x

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
                     p:dataSource-ref="dataSource"
                     p:configLocation="${hibernate.config}"
                     p:packagesToScan="org.krams.tutorial"/>
    

    Also: For hibernate 3.0

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
                    p:sessionFactory-ref="sessionFactory" />
    

    For Hibernate 4.0

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
                    p:sessionFactory-ref="sessionFactory" />
    

    IMP Note: Hibernate 3.5 and onward contains Hibernate Annotations. : You should remove the dependency on hibernate-annotations.

    The latest pom.xml

    <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.8</java.version>
            <spring.version>4.3.1.RELEASE</spring.version>
            <hibernate.version>4.3.11.Final</hibernate.version>
            <commons-digester.version>2.1</commons-digester.version>
            <mysql.version>5.1.39</mysql.version>
            <poi.version>3.6</poi.version>
            <javassist.version>3.20.0-GA</javassist.version>
            <c3p0.version>0.9.1.2</c3p0.version>
            <persistence-api-version>1.0.2</persistence-api-version>
            <logback.version>1.1.2</logback.version>
            <slf4j.version>1.7.6</slf4j.version>
        </properties>
    
        <dependencies>
            <!-- Spring Web and Web MVC -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <!-- Spring TX -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <!-- Spring ORM -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <!-- Commons Digester -->
            <dependency>
                <groupId>commons-digester</groupId>
                <artifactId>commons-digester</artifactId>
                <version>${commons-digester.version}</version>
            </dependency>
    
            <!-- Commons Collection -->
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>3.2.1</version>
            </dependency>
    
            <!-- Apache POI -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi.version}</version>
            </dependency>
    
            <!-- CGLib Nodep -->
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib-nodep</artifactId>
                <version>3.2.4</version>
            </dependency>
    
            <!-- Javassist -->
            <dependency>
                <groupId>org.javassist</groupId>
                <artifactId>javassist</artifactId>
                <version>${javassist.version}</version>
            </dependency>
    
            <!-- Hibernate -->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <!-- <version>3.3.2.GA</version> -->
                <version>${hibernate.version}</version>
            </dependency>
    
            <!-- Persistent API -->
            <dependency>
                <groupId>javax.persistence</groupId>
                <artifactId>persistence-api</artifactId>
                <version>${persistence-api-version}</version>
            </dependency>
    
            <!-- C3PO -->
            <dependency>
                <groupId>c3p0</groupId>
                <artifactId>c3p0</artifactId>
                <version>${c3p0.version}</version>
            </dependency>
    
            <!-- MySQL -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
    
            <!-- Logging -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>${logback.version}</version>
            </dependency>
    
    
            <!-- Servlet API, JSTL, Standard -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
            </dependency>
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.1.2</version>
            </dependency>
            <dependency>
                <groupId>taglibs</groupId>
                <artifactId>standard</artifactId>
                <version>1.1.2</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
    
            <!-- Junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    0 讨论(0)
提交回复
热议问题