Java - hibernate.cfg.xml file not found

I have my hibernate.cfg.xml located in src/main/resources/ , but I keep getting this error when starting my maven project, I also tried to put the file path in a configuration like this, but still got an error

 Configuration conf = new Configuration(); conf.configure("/src/main/resources/hibernate.cfg.xml"); 

What am I doing wrong?

And when I go to the project properties and go to Source, do I see / src / main / resources in the build path?

It also starts when I do it in eclipse, but when I export to jar it stops working, but I have it in the class path, as you can see <img src = "https: //i.stack.imgur. com / aJX5o.png "alt =" enter a description of the image here ">

thanks

Edit

Here is my directory in my eclipse project

enter image description here

And then, when I open the .jar file

enter image description here

Are you saying that .xml should be in the root directory?

+5
source share
6 answers

Your built project does not have /src/main/resources , where maven stores files before they are created. When creating a project (suppose you build .jar -artifact) maven will do the following:

  • All .java files in src/main/java will be compiled into .class files and transferred to the jar subdirectory corresponding to their package
  • All files from src/main/resources will be copied to the jar-file root

So, this file structure in your project:

 src | +-main | +-java | | | +- mypackage | | | +- MyClass.java | +-resources | +- hibernate.cfg.xml 

will appear in your .jar-file as follows:

 project.jar | +- mypackage | | | +- MyClass.class | +- hibernate.cfg.xml 

The essential point is that all files from /src/main/resources fall into the class path of your project. But since your configuration file is named hibernate.cfg.xml and located on the class path, you should be fine just by doing

 Configuration conf = new Configuration(); conf.configure(); 

Note that configure() is called the wthout argument, so it means "Look for a file named hibernate.cfg.xml in your current class path." So try removing the argument from configure -call.

+6
source

When using maven with hibernate , you only need to activate the plugin and hibernate.cfg.xml will go to the right place so that you can run the file using java -jar jarfile.jar . Stanza you need:

  <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.example.jpa.Main</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> 

To create the appropriate uberjar, enter mvn package and Bob your uncle.

+4
source

At first it’s clear that if you are using maven with hibernate, so there is no need to specify the name hibernate.cfg.xml when you call configure()

close your project, then open it again. use the code below to create a factory session you need hibernate 4 or higher

 public class HibernateUtils { private static SessionFactory sessionFactory = null; private static Logger logger = Logger.getLogger(HibernateUtils.class.getName()); public static void createSessionFactory() { Configuration configuration = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); try { sessionFactory = configuration.buildSessionFactory(builder.build()); } catch (Exception exe) { exe.printStackTrace(); } logger.debug("Session factory created"); } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void shutdown() { if (sessionFactory != null) getSessionFactory().close(); } } 

use the following main class for testing purposes

 class Main { public static void main(String[] args) { HibernateUtils.createSessionFactory(); SessionFactory sessionFactory = HibernateUtils.getSessionFactory(); // do your task HibernateUtils.shutdown(); } } 
+3
source

Instead:

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

Using:

 configuration.configure("/main/resources/hibernate.cfg.xml"); 
+3
source

Store the configuration file in the regular src directory, and not in the resource folder

Hibernate config [1]

And refer to the file as follows: Configuration configuration = new configuration (); configuration.configure ("/hibernate.cfg.xml");

// Here I looked at the configuration class (HibernateSessionFactory.java) in one package

Hope this helps!

+3
source

If you are sure that your file is in the resources folder, then check this Right click on project->Properties->Deployment Assembly and check whether the map of your file matches the deployment path or not

enter image description here

0
source

All Articles