Skip to content

DATACMNS-1208 - fixed hasPersistentEntityFor() vs getPersistentEntity… #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
Expand All @@ -52,7 +51,6 @@
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.Optionals;
import org.springframework.data.util.Pair;
import org.springframework.data.util.Streamable;
import org.springframework.data.util.TypeInformation;
Expand All @@ -73,6 +71,7 @@
*
* @param <E> the concrete {@link PersistentEntity} type the {@link MappingContext} implementation creates
* @param <P> the concrete {@link PersistentProperty} type the {@link MappingContext} implementation creates
* @author Bartosz Kielczewski
* @author Jon Brisbin
* @author Oliver Gierke
* @author Michael Hunger
Expand All @@ -85,8 +84,7 @@
public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?, P>, P extends PersistentProperty<P>>
implements MappingContext<E, P>, ApplicationEventPublisherAware, InitializingBean {

private final Optional<E> NONE = Optional.empty();
private final Map<TypeInformation<?>, Optional<E>> persistentEntities = new HashMap<>();
private final Map<TypeInformation<?>, E> persistentEntities = new HashMap<>();
private final Map<TypeAndProperties, PersistentPropertyPath<P>> propertyPaths = new ConcurrentReferenceHashMap<>();
private final PersistentPropertyAccessorFactory persistentPropertyAccessorFactory = new ClassGeneratingPropertyAccessorFactory();

Expand Down Expand Up @@ -145,7 +143,7 @@ public void setSimpleTypeHolder(SimpleTypeHolder simpleTypes) {

/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.MappingContext#getPersistentEntities()
* @see org.springframework.data.mapping.context.MappingContext#getPersistentEntities()
*/
@Override
public Collection<E> getPersistentEntities() {
Expand All @@ -154,9 +152,7 @@ public Collection<E> getPersistentEntities() {

read.lock();

return persistentEntities.values().stream()//
.flatMap(Optionals::toStream)//
.collect(Collectors.toSet());
return new HashSet<>(persistentEntities.values());

} finally {
read.unlock();
Expand Down Expand Up @@ -186,45 +182,42 @@ public boolean hasPersistentEntityFor(Class<?> type) {

/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.MappingContext#getPersistentEntity(org.springframework.data.util.TypeInformation)
* @see org.springframework.data.mapping.context.MappingContext#getPersistentEntity(org.springframework.data.util.TypeInformation)
*/
@Nullable
@Override
public E getPersistentEntity(TypeInformation<?> type) {

Assert.notNull(type, "Type must not be null!");

E entity = getExistingPersistentEntity(type);
if (entity != null) {
return entity;
} else if (!shouldCreatePersistentEntityFor(type)) {
return null;
} else if (strict) {
throw new MappingException("Unknown persistent entity " + type);
} else {
return addPersistentEntity(type);
}
}

/**
* (non-Javadoc)
* @see org.springframework.data.mapping.context.MappingContext#getPersistentEntity(org.springframework.data.util.TypeInformation)
*/
@Nullable
private E getExistingPersistentEntity(TypeInformation<?> type) {

try {

read.lock();

Optional<E> entity = persistentEntities.get(type);

if (entity != null) {
return entity.orElse(null);
}
return persistentEntities.get(type);

} finally {
read.unlock();
}

if (!shouldCreatePersistentEntityFor(type)) {

try {
write.lock();
persistentEntities.put(type, NONE);
} finally {
write.unlock();
}

return null;
}

if (strict) {
throw new MappingException("Unknown persistent entity " + type);
}

return addPersistentEntity(type).orElse(null);
}

/*
Expand Down Expand Up @@ -339,7 +332,7 @@ private Pair<DefaultPersistentPropertyPath<P>, E> getPair(DefaultPersistentPrope
* @param type must not be {@literal null}.
* @return
*/
protected Optional<E> addPersistentEntity(Class<?> type) {
protected E addPersistentEntity(Class<?> type) {
return addPersistentEntity(ClassTypeInformation.from(type));
}

Expand All @@ -349,26 +342,16 @@ protected Optional<E> addPersistentEntity(Class<?> type) {
* @param typeInformation must not be {@literal null}.
* @return
*/
protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
protected E addPersistentEntity(TypeInformation<?> typeInformation) {

Assert.notNull(typeInformation, "TypeInformation must not be null!");

try {

read.lock();

Optional<E> persistentEntity = persistentEntities.get(typeInformation);

if (persistentEntity != null) {
return persistentEntity;
}

} finally {
read.unlock();
E entity = persistentEntities.get(typeInformation);
if (entity != null) {
return entity;
}

Class<?> type = typeInformation.getType();
E entity = null;

try {

Expand All @@ -377,7 +360,7 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
entity = createPersistentEntity(typeInformation);

// Eagerly cache the entity as we might have to find it during recursive lookups.
persistentEntities.put(typeInformation, Optional.of(entity));
persistentEntities.put(typeInformation, entity);

PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(type);

Expand Down Expand Up @@ -414,7 +397,7 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
applicationEventPublisher.publishEvent(new MappingContextEvent<>(this, entity));
}

return Optional.of(entity);
return entity;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
/**
* Unit test for {@link AbstractMappingContext}.
*
* @author Bartosz Kielczewski
* @author Oliver Gierke
* @author Thomas Darimont
* @author Mark Paluch
Expand Down Expand Up @@ -115,6 +116,14 @@ public void returnsNullPersistentEntityForSimpleTypes() {
assertThat(context.getPersistentEntity(String.class)).isNull();
}

@Test // DATACMNS-1208
public void ensureHasPersistentEntityReportsFalseForTypesThatShouldntBeCreated() {
SampleMappingContext context = new SampleMappingContext();
assertThat(context.hasPersistentEntityFor(String.class)).isFalse();
assertThat(context.getPersistentEntity(String.class)).isNull();
assertThat(context.hasPersistentEntityFor(String.class)).isFalse();
}

@Test(expected = IllegalArgumentException.class) // DATACMNS-214
public void rejectsNullValueForGetPersistentEntityOfClass() {
context.getPersistentEntity((Class<?>) null);
Expand Down