aspose file tools*
The moose likes Object Relational Mapping and the fly likes hibernate.cfg.xml required in version 2.x? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate.cfg.xml required in version 2.x?" Watch "hibernate.cfg.xml required in version 2.x?" New topic
Author

hibernate.cfg.xml required in version 2.x?

Lance Hill
Greenhorn

Joined: Oct 13, 2004
Posts: 14
I had Hibernate 1.2 working okay in a simple junit test. Now I am trying to get it working in an simple Struts application. I have two questions:

1. Is is now required to provide a hibernate.cfg.xml file? Whenever I try to run a new test with the 2.1 version of Hibernate, I get the following error:
net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found

2. Where do I put the hibernate.properties and hibernate.cfg.xml files? I thought that putting them into the WEB-INF/classes folder would work, but I am hoping someone can confirm.
Surya Suravarapu
Greenhorn

Joined: Oct 20, 2004
Posts: 10
Originally posted by Lance Hill:
I had Hibernate 1.2 working okay in a simple junit test. Now I am trying to get it working in an simple Struts application. I have two questions:

1. Is is now required to provide a hibernate.cfg.xml file? Whenever I try to run a new test with the 2.1 version of Hibernate, I get the following error:
net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found

Make sure you have hibernate.cfg.xml in the classpath.


2. Where do I put the hibernate.properties and hibernate.cfg.xml files? I thought that putting them into the WEB-INF/classes folder would work, but I am hoping someone can confirm.

You don't need both the files. You can either have a properties file or an xml file for configuration. WEB-INF/classes is fine.
Mika Riekkinen
Greenhorn

Joined: Jun 12, 2002
Posts: 2
I had the same problem and got it solved.

1) hibernate.cfg.xml into WEB-INF/classes/
2) obtain an URL to this file
URL url = MyClass.class.getResource("/hibernate.cfg.xml");
Make sure MyClass is part of your web-app
3) Configure hibernate
new Configuration().configure(url);
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
I would like to add that you must be carefull to create only one time the SessionFactory as this is quite a consuming task.

./pope


blog - InfoQ.com
Filipe Marques
Greenhorn

Joined: Nov 08, 2004
Posts: 9
How can I add the hibernate.cfg.xml to the classpath ?

Under Eclipse, it is allowed to add only jar files ?

I'm developping a standalone application and using hibernate Synchronizer.
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
You just need to add to classpath the directory containing the xml file .

./pope
Filipe Marques
Greenhorn

Joined: Nov 08, 2004
Posts: 9
Thank You!
It works.

I still have a problem. The mapping files are not found.
Hanson Yuan
Greenhorn

Joined: Feb 04, 2003
Posts: 12
How do you make sure you are only loading sessionFactory once in a web app? With a singleton, or at server startup time? When do you clean it up? Thanks,
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
This is presented very detailed on the Hibernate official reference first chapter: Quickstart with Tomcat.

./pope
Bernd Zimmermann
Greenhorn

Joined: Nov 23, 2004
Posts: 2
We had the 'hibernate.cfg.xml not found' problem when developing
IBM WebSphere Portalserver portlets with struts and hibernate.

The solution was to have the hibernate.cfg.xml in WebSphere
Application Developer in ./JavaSources and get the hibernate
configuration at runtime this way:

File f=new File(servlet.getServletContext()
.getRealPath("/WEB-INF/classes/hibernate.cfg.xml"));

Configuration c = new Configuration().configure(f);

This is portable from WSAD to WPS

Bernd,
http://www.prolifics.de
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate.cfg.xml required in version 2.x?