【问题标题】:Hibernate ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml] IntelliJHibernate ConfigurationException:找不到 cfg.xml 资源 [hibernate.cfg.xml] IntelliJ
【发布时间】:2016-06-11 02:07:20
【问题描述】:

我在 IntelliJ IDE 中的 hibernate.cfg.xml 有问题。

这是我的休眠配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
        "-//Hibernate/Hibernate Configuration DTD//EN" 
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration>
    <session-factory>
       <!--  Database connection settings -->
       <property name="connection.driver:class">org.postgresql.Driver</property>
       <property name="connection.url">jdbc:postgresql://localhost/HIndex</property>
       <property name="hibernate.connection.username">index_user</property>
       <property name="hibernate.connection.password">password</property>

       <!--  JDBC connection pool (use the built-in) -->
       <property name="connection.pool_size">1</property>

       <!--  SQL Dialect -->
       <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect</property>

       <!-- Assume test is the database name -->
       <property name="show_sql">true</property>

       <!--  Drop and re-create the database schema on startup -->
       <property name="hbm2ddl.auto">create</property>

       <!--  Names the annotated entity class -->
       <mapping class="HIndexSaar.HIndex.Person"/>
       <mapping class="HIndexSaar.HIndex.University"/>
       <mapping class="HIndexSaar.HIndex.Publication"/>
    </session-factory>
</hibernate-configuration>

这是我的应用程序类:

package HIndexSaar.HIndex;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateManager {

    private static SessionFactory factory;

    public HibernateManager(){
        //
        //* Setup the configuration.
        //
        Configuration config = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Person.class)               .addAnnotatedClass(University.class).addAnnotatedClass(Publication.class);
        factory = config.buildSessionFactory();
    }

运行以下代码时出现错误:

package HIndexSaar.HIndex;

public class AppHibernate {

    public static void main(String[] args){
        HibernateManager mng = new HibernateManager();
        [...]
    }
}

因此,在 HibernateManager 中构建 SessionFactory 时似乎有些问题。我收到错误

找不到 cfg.xml 资源

以及以下堆栈跟踪:

Feb 27, 2016 10:26:03 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.7.Final}
Feb 27, 2016 10:26:03 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 27, 2016 10:26:03 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Exception in thread "main"        org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:259)
at org.hibernate.cfg.Configuration.configure(Configuration.java:245)
at HIndexSaar.HIndex.HibernateManager.<init>(HibernateManager.java:18)
at HIndexSaar.HIndex.AppHibernate.main(AppHibernate.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

我已经将 hibernate.cfg.xml 文件放在 /src/main/java/resources 下,但显然还是有问题。 有人知道我犯的错误是什么吗?令人惊讶的是,相同的代码在 Eclipse 中运行没有错误,那么 IntelliJ 的问题可能是什么?我使用相同的 SDK/JDK,即 jdk 1.7。

【问题讨论】:

  • 将你的 hibernate.cfg.xml 文件放在 src 文件夹下然后尝试执行你的程序
  • 刚刚试过,但没有解决问题:(
  • 如果您使用的是 intellij,请尝试创建具有 hibernate 框架支持的新模块,然后粘贴相同的代码并执行它。
  • 这可能是eclipse和intellij的路径差异
  • 好的,我会试试这个,希望它会工作。当前模块已经启用了 Hibernate 框架支持:(

标签: java xml hibernate intellij-idea hibernate.cfg.xml


【解决方案1】:

如果您使用的是 intellij,请尝试创建具有 hibernate 框架支持的新模块,然后粘贴相同的代码并执行它。 这可能是 clipse 和 intellij 的路径差异

【讨论】:

  • 我也有同样的问题。如何创建具有 Hibernate 支持的新模块?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-19
  • 2016-06-14
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
相关资源
最近更新 更多