Skip to content

Commit dbb7c35

Browse files
committed
DATACMNS-462 - AbstractPersistentProperty considers collections and maps entities now.
Removed the rejecting check for collections and maps and solely rely on the evaluation of the actual type for the ….isEntity() decision in AbstractPersistentProperty. This will cause collection and map properties also being inspected for persistent types as soon as the owner type gets added to the mapping context.
1 parent 0464d93 commit dbb7c35

File tree

3 files changed

+84
-21
lines changed

3 files changed

+84
-21
lines changed

src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public boolean isEntity() {
257257

258258
TypeInformation<?> actualType = information.getActualType();
259259
boolean isComplexType = actualType == null ? false : !simpleTypeHolder.isSimpleType(actualType.getType());
260-
return isComplexType && !isTransient() && !isCollectionLike() && !isMap();
260+
return isComplexType && !isTransient();
261261
}
262262

263263
/*

src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.mockito.Mockito.*;
2121
import groovy.lang.MetaClass;
2222

23+
import java.util.Collection;
2324
import java.util.Collections;
2425
import java.util.Iterator;
2526
import java.util.List;
@@ -224,6 +225,23 @@ public void shouldReturnNullForSimpleTypesIfInStrictIsEnabled() {
224225
assertThat(context.getPersistentEntity(Integer.class), is(nullValue()));
225226
}
226227

228+
/**
229+
* @see DATACMNS-462
230+
*/
231+
@Test
232+
public void hasPersistentEntityForCollectionPropertiesAfterInitialization() {
233+
234+
context.getPersistentEntity(Sample.class);
235+
236+
for (BasicPersistentEntity<Object, SamplePersistentProperty> entity : context.getPersistentEntities()) {
237+
if (entity.getType().equals(Person.class)) {
238+
return;
239+
}
240+
}
241+
242+
fail("Expected to find persistent entity for Person!");
243+
}
244+
227245
class Person {
228246
String name;
229247
}

src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2013 the original author or authors.
2+
* Copyright 2011-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,6 +51,7 @@ public class AbstractPersistentPropertyUnitTests {
5151

5252
@Before
5353
public void setUp() {
54+
5455
typeInfo = ClassTypeInformation.from(TestClassComplex.class);
5556
entity = new BasicPersistentEntity<TestClassComplex, SamplePersistentProperty>(typeInfo);
5657
typeHolder = new SimpleTypeHolder();
@@ -68,6 +69,9 @@ public void discoversComponentTypeCorrectly() throws Exception {
6869
property.getComponentType();
6970
}
7071

72+
/**
73+
* @see DATACMNS-101
74+
*/
7175
@Test
7276
public void returnsNestedEntityTypeCorrectly() {
7377

@@ -197,25 +201,6 @@ public void returnsNullGetterAndSetterIfNoPropertyDescriptorGiven() {
197201
assertThat(property.getSetter(), is(nullValue()));
198202
}
199203

200-
private static PropertyDescriptor getPropertyDescriptor(Class<?> type, String propertyName) {
201-
202-
try {
203-
204-
BeanInfo info = Introspector.getBeanInfo(type);
205-
206-
for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) {
207-
if (descriptor.getName().equals(propertyName)) {
208-
return descriptor;
209-
}
210-
}
211-
212-
return null;
213-
214-
} catch (IntrospectionException e) {
215-
return null;
216-
}
217-
}
218-
219204
/**
220205
* @see DATACMNS-337
221206
*/
@@ -235,6 +220,46 @@ public void resolvesActualType() {
235220
assertThat(property.getActualType(), is((Object) Person.class));
236221
}
237222

223+
/**
224+
* @see DATACMNS-462
225+
*/
226+
@Test
227+
public void considersCollectionPropertyEntitiesIfComponentTypeIsEntity() {
228+
229+
SamplePersistentProperty property = getProperty(Sample.class, "persons");
230+
assertThat(property.isEntity(), is(true));
231+
}
232+
233+
/**
234+
* @see DATACMNS-462
235+
*/
236+
@Test
237+
public void considersMapPropertyEntitiesIfValueTypeIsEntity() {
238+
239+
SamplePersistentProperty property = getProperty(Sample.class, "personMap");
240+
assertThat(property.isEntity(), is(true));
241+
}
242+
243+
/**
244+
* @see DATACMNS-462
245+
*/
246+
@Test
247+
public void considersArrayPropertyEntitiesIfComponentTypeIsEntity() {
248+
249+
SamplePersistentProperty property = getProperty(Sample.class, "personArray");
250+
assertThat(property.isEntity(), is(true));
251+
}
252+
253+
/**
254+
* @see DATACMNS-462
255+
*/
256+
@Test
257+
public void considersCollectionPropertySimpleIfComponentTypeIsSimple() {
258+
259+
SamplePersistentProperty property = getProperty(Sample.class, "strings");
260+
assertThat(property.isEntity(), is(false));
261+
}
262+
238263
private <T> SamplePersistentProperty getProperty(Class<T> type, String name) {
239264

240265
BasicPersistentEntity<T, SamplePersistentProperty> entity = new BasicPersistentEntity<T, SamplePersistentProperty>(
@@ -244,6 +269,25 @@ private <T> SamplePersistentProperty getProperty(Class<T> type, String name) {
244269
return new SamplePersistentProperty(field, null, entity, typeHolder);
245270
}
246271

272+
private static PropertyDescriptor getPropertyDescriptor(Class<?> type, String propertyName) {
273+
274+
try {
275+
276+
BeanInfo info = Introspector.getBeanInfo(type);
277+
278+
for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) {
279+
if (descriptor.getName().equals(propertyName)) {
280+
return descriptor;
281+
}
282+
}
283+
284+
return null;
285+
286+
} catch (IntrospectionException e) {
287+
return null;
288+
}
289+
}
290+
247291
class Generic<T> {
248292
T genericField;
249293

@@ -345,5 +389,6 @@ static class Sample {
345389
Collection<Person> persons;
346390
Person[] personArray;
347391
Map<String, Person> personMap;
392+
Collection<String> strings;
348393
}
349394
}

0 commit comments

Comments
 (0)