Location of hibernate.cfg.xml in project?

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

I created a project with following structure:

\"enter

HibernateUtil:



        
相关标签:
10条回答
  • 2020-11-27 07:42

    Somehow placing under "src" folder didn't work for me.

    Instead placing cfg.xml as below:

    [Project Folder]\src\main\resources\hibernate.cfg.xml
    

    worked. Using this code

    new Configuration().configure().buildSessionFactory().openSession();
    

    in a file under

        [Project Folder]/src/main/java/com/abc/xyz/filename.java
    

    In addition have this piece of code in hibernate.cfg.xml

    <mapping resource="hibernate/Address.hbm.xml" />
    <mapping resource="hibernate/Person.hbm.xml" />
    

    Placed the above hbm.xml files under:

    EDIT:

    [Project Folder]/src/main/resources/hibernate/Address.hbm.xml
    [Project Folder]/src/main/resources/hibernate/Person.hbm.xml
    

    Above structure worked.

    0 讨论(0)
  • 2020-11-27 07:42

    try below code it will solve your problem.

    Configuration  configuration = new Configuration().configure("/logic/hibernate.cfg.xml");
    
    0 讨论(0)
  • 2020-11-27 07:44

    Give the path relative to your project.

    Create a folder called resources in your src and put your config file there.

       configuration.configure("/resources/hibernate.cfg.xml");
    

    And If you check your code

    Configuration  configuration = new Configuration().configure( "C:\\Users\\Nikolay_Tkachev\\workspace\\hiberTest\\src\\logic\\hibernate.cfg.xml");
    return new Configuration().configure().buildSessionFactory();
    

    In two lines you are creating two configuration objects.

    That should work(haven't tested) if you write,

    Configuration  configuration = new Configuration().configure( "C:\\Users\\Nikolay_Tkachev\\workspace\\hiberTest\\src\\logic\\hibernate.cfg.xml");
    return  configuration.buildSessionFactory();
    

    But It fails after you deploy on the server,Since you are using system path than project relative path.

    0 讨论(0)
  • 2020-11-27 07:46

    Another reason why this exception occurs is if you call the configure method twice on a Configuration or AnnotatedConfiguration object like this -

    AnnotationConfiguration config = new AnnotationConfiguration();
    config.addAnnotatedClass(MyClass.class);
    //Use this if config files are in src folder
    config.configure();
    //Use this if config files are in a subfolder of src, such as "resources"
    config.configure("/resources/hibernate.cfg.xml");
    

    Btw, this project structure is inside eclipse.

    0 讨论(0)
  • 2020-11-27 07:47

    For anyone interested: if you are using Intellj, just simply put hibernate.cfg.xml under src/main/resources.

    0 讨论(0)
  • 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;
    }
    }  
    
    0 讨论(0)
提交回复
热议问题