hibernate - hibernate.cfg.xml放在哪里?

标签 hibernate hibernate.cfg.xml

我的项目是这样的:

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

有人可以告诉我把 hibernate.cfg.xml 放在哪里吗?

因为我收到此错误:

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

最佳答案

配置文件hibernate.cfg.xml需要位于类路径上。

这可以通过不同的方式来完成,具体取决于您的项目。

  • 对于 web-app WAR 项目(您在 Servlet 容器中运行该程序): 将其放在 WEB-INF/classes 中将起作用,因为当应用在容器中运行时,WEB-INF/classes 中的文件在类路径上可见。

    <
  • 对于 Maven 风格的项目(不在 Servlet 容器中运行程序):将其放在 /src/main/resources/ 中即可

  • 否则,请尝试在 src/ 目录中。

关于hibernate - hibernate.cfg.xml放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12677783/

相关文章:

java - Web 应用程序 - 在哪里放置 hibernate.cfg.xml 文件?

nhibernate - 配置 NHibernate hibernate.cfg.xml 文件以拥有更多连接字符串

mysql - CSV 导入数据库,在运行时改变外键属性

java - 当访问模式依赖于一对多时,如何处理 POJO 中多对多表中的附加属性?

hibernate - 在 bean 管理的事务中使用 EntityManager unwrap 方法

hibernate - 项目根文件夹中的 ConfigurationException : Could not locate cfg. xml 资源 [hibernate.cfg.xml]

java - 使用 JPA 在 Hibernate 中使用 EAGER 类型进行多次提取

java - 为什么 hibernate 创建一个没有值的查询?

java - hibernate - ServiceRegistryBuilder