Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Lance Hill
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Filipe Marques
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just need to add to classpath the directory containing the xml file .

./pope
 
Filipe Marques
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You!
It works.

I still have a problem. The mapping files are not found.
 
Hanson Yuan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is presented very detailed on the Hibernate official reference first chapter: Quickstart with Tomcat.

./pope
 
Bernd Zimmermann
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic