Browse Source

Polishing.

Extract adding the actual entity to the MappingContext into its own method along with error handling spanning the entire entity initialization process.

See #2329
Original pull request: #2367.
pull/2456/head
Mark Paluch 5 years ago
parent
commit
8f7a6d30e0
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 39
      src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

39
src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

@ -357,15 +357,34 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<? @@ -357,15 +357,34 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
read.unlock();
}
Class<?> type = typeInformation.getType();
E entity = null;
E entity;
try {
write.lock();
entity = doAddPersistentEntity(typeInformation);
entity = createPersistentEntity(typeInformation);
} catch (BeansException e) {
throw new MappingException(e.getMessage(), e);
} finally {
write.unlock();
}
// Inform listeners
if (applicationEventPublisher != null) {
applicationEventPublisher.publishEvent(new MappingContextEvent<>(this, entity));
}
return Optional.of(entity);
}
private E doAddPersistentEntity(TypeInformation<?> typeInformation) {
try {
Class<?> type = typeInformation.getType();
E entity = createPersistentEntity(typeInformation);
entity.setEvaluationContextProvider(evaluationContextProvider);
// Eagerly cache the entity as we might have to find it during recursive lookups.
@ -373,7 +392,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<? @@ -373,7 +392,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(type);
final Map<String, PropertyDescriptor> descriptors = new HashMap<>();
Map<String, PropertyDescriptor> descriptors = new HashMap<>();
for (PropertyDescriptor descriptor : pds) {
descriptors.put(descriptor.getName(), descriptor);
}
@ -388,21 +407,11 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<? @@ -388,21 +407,11 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
entity.setPersistentPropertyAccessorFactory(persistentPropertyAccessorFactory);
}
} catch (BeansException e) {
throw new MappingException(e.getMessage(), e);
return entity;
} catch (RuntimeException e) {
persistentEntities.remove(typeInformation);
throw e;
} finally {
write.unlock();
}
// Inform listeners
if (applicationEventPublisher != null && entity != null) {
applicationEventPublisher.publishEvent(new MappingContextEvent<>(this, entity));
}
return Optional.of(entity);
}
/*

Loading…
Cancel
Save