0

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.!

6
  • Where is your hibernate.cfg.xml file located within your project? Commented Mar 20, 2013 at 10:35
  • Its located at src/main/java/com/me/app/hibernate.cfg.xml Commented Mar 20, 2013 at 10:37
  • 1
    So your project is Maven based? Try putting your configuration file into src/main/resources Commented Mar 20, 2013 at 10:43
  • Please let me know it that solved your problem. I would then copy my comment as answer so that your question can be closed. Commented Mar 20, 2013 at 10:51
  • Yeah the project is Maven Based. I tried two things I tried putting the file in src/main/java/ its giving me TypeMismatchException. Then as you said I tried putting it in src/main/resources I get nested exception is java.lang.NullPointerException. I dont't exactly know but I guess putting that file in src/main/java/ is working. I just need to make sure about the Types. Commented Mar 20, 2013 at 10:53

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks benzonico I will definitely try injecting the session factory into my code. Thanks for your valuable input.
0

benzonico and KHY thanks for your inputs. The first mistake that I was doing was I placed the hibernate.cfg.xml inside src/main/java/com/me/app/ which should have been src/main/java/. This worked for me. The next part about TypedMismatch was

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

I was trying to get the String reference from the database which was actually Integer. So that solved the typedMismatchException.

I will definitely try injecting the session factory into my code through spring. Since I am new to this frameworks I learnt a loads while discussing with you and trying the new codes.

Thanks again for your valuable feedback.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.