jonney jonney - 10 months ago 48
Java Question

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

I'm trying to use hibernate with spring 3 mvc but at the moment I get this exception thrown. I think I need to define my

somewhere, but not sure where?

I basically followed this example here And in particularly saw this line of code there that suppose to "magically" find my hibernate.cfg file using this:

return new Configuration().configure().buildSessionFactory();

I'm guessing that is not correct? i currently have my hibernate.cfg file inside

below is my cfg file:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/racingleague</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="hibernate.format_sql">true</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="">update</property>
<!--property name="">update</property-->
<mapping resource="com/jr/model/hibernateMappings/user.hbm.xml"/>

my hibernate utils class:

package com.jr.utils;

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

public class HibernateUtils {

private static final SessionFactory sessionFactory = buildSessionFactory();

public static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
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);


which gets called bu this abstract class:

package com.jr.db;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import com.jr.utils.HibernateUtils;

public abstract class DbWrapper<T> {

private static SessionFactory sessionFactory = null;
private static Session session;

public DbWrapper() {

private void setSessionFactory() {
sessionFactory = HibernateUtils.buildSessionFactory();
session = sessionFactory.getCurrentSession();

public boolean addNewItem(T dbItem) {

try {
} catch (Exception e) {
System.err.println("error exception when adding new item to table"
+ e);
} finally {


return false;


public abstract boolean removeItem(String uid);

public abstract boolean modifyItem(String uid, T item);


And here is the controller that originally does some hibernate stuff:

private Logger logger = Logger.getLogger(UserController.class);

private UserDb userDb;

@RequestMapping(value = "/user/registerSuccess", method = RequestMethod.POST)
public String submitRegisterForm(@Valid User user, BindingResult result) {

// validate the data recieved from user"validate the data recieved from user");
if (result.hasErrors()) {"form has "+result.getErrorCount()+" errors");

return "account/createForm";
} else{
// if everthings ok, add user details to database"if everthings ok, add user details to database");

userDb = new UserDb();


// display success and auto log the user to the system.
return "account/main";


Cheers in advance. I also have all my table hibvernate xml mappings in the same location as my hibernate.cfg.xml file

Answer Source

Instead of placing hibernate.cfg.xml file under src/com/jr/hibernate/ directory, place it under src directory. It will then automatically appear in WEB-INF/classes directory, as mentioned by the folks here.