rollingbrock wrote:
I have a jar file that contains all of my Hibernate model and mapping files. This jar file is included by a plugin that is used by an Eclipse RCP application. My problem is that Hibernate cannot find the hbm files for the model classes. What do I put in my hibernate.cfg.xml file so Hibernate can find my mapping files? I have tried using the
Code:
<mapping resource="..." />
entries, but they cannot be resolved since the hbm files are inside the jar.
I am using the following code to initialize the SessionFactory:
Code:
Configuration config = new Configuration();
config.configure();
sessionFactory = config.buildSessionFactory();
Is there something I can call on the config object? I have tried calling addClass, but that doesn't work either.
Thanks,
Brock Parker
You might need to create your own EntityResolver and tell Hibernate to use
it when attempting to configure. Have a look at the code below:
Code:
public static Configuration createConfiguration(String configName, String schemaName)
{
Configuration cfg = new Configuration();
cfg.setEntityResolver(new SPCTEntityResolver());
return cfg.configure(configName);
}
...
private static class SPCTEntityResolver extends DTDEntityResolver
{
public InputSource resolveEntity(String publicId, String systemId)
{
if (systemId != null && inclPattern.matcher(systemId).matches())
{
Class resCls = NetworkObject.class;
String rsrcName = systemId.substring(systemId.lastIndexOf('/') + 1);
// log.debug("trying to locate " + rsrcName + " in classpath");
// Search for XML Include
InputStream inclStream = resCls.getResourceAsStream(rsrcName);
if (inclStream == null)
inclStream = resCls.getResourceAsStream("resources/" + rsrcName);
if (inclStream == null)
{
log.debug(systemId + " not found in classpath");
return null;
}
else
{
// log.debug("found " + rsrcName + " in classpath");
InputSource source = new InputSource(inclStream);
source.setPublicId(publicId);
source.setSystemId(systemId);
return source;
}
}
else return super.resolveEntity(publicId, systemId);
}
}
In my case, the hbm files are in a resources subdirectory under the beans
package, and although the path specified in the HIbernate config file
specifies the complete class path, Hibernate could not find the config
files for the beans.