May 8 2015

org.hibernate.hibernateexception: /hibernate.cfg.xml not found

org.hibernate.hibernateexception /hibernate.cfg.xml not found

This exception will be thrown when hibernate is not able to locate hibernate.cfg.xml file in the root directory of your application’s classpath.

Example 

In the below example, if the hibernate.cfg.xml is not available in your src folder org.hibernate.hibernateexception will be thrown

HibernateUtility.java

package com.answersz.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtility{

public static void main(String[] args) {

SessionFactory factory = new Configuration().configure()
.buildSessionFactory();

Session session = factory.openSession();

// Hibernate operations

session.close();
factory.close();

}

}

Output

Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1329)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1351)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1338)
at com.answersz.dao.HibernateUtil.main(HibernateUtil.java:11)

If you want to place the xml file in different directory you need to pass the file path as argument to configure method.

Example: If  hibernate.cfg.xml file  is placed in src -> resource folder you have to pass ‘/resource/hibernate.cfg.xml’ to configure method.

HibernateUtility.java

package com.answersz.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtility{

 public static void main(String[] args) {

 SessionFactory factory = new Configuration().configure(
 "/resource/hibernate.cfg.xml").buildSessionFactory();

 Session session = factory.openSession();

 // Hibernate operations

 session.close();
 factory.close();

 }

}


Copyright © 2014. All rights reserved.

Posted May 8, 2015 by answersz in category Hibernate

Leave a Comment

Your email address will not be published. Required fields are marked *

*