Skip to content

Commit 7d32223

Browse files
committed
DATACMNS-1364 - Store persistent properties in HashMap.
We now use HashMap to store persistent properties of a PersistentEntity. An entity is built in a single thread so no concurrent modification happens. Concurrent reads may happen during entity usage which is fine as the PersistentEntity is not changed anymore. Previously, we used ConcurrentReferenceHashMap defaulting to soft references. Soft references can be cleared at the discretion of the GC in response to memory demand. So a default ConcurrentReferenceHashMap is memory-sensitive and acts like a cache with memory-based eviction rules. Persistent properties are not subject to be cached but elements of a PersistentEntity and cannot be recovered once cleared. Original pull request: #304.
1 parent 13b1150 commit 7d32223

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

Diff for: src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public BasicPersistentEntity(TypeInformation<T> information, @Nullable Comparato
104104
this.constructor = PreferredConstructorDiscoverer.discover(this);
105105
this.associations = comparator == null ? new HashSet<>() : new TreeSet<>(new AssociationComparator<>(comparator));
106106

107-
this.propertyCache = new ConcurrentReferenceHashMap<>();
107+
this.propertyCache = new HashMap<>();
108108
this.annotationCache = new ConcurrentReferenceHashMap<>();
109109
this.propertyAnnotationCache = CollectionUtils.toMultiValueMap(new ConcurrentReferenceHashMap<>());
110110
this.propertyAccessorFactory = BeanWrapperPropertyAccessorFactory.INSTANCE;
@@ -236,7 +236,7 @@ public void addPersistentProperty(P property) {
236236
}
237237
}
238238

239-
/*
239+
/*
240240
* (non-Javadoc)
241241
* @see org.springframework.data.mapping.model.MutablePersistentEntity#setEvaluationContextProvider(org.springframework.data.spel.EvaluationContextProvider)
242242
*/
@@ -469,7 +469,7 @@ public IdentifierAccessor getIdentifierAccessor(Object bean) {
469469
return hasIdProperty() ? new IdPropertyIdentifierAccessor(this, bean) : new AbsentIdentifierAccessor(bean);
470470
}
471471

472-
/*
472+
/*
473473
* (non-Javadoc)
474474
* @see org.springframework.data.mapping.PersistentEntity#isNew(java.lang.Object)
475475
*/
@@ -481,7 +481,7 @@ public boolean isNew(Object bean) {
481481
return isNewStrategy.get().isNew(bean);
482482
}
483483

484-
/*
484+
/*
485485
* (non-Javadoc)
486486
* @see org.springframework.data.mapping.PersistentEntity#isImmutable()
487487
*/
@@ -490,7 +490,7 @@ public boolean isImmutable() {
490490
return isImmutable.get();
491491
}
492492

493-
/*
493+
/*
494494
* (non-Javadoc)
495495
* @see org.springframework.data.mapping.PersistentEntity#requiresPropertyPopulation()
496496
*/
@@ -516,7 +516,7 @@ protected EvaluationContext getEvaluationContext(Object rootObject) {
516516
* Returns the default {@link IsNewStrategy} to be used. Will be a {@link PersistentEntityIsNewStrategy} by default.
517517
* Note, that this strategy only gets used if the entity doesn't implement {@link Persistable} as this indicates the
518518
* user wants to be in control over whether an entity is new or not.
519-
*
519+
*
520520
* @return
521521
* @since 2.1
522522
*/
@@ -526,7 +526,7 @@ protected IsNewStrategy getFallbackIsNewStrategy() {
526526

527527
/**
528528
* Verifies the given bean type to no be {@literal null} and of the type of the current {@link PersistentEntity}.
529-
*
529+
*
530530
* @param bean must not be {@literal null}.
531531
*/
532532
private final void verifyBeanType(Object bean) {

Diff for: src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,27 @@ public void considersComparatorForPropertyOrder() {
138138
assertThat(entity.getPersistentProperty("ssn")).isEqualTo(iterator.next());
139139
}
140140

141-
@Test // DATACMNS-186
141+
@Test // DATACMNS-18, DATACMNS-1364
142142
public void addingAndIdPropertySetsIdPropertyInternally() {
143143

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

147+
when(property.getName()).thenReturn("id");
147148
when(property.isIdProperty()).thenReturn(true);
148149
entity.addPersistentProperty(property);
149150
assertThat(entity.getIdProperty()).isEqualTo(property);
150151
}
151152

152-
@Test // DATACMNS-186
153+
@Test // DATACMNS-186, DATACMNS-1364
153154
public void rejectsIdPropertyIfAlreadySet() {
154155

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

158+
when(property.getName()).thenReturn("id");
157159
when(property.isIdProperty()).thenReturn(true);
158160
when(anotherProperty.isIdProperty()).thenReturn(true);
161+
when(anotherProperty.getName()).thenReturn("another");
159162

160163
entity.addPersistentProperty(property);
161164
exception.expect(MappingException.class);
@@ -429,7 +432,7 @@ static class PersistableEntity implements Persistable<Long> {
429432

430433
private final Long id = 42L;
431434

432-
/*
435+
/*
433436
* (non-Javadoc)
434437
* @see org.springframework.data.domain.Persistable#getId()
435438
*/
@@ -438,7 +441,7 @@ public Long getId() {
438441
return 4711L;
439442
}
440443

441-
/*
444+
/*
442445
* (non-Javadoc)
443446
* @see org.springframework.data.domain.Persistable#isNew()
444447
*/

0 commit comments

Comments
 (0)