Hibernate JUnit persistence vs. jsf live persistence

As you can see, I have HibernateUtil, which I use to store data in my database. The same code that I use in my userBean.save method is the same code that I use in my junit test. In my junit test below it works fine. However, when I try to call the userBean.save method in my command point, it gives me an error, org.hibernate.HibernateException: /hibernate.cfg.xml was not found.

After some debugging, I can say that the error occurs in the hibernateUtil class, config.configure (). addAnnotatedClass (userBean.class);

If I change the commandbutton action for the greeting, which is the name of my welcome page, the form works fine by redirecting me to the welcome page and displaying the bean values ​​on the page. This means how confident I am that the bean was properly initialized. This is very confusing, why does it work in my junit testing 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> 

Additional Information

As I said, the problem comes from. Configuration.configure () addAnnotatedClass (userBean.class);

So, I took my debugger and copied the console outputs when we cross this piece of code. Here is the result of a Junit work 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 jsf running not working

 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 

This is actually an image of my folder structure showing my src and deployed resources enter image description here

+4
source share
1 answer

The cause of the problem is that JUnit cannot find hibernate.cfg.xml . This happens because you place all *.xml resources in the WEB-INF/classes folder. In this case, these resources are visible in the classpath of the web application, but are not displayed for JUnit tests.

As a solution, I recommend you do the following:

  • Move all *.xml resources from WEB-INF/classes to the root sources folder.
  • Make sure your compiler does not ignore *.xml files

As the end result, your *.xml resources will in any case be located in the WEB-INF/classes folder, but you can also get these resources from the usual class path.

Please note that you need to move files, otherwise there may be conflitcs of versions.

+2
source

All Articles