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.

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
newest oldest most voted
Abraham
Guest
Abraham

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

Mufim
Guest
Mufim

Very Good ANSWER

Vishal Panchal
Guest
Vishal Panchal

It works like charm..
Thanks..!

Carlos Gonzaga
Guest
Carlos Gonzaga

\o/ wowwwwww thanks!!

Walker Rowe
Guest
Walker Rowe

Mykong thanks again. Your web site is filled with useful information.

B?ch
Guest
B?ch

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

SyedThayeefAhmed
Guest
SyedThayeefAhmed

set the path of the configuration file

Erez
Guest
Erez

Beautiful! Thanks!

Nishant Mishra
Guest
Nishant Mishra

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.

Jeff
Guest
Jeff

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

is deprecated

Davies Arthur M.
Guest
Davies Arthur M.

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);

—–

The Hien Pham
Guest
The Hien Pham

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!

rahul jain
Guest
rahul jain

assxs

Luciano Seibel
Guest
Luciano Seibel

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…

PhanVu HoaiNam
Guest
PhanVu HoaiNam

Thank you. It’s helpful.

trackback
Hibernate Tutorials | Tutorials

[…] How to load hibernate.cfg.xml from different directory By default, Hibernate looking the hibernate.cfg.xml at the project class path, here’s a guide to load it from specified folder. […]

emogirl
Guest
emogirl

thank you so much im dying fuck everything

Rakesh
Guest
Rakesh

which component load the hibenate.cfg.xml file in hibernate?

Shams
Guest
Shams

Thanks for this post.

Really helpful
Thanks again

Mahesh
Guest
Mahesh

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)

rakesh b.k
Guest
rakesh b.k

can this method be used with struts 1 ?