Skip to content

Commit 5912d6f

Browse files
committed
LocalSessionFactoryBean clears default MetadataSources on reinitialization
Issue: SPR-14815
1 parent 20419d7 commit 5912d6f

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

spring-orm/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBean.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
100100

101101
private AsyncTaskExecutor bootstrapExecutor;
102102

103+
private boolean metadataSourcesAccessed = false;
104+
103105
private MetadataSources metadataSources;
104106

105107
private ResourcePatternResolver resourcePatternResolver;
@@ -340,6 +342,7 @@ public void setBootstrapExecutor(AsyncTaskExecutor bootstrapExecutor) {
340342
*/
341343
public void setMetadataSources(MetadataSources metadataSources) {
342344
Assert.notNull(metadataSources, "MetadataSources must not be null");
345+
this.metadataSourcesAccessed = true;
343346
this.metadataSources = metadataSources;
344347
}
345348

@@ -352,6 +355,7 @@ public void setMetadataSources(MetadataSources metadataSources) {
352355
* @see LocalSessionFactoryBuilder#LocalSessionFactoryBuilder(DataSource, ResourceLoader, MetadataSources)
353356
*/
354357
public MetadataSources getMetadataSources() {
358+
this.metadataSourcesAccessed = true;
355359
if (this.metadataSources == null) {
356360
BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
357361
if (this.resourcePatternResolver != null) {
@@ -386,6 +390,11 @@ public ResourceLoader getResourceLoader() {
386390

387391
@Override
388392
public void afterPropertiesSet() throws IOException {
393+
if (this.metadataSources != null && !this.metadataSourcesAccessed) {
394+
// Repeated initialization with no user-customized MetadataSources -> clear it.
395+
this.metadataSources = null;
396+
}
397+
389398
LocalSessionFactoryBuilder sfb = new LocalSessionFactoryBuilder(
390399
this.dataSource, getResourceLoader(), getMetadataSources());
391400

0 commit comments

Comments
 (0)