Hi,
I am writing a web service which relies on Hibernate to persist data in a MySQL DBMS. The web service is using Tomcat 6 with Axis2 on Ubuntu 9.
Unfortunately, I am unable to create a session factory. Whatever I try, I always get the exception message "/hibernate.cfg.xml not found". So far, I tried the following:
- Putting hibernate.cfg.xml and mappings.hbm.xml (which is referenced by the former) into <Axishome>/classes.
- Putting the XML files into <Axishome>/services.
- Putting the XML files into <Axishome> (/var/lib/tomcat6/webapps/axis2/WEB-INF/).
- Storing the XML files in one JAR file, then putting the JAR file into <Axishome>/lib.
- Storing the XML files on the root of the AAR archive used for deployment.
- Storing the XML files inside the META-INF directory of the AAR archive.
I combined all of these approaches with this modification:
Code:
<parameter name="ServiceTCCL">composite</parameter>
of the service.xml inside the AAR archive, below the <service> tag.
I used the following two approaches to create a Hibernate session factory, both of which did not work:
Approach 1:
Code:
SessionFactory myFactory = new Configuration().configure().buildSessionFactory();
Approach 2:
Code:
ConfigurationContext myConfigContext = ConfigurationContextFactory.createDefaultConfigurationContext();
ClassLoader myLoader = myConfigContext.getAxisConfiguration().getServiceClassLoader();
URL configURL = myLoader().getResource("hibernate.cfg.xml");
SessionFactory myFactory = new Configuration().configure(configURL).buildSessionFactory();
When using Approach 1, the method buildSessionFactory() throws a "org.hibernate.HibernateException" with the message "/hibernate.cfg.xml not found". When using Approach 2, the loader cannot find the resource (configURL remains null). This remains the same when calling getSystemClassLoader() or getModuleClassLoader() rather than getServiceClassLoader().
Someone on the internet suggested to use the following code instead:
Code:
URL configURL = MessageContext.getCurrentMessageContext().getAxisService().getClassLoader().getResource("hibernate.cfg.xml");
However, this does not work during the initialization of the server stub, since getCurrentMessageContext() returns null then. My database gateway class is supposed to be initialized when the server stub is instantiated.
So far, I've browsed a lot of Google pages, but without any luck. Does anyone know why Hibernate keeps ignoring the config file?