Hibernate Config / war

D

Daniel

Hi all.

Currently, I have Hibernate working in an exploded directory.

I am having a problem setting up Hibernate's configuration when it is
wrapped up in a war file. The problem, I believe, is somewhere in
setting up the Configuration object. Can you send me or post some code
that shows me how to get the hibernate.cfg.xml file from the WAR and
how you configured Configuration?

I tried getClass().getResourceAsStream( path ) and then converted that
stream into a String, and tried Configuration.configure( string ). It
found the file and I printed it out the contents of that string to make
sure and I do see the hibernate.cfg.xml contents.

I put the war in Tomcat 5's webapp dir and got the following during
startup:

"net.sf.hibernate.HibernateException: (The contents of my
hibernate.cfg.xml go here) not found"



Second related question, when developing I would prefer to use the
exploded directory format, so would I have to have one code for getting
the config from the war file and one code for getting it from a
java.io.File, and then comment one of those codes out depending if I am
developing or want to test it as a war file?

Much appreciated and thanks in advance.
 
O

Oscar kind

Daniel said:
I am having a problem setting up Hibernate's configuration when it is
wrapped up in a war file. The problem, I believe, is somewhere in
setting up the Configuration object. Can you send me or post some code
that shows me how to get the hibernate.cfg.xml file from the WAR and
how you configured Configuration?

I tried getClass().getResourceAsStream( path ) and then converted that
stream into a String, and tried Configuration.configure( string ). It
found the file and I printed it out the contents of that string to make
sure and I do see the hibernate.cfg.xml contents.

I put the war in Tomcat 5's webapp dir and got the following during
startup:

"net.sf.hibernate.HibernateException: (The contents of my
hibernate.cfg.xml go here) not found"

Did you put the configuration file in the webapps directory with the
directories and war files for the wep applications, in WEB-INF, or in
WEB-INGF/classes? Only the last one is found using the classloader (as you
otherwise did cortrectly).

Second related question, when developing I would prefer to use the
exploded directory format, so would I have to have one code for getting
the config from the war file and one code for getting it from a
java.io.File, and then comment one of those codes out depending if I am
developing or want to test it as a war file?

No, one piece of code is sufficient: In the exploded form, you can also
use Class#getResourceAsStream(String)
 
D

Daniel

Hi Oscar,

I have one single war file. Inside the war file, I have
WEB-INF/classes/hibernate/hibernate.cfg.xml. When I load the xml file,
I use getClass().getResourceAsStream( "/hibernate/hibernate.cfg.xml" ).


Then, I put this war file into Tomcat's webapps directory. The only
hibernate.cfg.xml file is inside the war.

So:

c:\java\tomcat\webapps\myapp.war

Inside the myapp.war:

WEB-INF\classes\hibernate\hibernate.cfg.xml
 
G

gimme_this_gimme_that

Your hibernate.cfg.xml file should reside in
htdocs/WEB-INF/classes

And your *.hbm.xml files should reside in the
same directory as your class files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top