Location of hibernate.cfg.xml in project?

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

I created a project with following structure:

\"enter

HibernateUtil:



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

    In case of a Maven Project, create a folder named resources under src/main folder and add the resources folder as a source folder in your classpath.

    You can do that by going to Configure Build Path and then clicking Add Folder to the Sources Tab.

    Then check the resources folder and click Apply.

    Then just use :

    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    
    0 讨论(0)
  • 2020-11-27 07:57

    My problem was that i had a exculding patern in the resorces folder. After removing it the

    config.configure(); 
    

    worked for me. With the structure src/java/...HibernateUtil.java and cfg file under src/resources.

    0 讨论(0)
  • 2020-11-27 08:01

    This is an reality example when customize folder structure: Folder structure, and initialize class HibernateUtil

    with:

    return new Configuration().configure("/config/hibernate.cfg.xml").buildSessionFactory();
    

    mapping:


    with customize entities mapping files:

            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="show_sql">true</property>
            <mapping class="com.vy.entities.Users"/>
            <mapping class="com.vy.entities.Post"/>
            <mapping resource="config/Users.hbm.xml"/>      
            <mapping resource="config/Post.hbm.xml"/>               
        </session-factory>
    </hibernate-configuration>
    

    (Note: Simplest way, if you follow default way, it means put all xml config files inside src folder, when build sessionFactory, only:

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

    )

    0 讨论(0)
  • 2020-11-27 08:04

    You can put the file "hibernate.cfg.xml" to the src folder (src\hibernate.cfg.xml) and then init the config as the code below:

    Configuration configuration = new Configuration();          
    sessionFactory =configuration.configure().buildSessionFactory();
    
    0 讨论(0)
提交回复
热议问题