Location of hibernate.cfg.xml in project?

前端 未结 10 1808
盖世英雄少女心
盖世英雄少女心 2020-11-27 07:28

I created a project with following structure:

\"enter

HibernateUtil:



        
10条回答
  •  旧时难觅i
    2020-11-27 07:51

    Using configure() method two times is responsible the problem for me. Instead of using like this :

        Configuration configuration = new Configuration().configure();
        configuration.configure("/main/resources/hibernate.cfg.xml");
    

    Now, I am using like this, problem does not exist anymore.

        Configuration configuration = new Configuration();
        configuration.configure("/main/resources/hibernate.cfg.xml");
    

    P.S: My hibernate.cfg.xml file is located at "src/main/resources/hibernate.cfg.xml",too. The code belove works for me. at hibernate-5

    public class HibernateUtil {
    
     private static SessionFactory sessionFactory ;
    
    
     static {
         try{
        Configuration configuration = new Configuration();
        configuration.configure("/main/resources/hibernate.cfg.xml");
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        sessionFactory = configuration.buildSessionFactory(builder.build());
     }
         catch(Exception e){
             e.printStackTrace();
         }
         }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    }  
    

提交回复
热议问题