How to load hibernate.cfg.xml from different directory

Hibernate XML configuration file “hibernate.cfg.xml” is always put at the root of your project classpath, outside of any package. If you place this configuration file into a different directory, you may encounter the following error :


Initial SessionFactory creation failed.org.hibernate.HibernateException: 
/hibernate.cfg.xml not found

Exception in thread "main" java.lang.ExceptionInInitializerError
	at com.mkyong.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:25)
	at com.mkyong.persistence.HibernateUtil.<clinit>(HibernateUtil.java:8)
	at com.mkyong.common.App.main(App.java:11)
Caused by: 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:1405)
	at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
	at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
	at com.mkyong.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
	... 2 more

To ask Hibernate look for your “hibernate.cfg.xml” file in other directory, you can modify the default Hibernate’s SessionFactory class by passing your “hibernate.cfg.xml” file path as an argument into the configure() method:


            SessionFactory sessionFactory = new Configuration()
            .configure("/com/mkyong/persistence/hibernate.cfg.xml")
            .buildSessionFactory();
            
            return sessionFactory;

HibernateUtil.java

Full Example in HibernateUtil.java, to load “hibernate.cfg.xml” from directory “/com/mkyong/persistence/“.


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

public class HibernateUtil {

	private static final SessionFactory sessionFactory = buildSessionFactory();

	private static SessionFactory buildSessionFactory() {
		try {
			// load from different directory
			SessionFactory sessionFactory = new Configuration().configure(
					"/com/mkyong/persistence/hibernate.cfg.xml")
					.buildSessionFactory();

			return sessionFactory;

		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static void shutdown() {
		// Close caches and connection pools
		getSessionFactory().close();
	}

}

Done.

20 comments on “How to load hibernate.cfg.xml from different directory

  1. can this method be used with struts 1 ?

    Reply
  2. Can I change the configuration set in .xml on login time? For change de user and pass for another who has the real database access…

    Reply
  3. Thanks for this post!

    I have a question. Can I execute a hibernate project in another?

    When I call a method in hibernate project in another, it throw an exception:

    java.lang.NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lorg/hibernate/Session;

    Can you help me, please?

    Thank you very much!

    Reply
  4. to load hibernate.cfg.xml from a different directory use the configure(File configFile) method that takes the hibernateConfig File argument.
    (note, am using hibernate 4.3.7)

    Like this:

    —-

    String hibernatePropsFilePath = “/com/mkyong/persistence/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);

    —–

    Reply
  5. new Configuration()
    .configure(“/com/mkyong/persistence/hibernate.cfg.xml”)
    .buildSessionFactory()

    is deprecated

    Reply
  6. what if my file is not in root directory and still i am using configure(“hibernate.cfg.xml”) and have

    /com/mkyong/persistence/ in my classpath. will it than refer the file. i have problems
    refering the same.

    Reply
  7. This code not working
    Error: /com/mkyong/persistence/hibernate.cfg.xml not found

    Reply
  8. I got the error, Message table not found.
    Do I have to manually create it on my MySql database. If so, what is the command to do it?

    Thanks in advance

    Reply
  9. I have my hibernate.cfg.xml file in project root folder(src). But flowing errors are in NetBeans IDE
    INFO: configuring from resource: /hibernate.cfg.xml
    Feb 27, 2012 8:51:35 AM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: Configuration resource: /hibernate.cfg.xml
    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:1405)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
    at com.hibernate.hibr.main(hibr.java:18)

    Reply

Leave a Comment

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