-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 
Author Message
 Post subject: hibernate.cfg.xml not found
PostPosted: Thu Mar 25, 2004 11:55 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
Hi!

Hibernate does not find its configuration file when put in /WEB-INF/classes, neither does it find it in /WEB-INF/ or /.

The following does work as a workaround when putting it in /:

URL hib = new URL("http://localhost:8080/hibernate.cfg.xml");
Configuration config = new Configuration().configure(hib);

Curiously it does find the referenced .hbm.xml files in /WEB-INF/classes....?

I run JettyPlus if this should matter...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2004 6:59 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
the config file must be seen by the classloader loading Hibernate

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 3:34 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
Well, i found out that Environment.class.getResourceAsStream() in Configuration().configure() returns null. I don't know why it does. getClass().getResourceAsStream() in my own class does return the stream (?!).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 4:33 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Again certainly a CL problem

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 4:52 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
And what am I going to do against it?


Top
 Profile  
 
 Post subject: Same Issue..
PostPosted: Fri Mar 26, 2004 10:58 am 
Newbie

Joined: Fri Mar 26, 2004 10:55 am
Posts: 2
I am having the same problem. It is my understanding from reading the documentation that Hibernate should automatically find a file called hibernate.cfg.xml that is in the classpath. If I switch to the properties file, hibernate.properties, and register my persistent classes automatically, then it works. Why does it find one file and not the other?

Regards,
awellman


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 11:03 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Hibernate simply calls getResourceAsStream("/hibernate.cfg.xml"), this is executed by the classloader that loaded Hibernate (the JAR, usually). There's not much we can do if this does not work in your classloader hierarchy/environment.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 11:15 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
But what can be reasons for it to fail?

class Test
{
public void go()
{
new Configuration.configuration(); // fails (stream == null)
getClass().getResourceAsStream("/hibernate.cfg.xml"); // works
}
}

??


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 11:17 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
It works for thousands of people too. I can't help you here, it's your environment. Remember that getClass() is not the same as in Hibernate Environment.class.etc, it might be a different classloader.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 12:18 pm 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
christian wrote:
It works for thousands of people too. I can't help you here, it's your environment. Remember that getClass() is not the same as in Hibernate Environment.class.etc, it might be a different classloader.


Could you be a little bit more precise on what you mean by 'my environment'? Both runs in 1 VM - isn't it the same classloader in both cases as well?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 12:20 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No, please read up on the Java classloader system.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 12:25 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
Could you be a little bit more precise on what you mean by 'my environment'? Both runs in 1 VM - isn't it the same classloader in both cases as well?[/quote]


I am using Resin 2.1.12 with Hibernate 2.1 on SAPDB 7.4

Here is how I have my environment setup.

Location of Hibernate configuration files:
    /webapps/foo/WEB-INF/classes/hibernate-sapdb.cfg.xml
    ...
    ...



Here is my HibernateManager code that loads the confirguration successfully.

Code:
static
  {
    try
    {
      /**
       *  File name need not be specified here. By default hibernate.cfg.xml is chosen.
       *  However, if the file name is changed in the application, it will need to
       *  be modified here
       */
      SessionFactory sessionFactory = new Configuration().configure("/hibernate-sapdb.cfg.xml").buildSessionFactory();
    }
    catch (HibernateException e)
    {
      logger.debug("HibernateException building SessionFactory: " + e.getMessage());
      throw new RuntimeException("HibernateException building SessionFactory: " + e.getMessage(), e);
    }
  }



Hope that helps.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 3:50 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:09 pm
Posts: 58
I may be mistaken, but it could be related to [urlhttp://opensource.atlassian.com/projects/hibernate/browse/HB-576]HB-576[/url]?

You could try something like this and see if it makes a difference:

Code:
URL url = Thread.currentThread().getContextClassLoader().getResource("/hibernate-sapdb.cfg.xml");
SessionFactory sessionFactory = new Configuration().configure(url).buildSessionFactory();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 4:02 pm 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
jfifield wrote:
I may be mistaken, but it could be related to [urlhttp://opensource.atlassian.com/projects/hibernate/browse/HB-576]HB-576[/url]?

You could try something like this and see if it makes a difference:

Code:
URL url = Thread.currentThread().getContextClassLoader().getResource("/hibernate-sapdb.cfg.xml");
SessionFactory sessionFactory = new Configuration().configure(url).buildSessionFactory();


NullPointerException :-(


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 4:05 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
Is there any particular reason you are using the URL object? If you can't find the file in your path somehow, try creating a java.io.File object and using that. I tried that as well and it worked for me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.