找不到hibernate.cfg.xml [英] hibernate.cfg.xml not found

查看:93
本文介绍了找不到hibernate.cfg.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate的新手,阅读本书Hibernate的Java持久化,并试图从那里实现这个例子。到目前为止,我的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
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment buildBytecodeProvider
INFO:字节码提供程序名称:cglib
19- Nov-2011 18:40:09 org.hibernate.cfg.Environment< clinit>
INFO:使用JDK 1.4 java.sql.Timestamp处理
19-Nov-2011 18:40:09 org.hibernate.cfg.Configuration configure
INFO:从资源配置:/ hibernate。 cfg.xml
19-Nov-2011 18:40:09 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO:配置资源:/hibernate.cfg.xml
线程main中的异常java.lang.ExceptionInInitializerError
at persistence.HibernateUtil。< clinit>(Unknown Source)
at hello.Driver.main(Unknown Source)
引起:org.hibernate.HibernateException:/ hibernate.cfg.xml未找到
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

显然,休眠nate找不到我的配置文件,它位于根目录。



项目

  + lib 
<所有必需的库>
+ src
+ hello
HelloWorld.java
Message.java
message.hbm.xml
+持久性
HibernateUtil.java
build.xml
hibernate.cfg.xml

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



我有一个运行MySQL服务器的数据库hibernateapp和表消息

感谢:)

解决方案

您的 hibernate.cfg.xml 需要位于 src 目录中;否则它不会被Ant的 copymetafiles target覆盖,所以它不会在你编译的类路径中结束。


I am new to Hibernate, reading this book "Java persistence with Hibernate" and I am trying to implement the example from there. So far my Ant build is successful, but when I try to execute the class containing the main method I am getting this error message:

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

It is clear that hibernate can't find my config file, which is located in the root dir.

Project

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

My the complete source code can be found here: http://pastebin.com/bGDUrxUf

I have a running MySQL server with a database hibernateapp and table messages

Thanks :)

解决方案

Your hibernate.cfg.xml needs to be inside the src directory; otherwise it's not covered by Ant's copymetafiles target, so it won't end up in your compiled classpath.

这篇关于找不到hibernate.cfg.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆