获取HTTP状态500 - org.hibernate.internal.util.config.ConfigurationException:指定的cfg.xml文件不存在

时间:2016-05-13 18:46:34

标签: java hibernate maven tomcat hibernate.cfg.xml

我正在开发一个使用Java,Spring MVC和Hibernate的项目(我的IDE是IntelliJ)。当我在部署到Tomcat后尝试访问我的localhost上的URL(通过Hibernate对我的Oracle数据库进行简单调用)时,收到以下错误:

  

HTTP状态500 - 请求处理失败;嵌套异常是   org.hibernate.internal.util.config.ConfigurationException:指定   cfg.xml文件   [C:\ Apache的Tomcat的7.0.52 \ BIN \ SRC \主\资源\的hibernate.cfg.xml]   不存在

我的问题:为什么我的程序在我的apache-tomcat文件夹中查找?我从未在代码中指定任何内容来查看该文件夹,并且在使用Hibernate时我的所有测试都通过了。

到目前为止解决问题的方法:

  1. 尝试从头开始重建我的WAR文件
  2. 进行maven清理,编译,安装,然后重新部署到Tomcat
  3. org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [/HibernateTest/src/hibernate.cfg.xml]
  4. http://www.mkyong.com/hibernate/how-to-load-hibernate-cfg-xml-from-different-directory/
  5. Location of hibernate.cfg.xml in project?
  6. 上面列出的所有方法都不适用于我。我在下面提供了我的代码以及我的文件结构以帮助:

    我的SessionFactory方法:

    private static SessionFactory getSessionFactory() {
        String hibernatePropsFilePath = "src/main/resources/hibernate.cfg.xml";
        File hibernatePropsFile = new File(hibernatePropsFilePath);
    
        Configuration configuration = new Configuration();
        configuration.configure(hibernatePropsFile);
        configuration.addAnnotatedClass(Request.class);
    
        StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    
        ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
    
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    
        return sessionFactory;
    }
    

    Application.java

    @RestController
    @RequestMapping("/ourApp")
    public class Application {
    
        @RequestMapping(value = "/getRequestResponse", method = RequestMethod.GET, headers = "Accept=application/json")
        @ResponseBody
        public String returnRequestResponse() {
            RequestService requestService = new RequestService();
            Request request = requestService.findById(1);
            Gson gson = new Gson();
            return gson.toJson(request);
        }
    }
    

    文件结构:

    enter image description here

    更新

    如果我将Hibernate配置文件放在我的Tomcat文件夹中,这是有效的。

    我尝试在SessionFactory方法中实现shankarsh15的解决方案时,我的测试确实失败了。在我的测试运行之前,这是我的setUp函数:

    @Before
    public void setUp() throws Exception {
    
        Configuration configuration = new Configuration();
        configuration.addAnnotatedClass(MyService.class)
                .addAnnotatedClass(MyModel.class);
        configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
        configuration.setProperty("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver");
        configuration.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@//servername.mycompany.com:12345/abcdefg");
        configuration.setProperty("hibernate.connection.username", "username");
        configuration.setProperty("hibernate.connection.password", "password");
    
        sessionFactory = configuration.buildSessionFactory();
        session = sessionFactory.openSession();
    }
    

    更新了hibernate.cfg.xml:

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="connection.url">jdbc:oracle:thin:@//servername.mycompany.com:12345/abcdefg</property>
            <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
            <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
            <property name="connection.username">username</property>
            <property name="connection.password">password</property>
            <property name="show_sql">true</property>
            <mapping class="com.mycompany.project.modelName.model.MyModelClass"/>
        </session-factory>
    </hibernate-configuration>
    

3 个答案:

答案 0 :(得分:0)

请不要直接指定hibernate.cfg.xml文件,请按以下方式重写代码:

Configuration configuration = new Configuration().configure();
ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
registry.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();

只需确保 hibernate.cfg.xml 位于类路径的根目录,在您的情况下为真,因为它位于 src / main下/资源即可。

答案 1 :(得分:0)

您是否尝试过为配置文件指定完整路径?或尝试不指定src例如。 main / resources / ....以及为什么它在你的tomcat文件夹中搜索而不是在你的源代码中搜索?

答案 2 :(得分:0)

我不应该手动检查hibernate配置文件。相反,我的getSessionFactory()方法应如下所示:

private static SessionFactory getSessionFactory() {
        Configuration configuration = new Configuration().configure();
        configuration.addAnnotatedClass(MyClassNameHere.class);
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());
        return configuration.buildSessionFactory(builder.build());
    }