• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Hibernate Configuration Resource Loading Problems

December 24, 2013 //  by Krishna Srinivasan//  Leave a Comment

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.

[code] 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)
[/code]

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.

[code lang=”java”] SessionFactory sessionFactory = new Configuration()
.configure("/javabeat/net/hibernate/hibernate.cfg.xml")
.buildSessionFactory();
[/code]

Hibernate Configuration Loading From Different Path

Lets see how to load hibernate configuration using this simple code.

[code lang=”java”] 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);
}
}

}
[/code]

Category: HibernateTag: Hibernate 4 Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Hibernate ORM 4.3.0 Released
Next Post: Hibernate Dependency Library Exception (JBoss Logging) : org/jboss/logging/BasicLogger »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact