5

Hibernate と 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/

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>

その例外の原因は何ですか?

4

6 に答える 6

16

@JBNizet が言ったように、あなたhibernate.cfg.xmlは src/main/resources にいるべきです。src では、実行時にクラスパスに追加されません。

src/main/resourcesプロジェクトを Eclipse 内で実行している場合は、ビルド パス構成のプロジェクト設定で、がクラス パスから除外されておらず、実際にソース フォルダーであることを確認することを忘れないでください。

于 2013-03-06T22:27:26.673 に答える
8

ファイルはランタイム クラスパスにある必要があります。Maventarget/classesは、 の下にあるリソースをフォルダーにコピーしますsrc/main/resources。したがって、構成ファイルはそこにあるはずです。

とはいえ、ファイルをロードするコードは表示されないため、他の問題が発生する可能性があります。

于 2013-03-06T21:42:51.940 に答える
1

hibernateConfig File 引数を取る configure(File configFile) メソッドを使用して、別のディレクトリ (必ずしもクラスパスではない) から hibernate.cfg.xml をロードできます。(注、休止状態 4.3.7 を使用しています)

このような:


String hibernatePropsFilePath = "/etc/configs/hibernate.cfg.xml";
File hibernatePropsFile = new File(hibernatePropsFilePath);

Configuration configuration = new Configuration(); 
configuration.configure(hibernatePropsFile);

StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());

ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

于 2014-12-27T10:14:09.300 に答える
0

Netbeansを使用している場合は、hibernate.cfg.xmlファイルを"Build > classes >"ディレクトリに配置すると機能します。

于 2020-04-01T14:28:34.053 に答える
0
For the People who are facing this issue while deploying on linux machine

You need to copy hibernate.cfg.xml to classes directory of your war project.

In Eclipse/Maven Project: (During Development)

You need to copy hibernate.cfg.xml to src directory of your Eclipse/Maven project.

于 2015-08-17T02:51:04.280 に答える