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.
This commit is contained in:
Mark Paluch
2021-07-20 10:16:19 +02:00
parent 2511b1f1d6
commit 5fd8687506

View File

@@ -364,15 +364,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.
@@ -380,7 +399,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);
}
@@ -395,21 +414,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);
}
/*