Search code examples
springhibernatespring-mvccontrollerhibernate-mapping

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: /hibernate.cfg.xml not found


I am a newbie to Hibernate and Spring and I am getting the above exception while executing the application.

I am trying to fetch the values of a record from the database.

Following is the exception that I am getting :-

message Request processing failed; nested exception is org.hibernate.HibernateException: /hibernate.cfg.xml not found

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: /hibernate.cfg.xml not found
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

org.hibernate.HibernateException: /hibernate.cfg.xml not found
    org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
    org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1497)
    org.hibernate.cfg.Configuration.configure(Configuration.java:1519)
    org.hibernate.cfg.Configuration.configure(Configuration.java:1506)
    com.me.app.HomeController.handleRequestInternal(HomeController.java:56)
    org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.

Following are my file :-

POJO

public class Usertable {

    int id;
    String userName;
    String password;



    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }



}


Controller

//@Controller
public class HomeController extends AbstractController{

    //private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
//  @RequestMapping(value = "/", method = RequestMethod.GET)
//  public String home(Locale locale, Model model) {
//      logger.info("Welcome home! The client locale is {}.", locale);
//      
//      Date date = new Date();
//      DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
//      
//      String formattedDate = dateFormat.format(date);
//      
//      model.addAttribute("serverTime", formattedDate );
//      
//      return "home";
//  }

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        Usertable user;

        Configuration cfg = new Configuration();
        SessionFactory sf = cfg.configure().buildSessionFactory();
        Session hibsession = sf.openSession();





        Transaction tx = hibsession.beginTransaction();
        user = (Usertable)hibsession.get(Usertable.class, userName);

        System.out.println("UserName is "+ user.getUserName());
        System.out.println("Password is "+ user.getPassword());


        tx.commit();



        hibsession.close();

        return new ModelAndView("first","abc","abc");
    }


hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">tiger</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/contacts</property>
        <property name="hibernate.connection.username">root</property>

        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <mapping resource="Usertable.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


Usertable.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20 Mar, 2013 4:26:30 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.me.app.Usertable" table="USERTABLE">
        <id name="id" type="java.lang.Integer">
            <column name="UserID" />
            <generator class="native" />
        </id>
        <property name="userName" type="java.lang.String">
            <column name="UserName" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="UserPassword" />
        </property>
    </class>
</hibernate-mapping>


servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <!--  <resources mapping="/resources/**" location="/resources/" /> -->

    <beans:bean id="urlMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

    <beans:bean name="/books.htm" class="com.me.app.HomeController" />




    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.me.app" />



</beans:beans>


Kindly help me with this issue.Thanks in advance.!


Solution

  • The thing is you shouldn't instantiate your hibernate configuration in your handle of a request but rather let spring instantiate your session factory and inject it into your controller and retrieve the session from there. Doing this, then you won't have to implement a session factory for each request made to your application.

    You can check this question or the official documentation to have details on how to instantiate your session factory via spring.

    This will (indirectly) solve your problem.