-->
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.  [ 5 posts ] 
Author Message
 Post subject: cfg.addFile() + cfg.configure() not working
PostPosted: Sun May 15, 2005 2:35 am 
Newbie

Joined: Tue May 03, 2005 8:02 pm
Posts: 13
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3
Mapping documents:
None

Code between sessionFactory.openSession() and session.close():
None. But we are doing:

Configuration c = new Configuration();
c.addFile(fileName);
SessionFactory factory = c.configure().buildSessionFactory();

and it blows up because it can't find the cfg file. I promise, it is where fileName says it is.

But it looks like the Hibernate source code ignores the setting. When you call configure, this is the method
Code:
   public Configuration configure() throws HibernateException {
      configure( "/hibernate.cfg.xml" );
      return this;
   }


which doesn't look like it's paying any attention to the files a calling class might add.

Am I misunderstanding/reading?

What I need to do is give Hibernate cfg file that's not in the application's path.

Thanks

Full stack trace of any exception that occurs:
Code:
Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found
   at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1137)
   at org.hibernate.cfg.Configuration.configure(Configuration.java:1161)
   at org.hibernate.cfg.Configuration.configure(Configuration.java:1148)
   at com.rps.hibernate.CFHelper.main(CFHelper.java:58)

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Code:
About to addFile C:\JRun4\servers\default\cfusion-ear\cfusion-war\hibernate\hibernate.cfg.xml to the configuration.
- Mapping file: C:\JRun4\servers\default\cfusion-ear\cfusion-war\hibernate\hibernate.cfg.xml
Added file C:\JRun4\servers\default\cfusion-ear\cfusion-war\hibernate\hibernate.cfg.xml to the configuration.
- configuring from resource: /hibernate.cfg.xml
- Configuration resource: /hibernate.cfg.xml
- /hibernate.cfg.xml not found
[/code]


Top
 Profile  
 
 Post subject: Using URL seems to work....
PostPosted: Sun May 15, 2005 9:58 pm 
Newbie

Joined: Tue May 03, 2005 8:02 pm
Posts: 13
Ok,

I did some more research on this. By using Class' classLoader, it looks like there may be some limitation on the file's location (not sure I understand why, but there's a lot of text in the docs for Class.getResourceAsStream()).

So I tried configure() off a URL (that points to the same file).

Code:
      final String sep = File.pathSeparator;
      final String fileName = "C:" + sep + "JRun4" + sep + "servers" + sep + "default" + sep + "cfusion-ear" + sep + "cfusion-war" + sep + "hibernate" + sep + "hibernate.cfg.xml";
      final URL url;
      try {
         url = new URL("file:///C:/JRun4/servers/default/cfusion-ear/cfusion-war/hibernate/hibernate.cfg.xml");
         Configuration c = me.getConfiguration();
         SessionFactory factory = c.configure(url).buildSessionFactory();

This is working, albeit kind of cumbersome.

If anyone can give me some more info on this I'd really appreciate it.

Thanks
Scott
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 16, 2005 3:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you apparently doesnt have /hibernate.cfg.xml in your classpath.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 16, 2005 11:25 am 
Newbie

Joined: Tue May 03, 2005 8:02 pm
Posts: 13
Max,

Thanks for your note.

As far as I can tell, it IS in the classpath. Maybe it's not. The file itself is in another Eclipse project which I believe I have correclty added to the Run as... classpath.

But the error does not occur when I addFile(). I've traced through the code and Configure happily parses it and creates the Mapping object.

The failure occurs when I call configure(), at which time it seems Hibernate is hard-coded to look for '/hibernate.cfg.xml'.

Reading the JavaDocs nn getResourceAsStream() I can see that "/" has some special meaning (to be honest I don't understand what exactly). But still, the "hibernate.cfg.xml" part is hard-code and it doesn't seem that it ever looks to the files that were added with addFile() to get the configuration information.

In short, I guess I'm confused why addFile() exists if the file name is hardcoded in configure().

If anyone on the forum could help me understand I'd really appreciate it. In our application, each client front end will need its own configuration file, which is why addFile() was so interesting: we thought we could have clientName.cfg.xml and be done.

Thanks
Scott


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 16, 2005 11:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
addFile is for adding mapping files (hbm.xml), adding a cfg.xml will just be a noop since it does not contain the xml for a hbm.xml

configure(xxx) is for configuring via .cfg.xml files. "/hibernate.cfg.xml" is the default location, which means "in the root of the classpath, find hibernate.cfg.xml"

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.