Where to place hibernate.cfg.xml?

送分小仙女□ 提交于 2019-11-26 20:18:38

问题


My project is like this:

/src/main/java
     -thegamers
         -app.java
         -hibernateutil.java

can someone tell me where to put the hibernate.cfg.xml?

because I'm getting this error:

Initial SessionFactory creation failed.org.hibernate.HibernateException: hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
    at thegamers.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
    at thegamers.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at thegamers.App.main(App.java:15)
Caused by: org.hibernate.HibernateException: hibernate.cfg.xml not found
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2149)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2130)
    at thegamers.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
    ... 2 more

回答1:


The config file hibernate.cfg.xml needs to be on the classpath.

This can be accomplished in different ways, depending on your project.

  • For a web-app WAR project (you are running the program in a Servlet container): placing it in WEB-INF/classes will work as files in WEB-INF/classes are visible on the classpath when app is running in container.

  • For a Maven-style project (not running the program in a Servlet container): placing it in /src/main/resources/ will work

  • For otherwise, try in the src/ directory.




回答2:


I'm using maven, and it didn't work for me until I put hibernate.cfg.xml in src/main/resources.




回答3:


At the root of your project: /src (at leat as default)

How to know if /src is the sources dir?
When you create a new Java class, it is contained in a package (normally it is called as the same name of the dir where it is created). So, in your class declarion you can see something like this:

package foo;

class MyClass{

In default IDE settings, the class should found under /src/foo/MyClass.java. As you can see, in this scenario /src acts as root sources dir.




回答4:


if it is not web project then do explicitly like that

new Configuration().configure( "pth/to/hibernate.cfg.xml").buildsessionfactory()

Hope this may help

@SiB pointed a link(mkyong's web site). It is explained well there.




回答5:


The file is suppose to go into the root of your /src dir, while /src is not deployed, everything in it is built/copied out to WEB-INF/classes which IS deployed. Hibernate needs the cfg.xml file in the classpath of your project to load its config settings, your WEB-INF dir is not in your classpath, so if you were to put it there, you'd be hiding it from Hibernate and it wouldn't work.

This thread would tell you how to load the hibernate.cfg.xml from any different path.




回答6:


If you are using Eclipse, go to Project -> Properties -> Java Build Path -> Source. You can add the new folder where you placed the file, or move the file to the existing folder.




回答7:


CMD+N/CTR+N while you are on Eclips, it will open a dialog-box there you have to dubbel click on the Hibernate folder. It will open a list of files with the XML extenuation. Select the cfg.xml and click on continue and when you are done! click on finish. Eclips will now add the Class name with the cfg.xml file and show it under the SRC folder. GB




回答8:


Place hibernate.cfg.xml under src/ folder or explicitly mention the path in code as:

new Configuration.configure("path of hibernate.cfg.xml").buildsessionfactory()



回答9:


try to place it into "src/main/resources" directory.



来源:https://stackoverflow.com/questions/12677783/where-to-place-hibernate-cfg-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!