Q:

Hibernate JUnit test persistence vs. jsf live persistence

As you can see I have a HibernateUtil that I use to persist data to my database. The same code I use in my userBean.save method is the same code I use in my junit test. In my junit test below it works perfectly. However when I try to call the userBean.save method in my commandbutton it gives me an error, org.hibernate.HibernateException: /hibernate.cfg.xml not found.
After some debugging I can tell you the error happens in hibernateUtil class, configuration.configure().addAnnotatedClass(userBean.class);
If I change the commandbutton action to welcome, which is the name of my welcome page, the form works fine by redirecting me to the welcome page and it outputs the bean values on the page. This means , as far as I'm concerned ,that the bean is been initialized correctly.
This is very confusing, why is it working in my junit test and not on my jsf page?
public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static ServiceRegistry serviceRegistry;

private static SessionFactory buildSessionFactory() {
    try {

        Configuration configuration = new Configuration();
        configuration.configure().addAnnotatedClass(userBean.class);
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        // Create the SessionFactory from hibernate.cfg.xml
        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;
}

}
package Beans;
import javax.faces.bean.ManagedBean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.*;
import controller.HibernateUtil;
import javax.persistence.Table;

@ManagedBean
@Entity
@Table(name = "userBean")
public class userBean {

@Id @GeneratedValue
private int id;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String name;
private int age=0;

public String getName() {
return name;
}
public void setName(String n) {
this.name = n;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

//Methods
public void save(){
//HibernateUtil.getSessionFactory().close();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

userBean u=new userBean();
u.setName("saveName");
u.setAge(20);

session.save(u);

session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();

}
}

Junittest.java
public void test2(){

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        userBean u=new userBean();
        u.setName("tommy");
        u.setAge(20);

        session.save(u);

        session.getTransaction().commit();
        HibernateUtil.getSessionFactory().close();
}    

index.xhtml 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>JSF 2: Blank Starting-Point Project</title>
<link href="./css/styles.css" 
  rel="stylesheet" type="text/css"/> 
</h:head>
<h:body>
<h:form>
<h:inputText id="it" value="#{userBean.name}"/>
<h:commandButton id="btn" value="Submit" action="#{userBean.save}"/>
</h:form>
</h:body>
</html>

More information
As I said the problem is comming from 
configuration.configure().addAnnotatedClass(userBean.class);
So I took my debugger through and copied the console outputs from when we cross this piece of code.
Here is the output from the working Junit test
000021: Bytecode provider name : javassist
Mar 24, 2013 2:10:21 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Mar 24, 2013 2:10:21 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Mar 24, 2013 2:10:21 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace        http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/   instead.   Refer to Hibernate 3.6 Migration Guide!
Mar 24, 2013 2:10:21 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null

Here is the code from the not working jsf running
14:23:03,988 INFO  [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-2) HHH000043: Configuring from resource: /hibernate.cfg.xml
14:23:03,998 INFO  [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-2) HHH000040: Configuration resource: /hibernate.cfg.xml
14:23:04,057 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) Initial SessionFactory   creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found

In fact here is an image of my folder structure showing my src and deployed resources as well

A:

The cause of the problem is that JUnit can't find hibernate.cfg.xml. This happens, because you place all *.xml resources into WEB-INF/classes folder. In this case these resources are visible in classpath of web-application, but not visible for JUnit tests.
As a solution I recommend you doing as follows:

Move all *.xml resources from WEB-INF/classes into root of sources folder.
Make sure, that you compiler doesn't ignore *.xml files

As the final result, your *.xml resources anyway will be in WEB-INF/classes folder, but you can also get those resources from ordinary classpath.
Please note, that you need to move files, otherwise there might be conflitcs of versions.

