You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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().
@OverridepublicvoidafterPropertiesSet() throwsIOException {
LocalSessionFactoryBuildersfb = newLocalSessionFactoryBuilder(
this.dataSource, getResourceLoader(), getMetadataSources());
// Here the getMetadataSources() has been reused in LocalSessionFactoryBuilderpublicMetadataSourcesgetMetadataSources() {
if (this.metadataSources == null) {
BootstrapServiceRegistryBuilderbuilder = newBootstrapServiceRegistryBuilder();
if (this.resourcePatternResolver != null) {
builder = builder.applyClassLoader(this.resourcePatternResolver.getClassLoader());
}
this.metadataSources = newMetadataSources(builder.build());
}
returnthis.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).
It's not clear to me what we could do better here. LocalSessionFactoryBean isn't really designed for registering dynamic mappings to begin with, so this looks like an enhancement request to me. However, what specifically could we be improving to make your case work?
In Hibernate4.LocalSessionFactoryBean.afterPropertiesSet() , LocalSessionFactoryBuilder constructor has only (this.datasource, this.resourcePatternResolver) in constructor argument whereas in In Hibernate5.LocalSessionFactoryBean.afterPropertiesSet() the LocalSessionFactoryBuilder has metaDataSource along with datasource and resourcePatternResolver as constructor argument. This leads to the duplicate files added in xmlBinding variable in metadatasources and lead to DuplicatMappingException in Hibernate package.
Is there any specific reason to reuse the metadaSources in Hibernate5.LocalSessionFactoryBean ? because in 3 and 4 ORM packagaes it was working fine and we have been using it for long time.
It will be good if you can add the enhancement request for registering the dynamic mapping of .hbm files. Meanwhile we will use localSessionFactoryBean.getMetadataSources().getXmlBindings().clear(); as our workaround.
As of 4.3.4, we're resetting the MetadataSources in afterPropertiesSet if they haven't been user-accessed (i.e. if there were no interactions with setMetadataSources / getMetadataSources. This should still allow for MetadataSources customizations as per #18285 while also covering your re-initialization case.
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().
As a quick fix in our code we have cleared the getMetadataSources.getXnlBindings
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:
Referenced from: commits 5912d6f, 7cbab0e
The text was updated successfully, but these errors were encountered: