My project is like this:
/src/main/java
-thegamers
-app.java
-hibernateutil.java
can someone tell me where to put the hibernate.cfg.xml?
because I'm getting this error:
Initial SessionFactory creation failed.org.hibernate.HibernateException: hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
at thegamers.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
at thegamers.HibernateUtil.<clinit>(HibernateUtil.java:8)
at thegamers.App.main(App.java:15)
Caused by: org.hibernate.HibernateException: hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2149)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2130)
at thegamers.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 2 more
cfg. xml to specify required Hibernate properties in my examples. Most of the properties take their default values and it is not required to specify them in the property file unless it is really required. This file is kept in the root directory of your application's classpath.
These configurations contain the mapping information that provides different functionalities to Java classes. Generally, we provide database related mappings in the configuration file. Hibernate facilitates to provide the configurations either in an XML file (like hibernate. cfg.
Basically you are setting all the required properties via your properties object so there is no real need to tell Hibernate to look for a hibernate. cfg. xml file which is exactly what the configure() method does.
The config file hibernate.cfg.xml
needs to be on the classpath
.
This can be accomplished in different ways, depending on your project.
For a web-app WAR project (you are running the program in a Servlet container):
placing it in WEB-INF/classes
will work as files in WEB-INF/classes
are visible on the classpath when app is running in container.
For a Maven-style project (not running the program in a Servlet container): placing it in /src/main/resources/
will work
For otherwise, try in the src/
directory.
I'm using maven, and it didn't work for me until I put hibernate.cfg.xml in src/main/resources
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With