【问题标题】:HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: /hibernate.cfg.xml not foundHTTP 状态 500 - 请求处理失败;嵌套异常是 org.hibernate.HibernateException: /hibernate.cfg.xml not found
【发布时间】:2013-03-20 10:28:07
【问题描述】:

我是 Hibernate 和 Spring 的新手,在执行应用程序时遇到上述异常。

我正在尝试从数据库中获取记录的值。

以下是我得到的例外:-

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.

以下是我的文件:-

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


请帮我解决这个问题。提前致谢!

【问题讨论】:

  • 您的 hibernate.cfg.xml 文件在您的项目中的什么位置?
  • 它位于 src/main/java/com/me/app/hibernate.cfg.xml
  • 所以你的项目是基于 Maven 的?尝试将您的配置文件放入 src/main/resources
  • 请告诉我它解决了您的问题。然后我会复制我的评论作为答案,这样你的问题就可以结束了。
  • 是的,该项目是基于 Maven 的。我尝试了两件事,我尝试将文件放在 src/main/java/ 中,它给了我 TypeMismatchException。然后正如你所说,我尝试将它放在 src/main/resources 中,我得到嵌套异常是 java.lang.NullPointerException。我不完全知道,但我猜将该文件放在 src/main/java/ 中是有效的。我只需要确定类型。

标签: spring hibernate spring-mvc controller hibernate-mapping


【解决方案1】:

问题是你不应该在你的请求句柄中实例化你的休眠配置,而是让 spring 实例化你的 session factory 并将其注入你的控制器并从那里检索会话。这样做,您就不必为应用程序的每个请求实现会话工厂。

您可以查看question 或官方文档以详细了解如何通过 spring 实例化会话工厂。

这将(间接)解决您的问题。

【讨论】:

  • 感谢benzonico 我一定会尝试将会话工厂注入我的代码。感谢您的宝贵意见。
【解决方案2】:

benzonicoKHY 感谢您的意见。我犯的第一个错误是将 hibernate.cfg.xml 放在 src/main/java/com/me/app/ 中,它应该是 src/main/java/。这对我有用。关于 TypedMismatch 的下一部分是

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

我试图从实际上是整数的数据库中获取字符串引用。这样就解决了 typedMismatchException。

我一定会尝试通过 spring 将会话工厂注入我的代码中。由于我是这个框架的新手,我在与您讨论和尝试新代码时学到了很多东西。

再次感谢您的宝贵反馈。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-29
    • 2017-11-24
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    相关资源
    最近更新 更多