【问题标题】:hibernate.cfg.xml pathhibernate.cfg.xml 路径
【发布时间】:2011-05-17 13:52:43
【问题描述】:

我有一个 java 项目,我正在使用 hibernate。这里的事情是我想将 hibernate.cfg.xml 文件放在“src”文件夹之外,但是当我配置这个文件时它显示 FileNotFoundException。如果我把它放在 src 文件夹中就可以了。但我想将 java src 和所有配置文件分开放在单独的文件夹中。

                               root
                                |____src
                                |____conf
                                      |____mapping
                                             |____(all xml file with hibernate.cfg.xml)

SessionFactory _sessionFactory = (new Configuration()).configure("./conf/mapping/hibernate.cfg.xml").buildSessionFactory();

它的显示异常......

【问题讨论】:

    标签: hibernate.cfg.xml


    【解决方案1】:

    将 conf/mapping 目录添加到您的CLASSPATH 并加载配置文件

    new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
    

    或者只是

    new Configuration().buildSessionFactory();
    

    因为这是标准名称。

    不管资源路径的开头不会有./

    【讨论】:

      【解决方案2】:

      你想做的是

      • 在您的project build path 中包含conf/mapping(使用Eclipse properties->javabuild path->source->add Folder
      • 那么不需要在你的代码中精确的休眠配置文件 new Configuration().buildSessionFactory();就够了

      这种操作很简单,而且效果很好。

      【讨论】:

        猜你喜欢
        • 2019-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-22
        • 2010-10-03
        • 2018-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多