Reputation: 183
I'm using Hibernate 4.1 in GWT app running on Jetty 1.6 Got the next code to start up hib.instance:
Configuration configuration = new Configuration().configure(ABS_PATH_TO_CONFIG+File.separator+"hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
First line gives me an error:
org.hibernate.HibernateException: ...hibernate.cfg.xml not found
at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
But I checked hibernate.cfg.xml
availability just before loading hib.config:
File conf = new File(ABS_PATH_TO_CONFIG+File.separator+"hibernate.cfg.xml");
System.out.println(conf.canRead());
Sysout returns true.
Looking into source of ConfigHelper.getResourceAsStream
with break point in it:
InputStream stream = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader!=null) {
stream = classLoader.getResourceAsStream( stripped );
}
if ( stream == null ) {
stream = Environment.class.getResourceAsStream( resource );
}
if ( stream == null ) {
stream = Environment.class.getClassLoader().getResourceAsStream( stripped );
}
if ( stream == null ) {
throw new HibernateException( resource + " not found" );
}
I'm doing something wrong (doesn't understand something) or it's really no xml loaders here?
Upvotes: 2
Views: 3335
Reputation: 4649
I'll tell you that program won't never treat you. about your problem, you can do like this to get the path of the config file:
String basePath = PropertiesUtil.class.getResource("/").getPath();
then read it
InputStream in = new FileInputStream(basePath + fileName);
Good luck!
Upvotes: 1
Reputation: 183
This way custom-located config file is loaded:
File conf = new File(ABS_PATH_TO_CONFIG+File.separator+"hibernate.cfg.xml");
Configuration configuration = new Configuration().configure(conf.getAbsoluteFile());
FYC: configure()
method are overloaded
Upvotes: 1
Reputation: 3749
There are several things wrong here.
First of all, this:
Configuration configuration = new Configuration().configure(ABS_PATH_TO_CONFIG+File.separator+"hibernate.cfg.xml");
does not do what you think it does.
Your example is not checking the availability of the configuration file. It is checking whether the file exists on the file system, not in the classpath. This difference is important.
Without knowing more about how you build and deploy your webapp or how you have your files organized, it's hard to give you any more concrete advice, other than try copying the "hibernate.cfg.xml" to the root of your classpath, and just passing that to the configure() method. That should work.
So your code should be:
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
And your hibernate.cfg.xml file should be in the root of your classpath.
Alternatively, if you're using Maven, just put it under the "resources" folder and Maven should do the rest for you.
Upvotes: 3