"Error creating bean" in Hibernate with Spring

1.1k Views Asked by At

I am working on a Spring with Hibernate project that also uses Maven. I am trying to make a page that has a form to capture search information as well as displaying the result. I have created the dispatcher-servlet, web.xml, controller, model, and view but when I try to run the project on the server I get the following 500 error:

Servlet.init() for servlet dispatcher threw exception (stacktrace and files below).

I am a little confused because I am following a tutorial and I believe I have all of my dependencies correct. I have taken a look at the stack trace and from what I have read, there seems to be a versioning problem. I have tried changing my versions in the pom.xml file but haven't had any luck. I also checked the WEB-INF/lib folder and I have hibernate-core as well as the other required jars, so I think the pom.xml is configured correctly.

From what I understand about Spring, the dispatcher-servlet should be injecting the sessionFactory bean, but it doesn't seem to be working correctly. I think if I can get past the sessionFactory problem, I would be able to move on.

pom.xml (I am not quite sure if my depencies are correct.)

    <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.yccd</groupId>
        <artifactId>StudentDataXML</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
    <properties>

   <org.springframework.version>4.3.2.RELEASE</org.springframework.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.3.1</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>5.1.0.Final</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>5.2.4.Final</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>

            <!-- <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc</artifactId>
        <version>10.2.0</version>
        </dependency> -->
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework.version}</version>
        </dependency>

        </dependencies>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <excludes>
                        <exclude>**/*.java</exclude>
                    </excludes>
                </resource>
                <resource>
                    <directory>src/main/webapp</directory>
                    <excludes>
                        <exclude>**/*.java</exclude>
                    </excludes>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </build>
    </project>

dispatcher-servlet.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:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"     
    xmlns:context="http://www.springframework.org/schema/context"
     xmlns:util="http://www.springframework.org/schema/util"     
    xmlns:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="http://www.springframework.org/schema/mvc     
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/util     
    http://www.springframework.org/schema/util/spring-util.xsd
      http://www.springframework.org/schema/context     
    http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/tx     
    http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop     
    http://www.springframework.org/schema/aop/spring-aop.xsd">


        <context:component-scan base-package="com.yccd.controllers" />
        <context:component-scan base-package="com.yccd.model" />

        <mvc:annotation-driven />

        <bean

    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/views/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>

    <context:property-placeholder location="classpath:connection.properties" />
        <bean name="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driverClass}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
        <bean id="sessionFactory"  
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="annotatedClasses">
       <list>
        <value>com.yccd.model.User</value>    
       </list>
      </property>
      <property name="hibernateProperties">
       <props>
        <prop key="hibernate.dialect">${jdbc.dialect}</prop>
         <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.show_sql">true</prop>
       </props>
      </property>
     </bean>
     <tx:annotation-driven />
        <bean id="transactionManager"
      class="org.springframework.orm.hibernate5.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
     </bean>
    </beans>

StudentController.java (I plan to use this as the primary controller for the application)

package com.yccd.controllers;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yccd.model.Student;

@Controller
@RequestMapping(value = "/student")
public class StudentController {

@RequestMapping(value = "/showAll", method = RequestMethod.GET)
public String sayHelloAgain(@PathVariable("queryType") String queryType,     
ModelMap model) {
        model.addAttribute("message", "Just to say hello again");
        model.addAttribute("queryTypeChoice", queryType);
        return "welcomeAgain";
    }

    @RequestMapping(value = "/searchResults", method = RequestMethod.GET)
    public String displayStudents(ModelMap model) {
        return "displayStudentRecords";
    }

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public String search(Student student, Map<String, Object> map) {
        //TO-DO: GET PARAM VALUES AND MAKE A CALL TO SERVICE LAYER HERE
        return "displayStudentRecords";
    }
}

displayStudentRecords.jsp (This is the page where I want to display the records obtained from the database)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>YCCD - Search</title>
</head>
<body>
<h3>Search Student Record Form</h3>
    <form name="searchStudentsForm" action="/search/searchStudents">
        <table>
            <tr>
                <td>Student ID:</td>
                <td><input type="text" name="stid" /></td>
            </tr>
            <tr>
                <td>Gender:</td>
                <td><input type="text" name="gender" /></td>
            </tr>
            <tr>
                <td>AGE:</td>
                <td><input type="text" name="age" /></td>
            </tr>
            <tr>
            <td colspan="2"><input type="submit" value="Search"></td>
            </tr>
        </table>
    </form>
</body>
</html>

I am fairly new to Spring, Hibernate and Maven. So I apologize if this seems trivial.

This is only part of the stack trace. I'm not very good at determining what the problem is from looking at the stack trace so if there is some pointers that you can give me like what part of it to look at first or if there are any areas that I will be able to ignore, that would be very helpful as well.

Stacktrace

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate5.LocalSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate5.LocalSessionFactoryBean

Related cause:

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate5.LocalSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate5.LocalSessionFactoryBean
1

There are 1 best solutions below

1
On BEST ANSWER

You're missing some spring dependencies. Try with these (add the correct version tag to the dependencies):

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <scope>test</scope>
    </dependency>