• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate junit test persitence vs jsf live persistence

 
Ben Dunne
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:I think your resource files belong under WEB-INF/classes rather than under WEB-INF.


Yep.
 
Ben Dunne
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have another classpath issue. The jar for the class org.apache.derby.jdbc.ClientDriver is not on your classpath.
 
Ben Dunne
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic