It is one of the very common exception thrown when you start setting up the environment for hibernate application. hibernate.cfg.xml is the default configuration file for defining the hibernate database configurations and mappings. This file has to be copied in the classpath directory. In this scenario, you always keep this file under the “src” folder of your source. If you move to the different folder, you have to specifiy the path while loading the file. The following is the exception thrown when the file is not found.
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173) at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2093) at org.hibernate.cfg.Configuration.configure(Configuration.java:2074) at org.hibernate.cfg.Configuration.configure(Configuration.java:2054) at javabeat.net.hibernate.HibernateUtil.main(HibernateUtil.java:12)
How To Load hibernate.cfg.xml From Different Path?
There are scenarios when you want to load this configuration file from the different path. You can pass the complete path of the directory to the configuration object as follows.
SessionFactory sessionFactory = new Configuration() .configure("/javabeat/net/hibernate/hibernate.cfg.xml") .buildSessionFactory();
Hibernate Configuration Loading From Different Path
Lets see how to load hibernate configuration using this simple code.
import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateLoading { private static void main (String args[]{ try { SessionFactory sessionFactory = new Configuration().configure( "/javabeat/net/hibernate/hibernate.cfg.xml") .buildSessionFactory(); } catch (Throwable t) { System.err.println("SessionFactory creation failed." + t); } } }