-->
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.  [ 3 posts ] 
Author Message
 Post subject: hibernate.cfg.xml not found, this is not trivial
PostPosted: Mon Mar 10, 2008 5:41 am 
Newbie

Joined: Tue Nov 14, 2006 11:05 pm
Posts: 3
Location: Romania, Timisoara
I have provided bellow the required data. Now the problem.

I want to have hibernate.cfg.xml (and other config files) together with web.xml, that is, in WEB-INF directory. I'm using Tomcat 6, btw. So, I've changed the HibernateUtil class as follows:

public class HibernateUtil {

private static Log log = LogFactory.getLog(HibernateUtil.class);
private static SessionFactory sessionFactory;

public static final void init(ServletContext context) {
try {
sessionFactory = new AnnotationConfiguration().configure(context.getRealPath("WEB-INF/hibernate.cfg.xml")).buildSessionFactory();
} catch (Throwable ex) {
// Log exception
log.error(ex, ex);
throw new ExceptionInInitializerError(ex);
}
}

public static Session getSession() throws HibernateException {
return sessionFactory.openSession();
}
}

I call the init(ServletContext) method from a ContextFilter.init so that I can provide the context. Given this scenario, I get the exception listed bellow, altough the xml is in the right location. If I place hibernate.cfg.xml in class path and use configure() (that is, no params version) everything works as expected.

A secondary problem here is that I can't convince hibernate (or log4j for that matter) to output more than INFO logging. I'm not proficient with log4j and I appologize for that. I used and changed the log4j.properties file included in hibernate distribution, please see bellow the excerpt.

Thank you for your time.


Hibernate version: 3.2.6 with Hibernate Annotations 3.3.0.GA

Mapping documents:

Code between sessionFactory.openSession() and session.close(): doesn't get that far

Full stack trace of any exception that occurs:
org.hibernate.HibernateException: D:\Java\tomcat\webapps\ROOT\WEB-INF\hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1411)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1433)
at ro.level7.tds.utils.HibernateUtil.init(HibernateUtil.java:22)
at ro.level7.tds.ContextFilter.init(ContextFilter.java:31)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3696)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4343)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)


Name and version of the database you are using: irrelevant for the issue
The generated SQL (show_sql=true): irrelevant for the issue

Debug level Hibernate log excerpt:
log4j.rootLogger=debug, stdout
log4j.logger.org.hibernate=debug

_________________
"When all think alike, then no one is thinking". Walter Lippman


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 3:24 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I believe that Configuration.configure(String resource) is actually using a class called config handler to load the specified resource via the classpath. Try to use Configuration.configure(File f) or Configuration.configure(Url url).

So in your example:
Code:
sessionFactory = new AnnotationConfiguration().configure(new File(context.getRealPath("WEB-INF/hibernate.cfg.xml"))).buildSessionFactory();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 1:30 pm 
Newbie

Joined: Tue Nov 14, 2006 11:05 pm
Posts: 3
Location: Romania, Timisoara
Thanx a lot, it worked. Pin point solution :)

Then what's the use of the String flavored method of Configuration?

_________________
"When all think alike, then no one is thinking". Walter Lippman


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