Location of hibernate.cfg.xml in project?
Asked Answered
J

11

25

I created a project with following structure:

enter image description here

HibernateUtil:

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration  configuration = new Configuration().configure( "C:\\Users\\Nikolay_Tkachev\\workspace\\hiberTest\\src\\logic\\hibernate.cfg.xml");
            return new Configuration().configure().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();
    }

}

at line

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

I have error

Initial SessionFactory creation failed.org.hibernate.HibernateException: C:\Users\Nikolay_Tkachev\workspace\hiberTest\src\logic\hibernate.cfg.xml not found Exception in thread "main" java.lang.ExceptionInInitializerError at logic.HibernateUtil.buildSessionFactory(HibernateUtil.java:19) at logic.HibernateUtil.(HibernateUtil.java:9) at logic.Main.main(Main.java:12) Caused by: org.hibernate.HibernateException: C:\Users\Nikolay_Tkachev\workspace\hiberTest\src\logic\hibernate.cfg.xml not found at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173) at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1947) at org.hibernate.cfg.Configuration.configure(Configuration.java:1928) at logic.HibernateUtil.buildSessionFactory(HibernateUtil.java:14) ... 2 more

What is the reason for the error and how do I fix it?

Jarvis answered 11/9, 2013 at 8:48 Comment(1)
Similar: Where to place hibernate.cfg.xml?Resilient
M
35

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.

Markitamarkka answered 11/9, 2013 at 8:54 Comment(3)
No need.But a good practice to put resources of project in resources folder :)Markitamarkka
@sᴜʀᴇsʜᴀᴛᴛᴀ - In eclipse, my simple code was working fine when the hibernate.cfg.xml file was inside the src folder. When I created a resources folder inside src and put the file there, my code threw an exception saying that it could not find the file anymore. How do I fix this problem ?Beaton
config.configure("/resources/hibernate.cfg.xml"); This does not work for me. I still get the error.Beaton
R
13

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.

Reverberator answered 29/12, 2013 at 4:16 Comment(0)
W
4

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();
Wachtel answered 11/9, 2013 at 8:55 Comment(0)
I
4

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

enter image description here

with:

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

mapping: enter image description here


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();

)

Interloper answered 9/1, 2016 at 10:20 Comment(0)
P
3

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

Palestra answered 26/6, 2018 at 11:53 Comment(0)
R
2

try below code it will solve your problem.

Configuration  configuration = new Configuration().configure("/logic/hibernate.cfg.xml");
Rousing answered 13/9, 2013 at 9:8 Comment(0)
A
2

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.

Apodaca answered 23/6, 2016 at 9:9 Comment(0)
B
1

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.

Beaton answered 24/4, 2014 at 21:53 Comment(0)
N
0

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;
}
}  
Nitriding answered 11/1, 2017 at 0:8 Comment(0)
R
0

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();
Regina answered 24/4, 2017 at 8:31 Comment(0)
S
0

Specially for VS Code

Other folks have already answered this question but I would like to add one common thing. If the other solutions are not working for you by moving the hibernate.cfg.xml file in your IDE, you can reveal you explorer and manually drop this file in the correct place.

Sidky answered 2/7 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.