Skip to content

Commit 0464d93

Browse files
jexpodrotbohm
authored andcommitted
DATACMNS-447 - Soften too strict strickt-check in MappingContext.
Previously we directly threw a MappingException in MappingContext.getPersistentEntity(TypeInformation) when the MappingContext was in strict mode and we were given a TypeInformation for which we didn't have a PersistentEntity already. We now first check whether we should actually create a PersistentEntity for the given TypeInformation if no PersistentEntity is available, before throwing a MappingException - if we should not then we simply return null (e.g. for simple types like Integer or Double). Original pull requests: #66, #71.
1 parent 4a3af27 commit 0464d93

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
* @param P the concrete {@link PersistentProperty} type the {@link MappingContext} implementation creates
6262
* @author Jon Brisbin <jbrisbin@vmware.com>
6363
* @author Oliver Gierke
64+
* @author Michael Hunger
65+
* @author Thomas Darimont
6466
*/
6567
public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?, P>, P extends PersistentProperty<P>>
6668
implements MappingContext<E, P>, ApplicationEventPublisherAware, InitializingBean {
@@ -159,14 +161,14 @@ public E getPersistentEntity(TypeInformation<?> type) {
159161
read.unlock();
160162
}
161163

162-
if (strict) {
163-
throw new MappingException("Unknown persistent entity " + type);
164-
}
165-
166164
if (!shouldCreatePersistentEntityFor(type)) {
167165
return null;
168166
}
169167

168+
if (strict) {
169+
throw new MappingException("Unknown persistent entity " + type);
170+
}
171+
170172
return addPersistentEntity(type);
171173
}
172174

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2012 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.
@@ -42,6 +42,7 @@
4242
* Unit test for {@link AbstractMappingContext}.
4343
*
4444
* @author Oliver Gierke
45+
* @author Thomas Darimont
4546
*/
4647
public class AbstractMappingContextUnitTests {
4748

@@ -213,6 +214,16 @@ public void exposesCopyOfPersistentEntitiesToAvoidConcurrentModificationExceptio
213214
}
214215
}
215216

217+
/**
218+
* @see DATACMNS-447
219+
*/
220+
@Test
221+
public void shouldReturnNullForSimpleTypesIfInStrictIsEnabled() {
222+
223+
context.setStrict(true);
224+
assertThat(context.getPersistentEntity(Integer.class), is(nullValue()));
225+
}
226+
216227
class Person {
217228
String name;
218229
}

0 commit comments

Comments
 (0)