Closed spring-projects-issues closed 12 years ago
John Baker commented
Here's my first attempt at a fix, which isn't very nice but seems to work.
import org.springframework.context.*; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource;
/**
This class extends the Spring ORM LocalSessionFactoryBean to fix bug
http://opensource.atlassian.com/projects/spring/browse/SPR-3965
by overriding the setMappingResource method and setting the classpath.
It uses the ApplicationContextAware interface to get the classloader set on the
ApplicationContext, and overrides the setMappingResources method to
create the ClassPathResource objects with this classloader. */ public class LocalSessionFactoryBean extends org.springframework.orm.hibernate3.LocalSessionFactoryBean implements ApplicationContextAware { private ClassLoader classLoader; private String[] mappingResources;
// This is (annoyingly) called after setMappingResources public void setApplicationContext(ApplicationContext applicationContext) { classLoader = applicationContext.getClassLoader(); Resource[] mappingLocations = new Resource[mappingResources.length]; for (int i = 0; i < mappingResources.length; i++) { mappingLocations[i] = new ClassPathResource(mappingResources[i].trim(), classLoader); } super.setMappingLocations(mappingLocations); }
public void setMappingResources(String[] mappingResources) { this.mappingResources = mappingResources; super.setMappingResources(mappingResources); }
}
Juergen Hoeller commented
Thanks for spotting this! Indeed, we need to use the correct ClassLoader there, else usage in OSGi environments won't work either.
Juergen
Juergen Hoeller commented
BTW, as a workaround: Specifying those mapping paths as "mappingLocations", using a "classpath:" prefix in front of each location, should use the correct ClassLoader already. It's only "mappingResources" which doesn't work properly there.
Juergen
John Baker opened SPR-3965 and commented
Consider the following:
\
\
And the following code sample:
While the configuration-context.xml is loaded from the classLoader provided, the Hibernate mapping file (Admin.hbm.xml) is loaded from the Thread's classloader. This is because of the following code in LocalSessionFactoryBean:
The ClassPathResource is not given a reference to 'classLoader', set on the ClassPathXmlApplicationContext object.
I've discovered this by looking at the classLoader reference in the following section of code (from ClassPathResource):
The classLoader reference is not the one I set here:
Affects: 2.0.5