Hibernate异常hibernate.cfg.xml未找到 [英] Hibernate exception hibernate.cfg.xml not found

查看:109
本文介绍了Hibernate异常hibernate.cfg.xml未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图用Hibernate和Maven启动项目。

I'm trying to start project with Hibernate and Maven.

我有这样的例外:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" 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:2176)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2157)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
    at FirstHibernate.com.myhib.CRUDS.CrudsOps.main(CrudsOps.java:15)

这是我的项目结构的截图(hibernate.cfg.xml在src /):
http://imageshack.us/photo/my-images/692/screenshotxba.jpg/

Here is the screenshot of my project structure, (hibernate.cfg.xml is in src/): http://imageshack.us/photo/my-images/692/screenshotxba.jpg/

CrudsOps.java

package FirstHibernate.com.myhib.CRUDS;

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

public class CrudsOps {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        System.out.println("Cfg and hbm files loaded succesfully");
        Session session = sf.openSession();
        session.beginTransaction();
        System.out.println("Transaction began");

    }

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>FirstHibernate</groupId>
  <artifactId>com.myhib</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>com.myhib Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.8.Final</version>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901.jdbc4</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>com.myhib</finalName>
    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
  </build>
</project>

可能是该异常的来源?

What could be a source of that exception?

推荐答案

正如@JBNizet所说,你的 hibernate.cfg.xml 应该在src / main / resources中。在src中,不会将其添加到运行时的类路径。

As @JBNizet said, your hibernate.cfg.xml should be in src/main/resources. In src, it won't be added to your classpath for runtime.

如果您在Eclipse中运行项目,请勿忘记构建中的项目首选项路径配置,以检查 src / main / resources 是否从您的类路径中排除,确实是源文件夹。

If you are running your project within Eclipse, don't forget in the project preferences in the build path configuration to check that the src/main/resources is not excluded from your class path and is indeed a source folder.

这篇关于Hibernate异常hibernate.cfg.xml未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆