Skip to content

DATACMNS-1364 - Store persistent properties in ConcurrentHashMap #304

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 3 commits 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATACMNS-1364-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,15 @@

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.data.annotation.Immutable;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.Alias;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.AssociationHandler;
import org.springframework.data.mapping.IdentifierAccessor;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PreferredConstructor;
import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.mapping.SimpleAssociationHandler;
import org.springframework.data.mapping.SimplePropertyHandler;
import org.springframework.data.mapping.TargetAwareIdentifierAccessor;
import org.springframework.data.mapping.*;
import org.springframework.data.spel.EvaluationContextProvider;
import org.springframework.data.support.IsNewStrategy;
import org.springframework.data.support.PersistableIsNewStrategy;
Expand All @@ -59,6 +39,7 @@
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.ConcurrentReferenceHashMap.ReferenceType;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -124,9 +105,10 @@ public BasicPersistentEntity(TypeInformation<T> information, @Nullable Comparato
this.constructor = PreferredConstructorDiscoverer.discover(this);
this.associations = comparator == null ? new HashSet<>() : new TreeSet<>(new AssociationComparator<>(comparator));

this.propertyCache = new ConcurrentReferenceHashMap<>();
this.annotationCache = new ConcurrentReferenceHashMap<>();
this.propertyAnnotationCache = CollectionUtils.toMultiValueMap(new ConcurrentReferenceHashMap<>());
this.propertyCache = new ConcurrentHashMap<>();
this.annotationCache = new ConcurrentReferenceHashMap<>(16, ReferenceType.WEAK);
this.propertyAnnotationCache = CollectionUtils
.toMultiValueMap(new ConcurrentReferenceHashMap<>(16, ReferenceType.WEAK));
this.propertyAccessorFactory = BeanWrapperPropertyAccessorFactory.INSTANCE;
this.typeAlias = Lazy.of(() -> getAliasFromAnnotation(getType()));
this.isNewStrategy = Lazy.of(() -> Persistable.class.isAssignableFrom(information.getType()) //
Expand Down Expand Up @@ -254,7 +236,7 @@ public void addPersistentProperty(P property) {
}
}

/*
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.MutablePersistentEntity#setEvaluationContextProvider(org.springframework.data.spel.EvaluationContextProvider)
*/
Expand Down Expand Up @@ -487,7 +469,7 @@ public IdentifierAccessor getIdentifierAccessor(Object bean) {
return hasIdProperty() ? new IdPropertyIdentifierAccessor(this, bean) : new AbsentIdentifierAccessor(bean);
}

/*
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.PersistentEntity#isNew(java.lang.Object)
*/
Expand All @@ -499,7 +481,7 @@ public boolean isNew(Object bean) {
return isNewStrategy.get().isNew(bean);
}

/*
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.PersistentEntity#isImmutable()
*/
Expand All @@ -525,7 +507,7 @@ protected EvaluationContext getEvaluationContext(Object rootObject) {
* Returns the default {@link IsNewStrategy} to be used. Will be a {@link PersistentEntityIsNewStrategy} by default.
* Note, that this strategy only gets used if the entity doesn't implement {@link Persistable} as this indicates the
* user wants to be in control over whether an entity is new or not.
*
*
* @return
* @since 2.1
*/
Expand All @@ -535,7 +517,7 @@ protected IsNewStrategy getFallbackIsNewStrategy() {

/**
* Verifies the given bean type to no be {@literal null} and of the type of the current {@link PersistentEntity}.
*
*
* @param bean must not be {@literal null}.
*/
private final void verifyBeanType(Object bean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,27 @@ public void considersComparatorForPropertyOrder() {
assertThat(entity.getPersistentProperty("ssn")).isEqualTo(iterator.next());
}

@Test // DATACMNS-186
@Test // DATACMNS-18, DATACMNS-1364
public void addingAndIdPropertySetsIdPropertyInternally() {

MutablePersistentEntity<Person, T> entity = createEntity(Person.class);
assertThat(entity.getIdProperty()).isNull();

when(property.getName()).thenReturn("id");
when(property.isIdProperty()).thenReturn(true);
entity.addPersistentProperty(property);
assertThat(entity.getIdProperty()).isEqualTo(property);
}

@Test // DATACMNS-186
@Test // DATACMNS-186, DATACMNS-1364
public void rejectsIdPropertyIfAlreadySet() {

MutablePersistentEntity<Person, T> entity = createEntity(Person.class);

when(property.getName()).thenReturn("id");
when(property.isIdProperty()).thenReturn(true);
when(anotherProperty.isIdProperty()).thenReturn(true);
when(anotherProperty.getName()).thenReturn("another");

entity.addPersistentProperty(property);
exception.expect(MappingException.class);
Expand Down Expand Up @@ -406,7 +409,7 @@ static class PersistableEntity implements Persistable<Long> {

private final Long id = 42L;

/*
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Persistable#getId()
*/
Expand All @@ -415,7 +418,7 @@ public Long getId() {
return 4711L;
}

/*
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Persistable#isNew()
*/
Expand Down