Description
Arunkumar opened SPR-14815 and commented
We are working on a project where we have to invoke the
localSessionFactoryBean.afterPropertiesSet(); to reload the hibernate's session factory to add dynamically created HBM from our application, but we got the org.hibernate.DuplicateMappingException.
Upon debugging hibernate5.2.2 source code, we found that the issue starts at the localSessionFactoryBean.afterPropertiesSet().
@Override
public void afterPropertiesSet() throws IOException {
LocalSessionFactoryBuilder sfb = new LocalSessionFactoryBuilder(
this.dataSource, getResourceLoader(), getMetadataSources());
// Here the getMetadataSources() has been reused in LocalSessionFactoryBuilder
public MetadataSources getMetadataSources() {
if (this.metadataSources == null) {
BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
if (this.resourcePatternResolver != null) {
builder = builder.applyClassLoader(this.resourcePatternResolver.getClassLoader());
}
this.metadataSources = new MetadataSources(builder.build());
}
return this.metadataSources;
}
/*Here the previously created metadatasources values are being returned without clearing the xmlbindings values, so hibernate again adds (duplicates) all the .hbm files to its sessionfactory which finally results in DuplicateMappingException. */
As a quick fix in our code we have cleared the getMetadataSources.getXnlBindings
localSessionFactoryBean.getMetadataSources().getXmlBindings().clear();
//and then we called the
localSessionFactoryBean.afterPropertiesSet();
This scenario will be reproduced only when we have the joined subclass hbm mapping, because in hiberanate 5.2.2 the duplication validation happens at InFlightMetadataCollectorImpl (Line No: 268). This method will be invoked by ModelBinder class method : bindJoinedSubclassEntities( Line No: 576).
Affects: 4.3.3
Issue Links:
- Hibernate5 metadata access [SPR-13710] #18285 Hibernate5 metadata access