【问题标题】:hibernate.cfg.xml not found未找到 hibernate.cfg.xml
【发布时间】:2012-01-02 00:58:15
【问题描述】:

我是 Hibernate 的新手,正在阅读《Java persistence with Hibernate》这本书,我正在尝试从那里实现示例。到目前为止,我的 Ant 构建成功,但是当我尝试执行包含 main 方法的类时,我收到以下错误消息:

19-Nov-2011 18:40:09 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.3
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
19-Nov-2011 18:40:09 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
19-Nov-2011 18:40:09 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at persistence.HibernateUtil.<clinit>(Unknown Source)
    at hello.Driver.main(Unknown Source)
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
    ... 2 more

很明显,hibernate 找不到我的配置文件,它位于根目录中。

项目

+lib
<all required libraries>
+src
  +hello
    HelloWorld.java
    Message.java
    message.hbm.xml
  +persistence
    HibernateUtil.java
build.xml
hibernate.cfg.xml

我的完整源代码可以在这里找到:http://pastebin.com/bGDUrxUf

我有一个正在运行的 MySQL 服务器,其中包含一个数据库 hibernateapp 和表消息

谢谢:)

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    hibernate.cfg.xml 文件应位于项目类路径的根目录中。如果您使用 Maven,请确保它应该类似于 src > resources > hibernate.cfg.xml。

    【讨论】:

      【解决方案2】:

      您的hibernate.cfg.xml 需要在src 目录中;否则它不会被 Ant 的 copymetafiles 目标覆盖,因此它不会最终出现在您编译的类路径中。

      【讨论】:

      • 我确实将它移到了 src,使用 ant 重新编译并运行包含 main 方法的类作为 Java 应用程序并得到相同的错误?我需要设置一些 GLOBAL PATH 吗?
      • @Agop:确保它最终位于类路径上的一个目录的根目录中。例如,如果您正在运行 java -classpath lib/foo.jar:lib/bar.jar:. hello.HelloWorld,那么您需要确保它以 . 结尾。
      • 我使用 Eclipse 运行它,所以我相信类路径应该没问题?
      • 我不是说“你的类路径错误”,我是说“无论你的类路径是什么,确保你的 hibernate.cfg.xml 最终在其中”。
      【解决方案3】:

      它不应该在你的根目录中,它应该在你的类路径中。

      【讨论】:

        【解决方案4】:

        您可以使用采用hibernateConfig File 参数的configure(File configFile) 方法从不同的目录(不一定是类路径)加载hibernate.cfg.xml。 (注意,我使用的是休眠 4.3.7)

        像这样:


        String hibernatePropsFilePath = "/etc/configs/hibernate.cfg.xml";
        
        File hibernatePropsFile = new File(hibernatePropsFilePath);
        
        Configuration configuration = new Configuration(); 
        configuration.configure(hibernatePropsFile);
        
        StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        
        ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
        
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        

        【讨论】:

          【解决方案5】:

          如果您正在处理Intellij Idea,则在 src\main\java 下创建一个名为“resources”的文件夹。打开项目的模块设置,从左侧选择“模块”,然后在“源”选项卡中选择新创建的“资源”文件夹并将其标记为“资源”。在这个新创建的“资源”文件夹中创建hibernate.cfg.xml 文件。

          那么这应该可以工作

          Configuration con = new Configuration().configure("hibernate.cfg.xml");
          

          【讨论】:

            【解决方案6】:

            虽然已经晚了,但解决方案是您需要将此配置文件放在资源文件夹(projectxxxx->Resources)中,前提是它是一个maven项目。

            【讨论】:

              【解决方案7】:

              XML 配置文件默认位于您的 CLASSPATH 的根目录中。

              您可以使用以下方法选择不同的 XML 或路径配置文件:

                      SessionFactory sessionFactory;
                      try {
                          Logger log = Logger.getLogger(LOG);
                          final String HIB_CONFIG = "/path/to/hibernate.cfg.xml";
              
                          final File hibernate = new File(HIB_CONFIG);
                          log.info("Try to init SessionFactory: " + HIB_CONFIG);
              
                          // Create the SessionFactory from hibernate.cfg.xml
                          if (hibernate.exists()) {
                              log.info("File exists. Init with custom file.");
                              sessionFactory = new Configuration()
                                      .configure(hibernate)
                                      .buildSessionFactory();
                          } else {
                              log.info("File doesn't exist. Init with default project file.");
                              sessionFactory = new Configuration().configure().buildSessionFactory();
                          }
                      } catch (Throwable ex) {
                          throw new ExceptionInInitializerError(ex);
                      }
              

              更多信息请关注session-configuration

              【讨论】:

                猜你喜欢
                • 2011-06-23
                • 2015-02-07
                • 1970-01-01
                • 2013-01-24
                • 1970-01-01
                • 1970-01-01
                • 2013-02-21
                • 2012-03-16
                相关资源
                最近更新 更多