Hibernate hibernate.cfg.xml exception not found

I am trying to start a project with Hibernate and Maven.

I got this exception:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Exception in thread "main" 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:2176) at org.hibernate.cfg.Configuration.configure(Configuration.java:2157) at org.hibernate.cfg.Configuration.configure(Configuration.java:2137) at FirstHibernate.com.myhib.CRUDS.CrudsOps.main(CrudsOps.java:15) 

Here is a screenshot of my project structure (hibernate.cfg.xml is in src /): http://imageshack.us/photo/my-images/692/screenshotxba.jpg/

CrudsOps.java

 package FirstHibernate.com.myhib.CRUDS; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class CrudsOps { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub SessionFactory sf = new Configuration().configure().buildSessionFactory(); System.out.println("Cfg and hbm files loaded succesfully"); Session session = sf.openSession(); session.beginTransaction(); System.out.println("Transaction began"); } } 

pom.xml

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>FirstHibernate</groupId> <artifactId>com.myhib</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>com.myhib Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.8.Final</version> </dependency> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.1-901.jdbc4</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency> </dependencies> <build> <finalName>com.myhib</finalName> <resources> <resource> <filtering>true</filtering> <directory>src/main/resources</directory> </resource> </resources> </build> </project> 

What could be the source of this exception?

+4
source share
5 answers

As @JBNizet said, your hibernate.cfg.xml should be in src / main / resources. In src, it will not be added to your classpath for execution.

If you run the project in Eclipse, be sure to check in the project settings in the build path configuration that src/main/resources not excluded from your class path and is really the source folder.

+15
source

The file must be in the path of the runtime. Maven copies the resources under src/main/resources to the target/classes folder. Therefore, your configuration file should be there.

However, you are not showing the code that downloads the file, so other problems may occur.

+8
source

You can load hibernate.cfg.xml from another directory (not necessarily the class path) using the configure (File configFile) method, which takes an argument to the hibernateConfig file. (note I am using sleep mode 4.3.7)

Like this:


 String hibernatePropsFilePath = "/etc/configs/hibernate.cfg.xml"; File hibernatePropsFile = new File(hibernatePropsFilePath); Configuration configuration = new Configuration(); configuration.configure(hibernatePropsFile); StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); ServiceRegistry serviceRegistry = serviceRegistryBuilder.build(); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

+1
source
 For the People who are facing this issue while deploying on linux machine 

You need to copy hibernate.cfg.xml to the class directory of your military project.

 In Eclipse/Maven Project: (During Development) 

You need to copy hibernate.cfg.xml to the src directory of your Eclipse / Maven project.

0
source

If you work in Intellij Idea, then create a folder called "resources" in the src \ main \ java section. Open the "Module module" parameter of your project, select "Modules" on the left and on the "Sources" tab, select the newly created "Resources" folder and mark it as "Resources". enter image description here

then it should work

 Configuration con = new Configuration().configure("hibernate.cfg.xml"); 
0
source

All Articles