Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

java - hibernate.cfg.xml not found

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 :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The hibernate.cfg.xml file shoul be in root directory of the classpath of your project. If you using Maven then make sure it should be like src > resources > hibernate.cfg.xml.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...