一尘不染

找不到hibernate.cfg.xml

hibernate

我是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 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和表消息

谢谢 :)


阅读 303

收藏
2020-06-20

共1个答案

一尘不染

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

2020-06-20