2014-12-09 3 views
1

오늘 나는 Hibernate를 사용하기 시작했고 간단한 예제를 테스트했지만 hibernate.cfg.xml을 찾을 수 없습니다. 여기 src 폴더에있는 hibernate.cfg.xml 파일을 퍼트와 는 내용이다 :hibernate.cfg.xml을 찾을 수 없습니다 (일식)

<?xml version='1.0' encoding='utf-8'?> 

<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > 

<hibernate-configuration> 

    <session-factory> 
    <!-- Database connection settings --> 
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost:3306/test</property> 
    <property name="connection.username">test</property> 
    <property name="connection.password">test</property> 

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

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

    <!-- Enable Hibernate's automatic session context management --> 
    <property name="current_session_context_class">thread</property> 

    <!-- Disable the second-level cache --> 
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 

    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">true</property> 

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

    <mapping resource="com/mycompany/model/Product.hbm.xml"/> 

    </session-factory> 
</hibernate-configuration> 

그리고 폴더 (SRC/UTIL) util을 아래 HibernateUtil.java 파일을 퍼트 여기에 그 내용

입니다
package util; 

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

public class HibernateUtil 
{ 
    private static final SessionFactory sessionFactory = buildSessionFactory(); 

    private static SessionFactory buildSessionFactory() 
    { 
     try 
     { 
      // Create the SessionFactory from hibernate.cfg.xml 
      return new Configuration().configure("hibernate.cgf.xml").buildSessionFactory(); 
     } 
     catch (Throwable ex) { 
      // Make sure you log the exception, as it might be swallowed 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 

    public static void shutdown() { 
     // Close caches and connection pools 
     getSessionFactory().close(); 
    } 
} 

또한 빌드 경로에 Jar 파일을 추가했습니다.

내 클래스 Test.java :

import ... 


public class Test { 

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

     ProductDao pd = new ProductDao(); 

     try { 

      Product p = new Product("PC", 1000L); 

      pd.add(p); 

     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


    } 

} 

추가 방법 :

공공 무효의 추가 (제품 페이지) {

Session session = HibernateUtil.getSessionFactory().openSession(); 
Transaction tx = session.beginTransaction(); 
session.beginTransaction(); 
session.save(p); 
tx.commit(); 

} 미리

에서

감사합니다

+0

'src-> util' 폴더에'hibernate.cfg.xml'을 넣고 다시 시도하십시오. –

+0

Gradle, Ant, Maven 같은 도구를 사용하고 있습니까 – foxt7ot

+0

그래도 같은 문제가 발생했습니다. – Simo03

답변

0

(받는다는없이) 간단한 이클립스 프로젝트를 사용하는 경우 받는다는을 사용하는 경우 enter image description here

은 또한 당신은 파일의 이름을 맞춤법이 틀린 (안 SRC에서) 프로젝트의 루트 디렉토리에 넣어

/src/main/resources 

에 넣어 시도 소스

return new Configuration().configure("hibernate.cgf.xml").buildSessionFactory(); 

에가 있어야합니다

hibernate.cfg.xml 
+0

그게 내가 한 일이야. src 폴더에있다. – Simo03

+0

당신은 maven을 사용하고있다. – outdev

+0

아니오 나는 Maven을 사용하지 않는다. – Simo03

0

Maven 프로젝트의 경우/src/main/resources의 cfg.xml 파일을 eclips에 추가하면

관련 문제