aspose file tools*
The moose likes JSF and the fly likes Hibernate junit test persitence vs jsf live persistence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Hibernate junit test persitence vs jsf live persistence" Watch "Hibernate junit test persitence vs jsf live persistence" New topic
Author

Hibernate junit test persitence vs jsf live persistence

Ben Dunne
Greenhorn

Joined: Mar 24, 2013
Posts: 7
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?



Bean class


Junit test



index.xhtml


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


Here is the code from the not working jsf running



Here is an image of my folder structure showing my src and deployed resources as well
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Looking at the Javadocs for the configure() method

Use the mappings and properties specified in an application resource named hibernate.cfg.xml.

So basically, when your test runs, the file hibernate.cfg.xml is on the classpath. However, when the application is running, the file is not on the classpath.

Looking at the structure of your project, I think your resource files belong under WEB-INF/classes rather than under WEB-INF.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

James Boswell wrote:I think your resource files belong under WEB-INF/classes rather than under WEB-INF.


Yep.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ben Dunne
Greenhorn

Joined: Mar 24, 2013
Posts: 7
Excellent, that solved the problem there.
I needed to have my hibernate.cfg.xml in the web-inf/classes folder.

However I am now experiencing a similar problem
On line 13 of my hibernateUtill class

Its throwing

I had this problem before and I fixed it by editing my persistence.xml. I tried moving the persistence.xml into the web-inf/classes folder in case it was a classpath issue again. However it did not solve the problem.My folder structure is the same as the image above except for hibernate.cfg.xml is now under the classes folder.

Again its working fine in my unit test but its failing on the live run.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

You have another classpath issue. The jar for the class org.apache.derby.jdbc.ClientDriver is not on your classpath.
Ben Dunne
Greenhorn

Joined: Mar 24, 2013
Posts: 7
Yes, you were right. I had to put my Derbyclient.jar
into Web-inf/lib
I'm a little disappointed in this because I was hoping Maven would have handled this for me, I think I have alot more reading to do here.

This is all working now, but I'm a bit confused. Why was Junit able to function properly and execute the code if it wasn't on my classpath.
Is it because Junit happens runs at compile time therefore never needs to be deployed to jboss?





Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

Maven test does use a different (expanded) classpath so that the test classes may be pulled from a place where they won't clutter up the non-test environment.

Actually, JDBC drivers should not normally be included in a WAR, they should be installed in the webapp server (for Tomcat 6 and up, that's TOMCAT_HOME/lib).

Even better is to not code database driver management into the webapp at all and use a server-provided database connection pool, but time enough for that once you have the basics going.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Totally agree. If your webapp uses a particular server configured datasource, you can easily change which database it points to without having to rebuild your application.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Hibernate junit test persitence vs jsf live persistence