views:

40

answers:

1

I've got a Java application that uses Hibernate for persistence. Now I'm trying to expose part of this app as a web service deployed to Glassfish 3.0.1.

The code that implements the service is in a JAR file that is deployed along with the WAR file into Glassfish. The service code in the WAR has no problem accessing the classes that use Hibernate in the JAR file.

When I try to call the service, however, an exception is thrown saying Hibernate can't find the hibernate.cfg.xml file. For the life of me I can't figure out where the .cfg file should go in either the JAR, the WAR or in Glassfish so that it can be found on the classpath.

I've tried putting it in a META-INF directory in the WAR file (which is actually "meta-inf" when I examine the WAR) and in that same directory in the JAR. I also tried creating a META-INF directory in several different Glassfish directories, all to no avail.

I had to monkey around quite a bit with where to put the .cfg file while developing this service in Eclipse in order to get it into the classpath. It seems this is the issue when deploying it to GF as well.

Can anyone tell me where the .cfg file should go so it can be accessed when this service is deployed in GF?

Thanks,

Chris

+1  A: 

If you are using the no-arg Configure#configure() method, then the hibernate.cfg.xml file is expected to be at the root of the classpath.

Here is the relevant snippet from o.h.c.Configuration.java:

/**
 * Use the mappings and properties specified in an application
 * resource named <tt>hibernate.cfg.xml</tt>.
 */
public Configuration configure() throws HibernateException {
    configure( "/hibernate.cfg.xml" );
    return this;
}

/**
 * Use the mappings and properties specified in the given application
 * resource. The format of the resource is defined in
 * <tt>hibernate-configuration-3.0.dtd</tt>.
 * <p/>
 * The resource is found via <tt>getConfigurationInputStream(resource)</tt>.
 */
public Configuration configure(String resource) throws HibernateException {
    log.info( "configuring from resource: " + resource );
    InputStream stream = getConfigurationInputStream( resource );
    return doConfigure( stream, resource );
}

This is actually documented in the Hibernate Core reference guide:

3.7. XML configuration file

An alternative approach to configuration is to specify a full configuration in a file named hibernate.cfg.xml. This file can be used as a replacement for the hibernate.properties file or, if both are present, to override properties.

The XML configuration file is by default expected to be in the root of your CLASSPATH. (...)

Pascal Thivent
Thanks, Pascal, but the issue I'm facing is, in Glassfish, what location is considered to be in the "root of the CLASSPATH"? Specifically, where should the .cfg file be placed in the deployed WAR file, a contained JAR file or directly in the Glassfish deployment directories?
Chris M
@Chris At the root of a JAR inside `WEB-INF/lib`, or at the root of `WEB-INF/classes`, or at the root of a GlassFish directory that is on the webapp classpath. As long as it's at the root.
Pascal Thivent