无法加载hibernate.cfg.xml

时间:2015-05-28 13:56:57

标签: java hibernate

我正在通过一个hibernate教程并遇到了这个问题。这是我的代码

public class HibernateTest {
    public static void main(String[] args) {
        System.out.println(args[0]);
        User user = new User();
        user.setId(1);
        user.setName("John Smith");
        SessionFactory sessionFactory = createSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
    }

    public static SessionFactory createSessionFactory() {
        Configuration configuration = new Configuration();
        configuration.configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                configuration.getProperties()).build();
        return configuration.buildSessionFactory(serviceRegistry);
    }
}

这是我项目的结构。

enter image description here

但我得到了一个例外,说

Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2095)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2076)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2056)
    at HibernateTest.createSessionFactory(HibernateTest.java:25)
    at HibernateTest.main(HibernateTest.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

3 个答案:

答案 0 :(得分:2)

将文件移动到src/main/resources,在构建时将其复制到类路径中

答案 1 :(得分:1)

hibernate.cfg.xml必须在类路径上。这样的文件被认为是一种资源,应该放在src/main/resources下,它由构建工具放在classes文件夹中。

答案 2 :(得分:1)

错误清楚substrb(..., 1, 4000)文件不在CLASSPATH中。

DECLARE
    TYPE TMP_REC_TYPE IS RECORD (
      ID STREAM_TEXT.ID%TYPE,
      POST_TEXT ACTIVITY_EVENT.POST_TEXT%TYPE,
      TEXT STREAM_TEXT.TEXT%TYPE,
      DT STREAM_TEXT.DT%TYPE,
      STATE STREAM_TEXT.STATE%TYPE,
      STREAM_ID STREAM_TEXT.STREAM_ID%TYPE
    );
    TYPE TMP_REC_TAB_TYPE IS TABLE OF TMP_REC_TYPE;
    TMP_REC_TAB TMP_REC_TAB_TYPE;
    RC SYS_REFCURSOR;
BEGIN
    OPEN RC FOR
        SELECT SEQ$STREAM_TEXT.NEXTVAL AS ID,
               T.POST_TEXT,
               NULL AS TEXT,
               T.DT AS DT,
               'READY' AS STATE,
               S.ID AS STREAM_ID
          FROM ACTIVITY_EVENT T
          LEFT JOIN STREAM S
            ON S.OLD_ID = T.ID
         WHERE T.POST_TEXT IS NOT NULL;
    LOOP
        FETCH RC BULK COLLECT INTO TMP_REC_TAB LIMIT 100;
        FOR I IN 1..TMP_REC_TAB.COUNT LOOP -- populate text field
            TMP_REC_TAB(I).TEXT := SUBSTRB(
              DBMS_LOB.SUBSTR(TMP_REC_TAB(I).POST_TEXT, 4000, 1), 1, 4000);
        END LOOP;
        FORALL I IN 1..TMP_REC_TAB.COUNT -- bulk insert
            INSERT INTO STREAM_TEXT (ID, TEXT, DT, STATE, STREAM_ID)
            VALUES (TMP_REC_TAB(I).ID, TMP_REC_TAB(I).TEXT, TMP_REC_TAB(I).DT,
                TMP_REC_TAB(I).STATE, TMP_REC_TAB(I).STREAM_ID);
        EXIT WHEN RC%NOTFOUND;
    END LOOP;
END;
/

要将文件保存在CLASSPATH中,请将其移动到文件夹/ src / main / resources(或)将文件的当前位置放在CLASSPATH中。