I have my hibernate.cfg.xml
located at src/main/resources/
but I keep getting this error when I run my maven project, I have also tried putting the path to the file in the configuration like this but still got the error
Configuration conf = new Configuration();
conf.configure("/src/main/resources/hibernate.cfg.xml");
What am I doing wrong?
And when I go to the properties of the project and go to Source I see /src/main/resources in the build path?
It also runs when I make it in eclipse, but when I export to jar it stops working but I have it in class path as you can see
Thanks
Edit
Here is my directory in my eclipse project
And then here is it when I open the .jar file
Are you saying the .xml should be in the root directory?
The hibernate. cfg. xml file shoul be in root directory of the classpath of your project. If you using Maven then make sure it should be like src > resources > hibernate.
Right click on the desired module -> Add -> Hibernate. Select the newly created Hibernate configuration option, and click the (+) sign in the right pane to create hibernate. cfg. xml file.
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.
There is no /src/main/resources
in your built project, that is just where maven keeps the files before building. While building the project (I assume you are building a .jar
-artifact) maven will do the following:
.java
-files in src/main/java
will be compiled to .class-files and will be moved to the subdirectory of the jar
corresponding to their packagesrc/main/resources
will be copied to the root of the jar-file
So this file-structure in your project:
src
|
+-main
|
+-java
| |
| +- mypackage
| |
| +- MyClass.java
|
+-resources
|
+- hibernate.cfg.xml
will end up in your .jar-file
like this:
project.jar
|
+- mypackage
| |
| +- MyClass.class
|
+- hibernate.cfg.xml
The essential point is: All files from /src/main/resources
will end up in the classpath of your built project. But since your configuration-file is named hibernate.cfg.xml
and is located on the classpath you should be OK just doing
Configuration conf = new Configuration();
conf.configure();
Note that configure()
is called wthout an argument, so that means "Look for a file called hibernate.cfg.xml
on your current classpath". So try removing the argument from your configure
-call.
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