Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate.cfg.xml not found

 
Danny Rebello
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am working on websphere 4 and have created a Java application.

My root folder is FirstHibernateExample and it is under
C:\danny\hibernate\FirstHibernateExample\
my java,class and xml files are under
C:\danny\hibernate\FirstHibernateExample\src\com\test\example\

When I compile the java files everything is fine.
When I run the application I get the following error :
net.sf.hibernate.HibernateException: hibernate.cfg.xml not found.
In my code I have specified
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

I have tried placing the hibernate.cfg.xml in all the locations , but
still the error exists.
Can Someone help me on this
Thanks in Advance
Regards
Danny
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simplest thing to do is put your hibernate.cfg.xml file in your src folder
(check that your build process copies it to a similar location wrt class files in your directory structure/jar/war etc...) and implicitly access it via the no-argument configure method:There's no need to mention a path in your code -- this is the default location.
 
prashant sable
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I kept Hibernate-cf.... in classes folder (in webapp since it was struts application), and it worked.

-prashant
 
Danny Rebello
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added the following code :
new Configuration().configure().buildSessionFactory();
but still the error :
net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found
is getting displayed

Regards
Danny
 
garth schneider
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am in the ejb container of Websphere 5.0.2.3 and can not find where to put the hibernate.cfg.xml file.
Is this a websphere issue? I have worked a full 2 days on this and can not figure out what to do.

1/31/06 20:18:38:334 CST] 785e785e SystemOut O Exception creating SessionFactory object: /hibernate.cfg.xml not found
[1/31/06 20:18:42:781 CST] 785e785e Configuration I net.sf.hibernate.cfg.Configuration configuring from resource: /hibernate.cfg.xml

[1/31/06 20:18:42:781 CST] 785e785e Configuration I net.sf.hibernate.cfg.Configuration Configuration resource: /hibernate.cfg.xml

[1/31/06 20:18:42:781 CST] 785e785e Configuration W net.sf.hibernate.cfg.Configuration /hibernate.cfg.xml not found

Garth
IBM Certified Enterprise Developer WSAD 5.x
[ January 31, 2006: Message edited by: garth schneider ]
 
garth schneider
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just figured it out.

File f = new File("C:\\ProjHibernatedEJB\\ProjHiberFacade\\ejbModule\\hibernate.cfg.xml");
sessions = new Configuration().configure(f).buildSessionFactory();
//

I hardcoded the absolute path in a file object and passed it to configure.
It is a sad thing to have to do but it works. And I am not proud.
Garth

[ January 31, 2006: Message edited by: garth schneider ]
[ January 31, 2006: Message edited by: garth schneider ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhmm... you should never have to hardcode this. Where did you try to put it?
 
garth schneider
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After 2 days of fighting this issue I put it everywhere. In the regular Java environment it worked great in the default package. I did not have to pass anything to the configure method. When I brought it into the EJB 2.0 container it did not work in the default package. I tryed to create a regular java jar and put it in the default area. I tryed putting it in its own place in a package and then pass that directory string to the configure method. I even changed the name of the cfg file and reference it by another name. It just did not work. I have a PMR open with IBM now and we will see if I can get anything from that. It works good in JBoss I hear say.
I tryed it in both WSAD 5.1.2 and RAD 6.0. As you can see from the below area it is in the default directory of the EJB area "ejbModule" area. I also tryed putting it above that and it did not work.
File f = new File("C:\\ProjHibernatedEJB\\ProjHiberFacade\\ejbModule\\hibernate.cfg.xml");
sessions = new Configuration().configure(f).buildSessionFactory();


Garth


SCJP (Sun Certified Java Programmer)
IBM #486 Object Oriented Analysis and Design with UML
IBM #283 Specialist for the VisualAge for Java 4.0
IBM Solution Developer for VisualAge for Java 4.0
IBM #157 Specialist for the WebSphere Studio Application Developer 4.0
IBM Solution Developer for WebSphere Studio Application Developer 4.0
IBM #484 Enterprise Connectivity with Java Technology J2EE 1.3
IBM #287 Enterprise Application Development with WebSphere Studio V5.0.
IBM Certified Enterprise Developer - WebSphere Studio, V5.0
[ February 01, 2006: Message edited by: garth schneider ]
 
GowriSankar Narayanaswamy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are running a struts application in Tomcat container of JBoss 4.0.3 SP1. We had the same problem. Here is how we fixed it...
# Copied hibernate.cfg.xml to the JBoss/bin directory.
# Added "." to my classpath.
# Called startup batch file (run.bat) from JBoss/bin directory.

Now, this error is not thrown. It works fine...
 
Jol Blazey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/hibernate.cfg.xml not found .. error in Jboss 3.2 Tomcat 4.1. Hope someone finds this fix useful.

Only getting the error on one development PC, other dev PCs and linux deploy server do not have error when the file is in /WEB_INF/classes - a strange one. To fix I put the file everywhere else I could think of eg. /WEB-INF; /; web-inf/classes/resources; hibernate package folder and error disappeared. Then I narrowed it down to the / <web root>. For some reason hibernate is looking here for the config file. So now I have a security problem with a config file in web root, and another unrelated hibernate error.
 
Jol Blazey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the above fix is not a fix. Putting cfg in web root means that cfg then can't find anything else. still working on it unfortunately.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default hibernate.cfg.xml is expected to be at the root of your classpath. So if that is a Web Application then the root is WEB-INF/classes. All Hibernate does is calls getResourceAsStream("/hibernate.cfg.xml") - no different than getting any other resource on the classpath. If you've put it there and it does not work, then your have another problem that is nothing to do with Hibernate.
 
Jol Blazey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you mean about the problem not being hibernate. More likely to be a Jboss classloader issue. But then how do you explain that Jboss is loading 50 other jars and none of them have classpath issues? Also how do you explain so many others on web having same issue?

I ended up 'fixing' the issue by passing -verbose:class option to the jvm when starting Jboss. I know this is only supposed to print classes loading but it did fix it. Then I took it away again and the problem was gone. And to prove it was not hardware/os, the same happened on 2 pcs. This bug is candidate for the x-files.
 
Debabrat Nayak
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to hard code the absolute path of the hibernate.cfg.xml. You can access it by relative path. Assuming your hibernate.cfg.xml is in src folder under com/hibernateweb/config/hibernate.cfg.xml, you can do the following:

URL url = Thread.currentThread().getContextClassLoader().getResource("com/hibernateweb/config/hibernate.cfg.xml");
SessionFactory sessionFactory = new Configuration().configure(url).buildSessionFactory();
session = sessionFactory.openSession();
 
Mike Tihonchik
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Rebello wrote:

C:\danny\hibernate\FirstHibernateExample\
my java,class and xml files are under
C:\danny\hibernate\FirstHibernateExample\src\com\test\example\

When I compile the java files everything is fine.
When I run the application I get the following error :
net.sf.hibernate.HibernateException: hibernate.cfg.xml not found.
In my code I have specified
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";


try removing forward slash from the location, i.e.
 
german araujo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are using eclipse, try to add the folder where you put the hibernate.cfg.xml as a source folder (configuring your buildpath)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic