何处放置hibernate.cfg.xml? [英] Where to place hibernate.cfg.xml?
问题描述
$ p $
/ src / main / java
-thegamers
- app.java
-hibernateutil.java
有人可以告诉我在何处放置hibernate。因为我得到这个错误:
初始化SessionFactory创建failed.org.hibernate.HibernateException:未找到hibernate.cfg.xml
线程main中的异常java.lang.ExceptionInInitializerError $ thegamers.HibernateUtil.buildSessionFactory中的$ b $(HibernateUtil.java:17 )
在thegamers.HibernateUtil。< clinit>(HibernateUtil.java:8)
在thegamers.App.main(App.java:15)
引起:org.hibernate.HibernateException :hibernate.cfg.xml未找到
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2149)在org.hibernate.cfg.Configuration.configure中
(Configuration.java:2130)
at thegamers.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 2 more
其他任何时候,您都需要在classpath中显示一个文件。 配置文件 hibernate.cfg.xml
需要位于 classpath
中。
这可以通过不同的方式完成,具体取决于您的项目。 对于 web-app WAR 项目(您正在Servlet容器中运行程序):
将它放在 WEB-INF / classes $ c $当应用程序在容器中运行时,c>将作为
WEB-INF / classes
中的文件在类路径中可见。
<对于 Maven 样式的项目(不在Servlet容器中运行该程序):将其放置在 / src / main / resources / $ c $中否则,请在
src /
目录中尝试。
/ li>
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
It is the same for any other time you need a file visible on the classpath. 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 inWEB-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 workFor otherwise, try in the
src/
directory.
这篇关于何处放置hibernate.cfg.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!