Skip to content

Commit 49f536e

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 61540f7 commit 49f536e

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
@@ -59,6 +59,8 @@
5959
* @param P the concrete {@link PersistentProperty} type the {@link MappingContext} implementation creates
6060
* @author Jon Brisbin <jbrisbin@vmware.com>
6161
* @author Oliver Gierke
62+
* @author Michael Hunger
63+
* @author Thomas Darimont
6264
*/
6365
public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?, P>, P extends PersistentProperty<P>>
6466
implements MappingContext<E, P>, ApplicationEventPublisherAware, InitializingBean {
@@ -157,14 +159,14 @@ public E getPersistentEntity(TypeInformation<?> type) {
157159
read.unlock();
158160
}
159161

160-
if (strict) {
161-
throw new MappingException("Unknown persistent entity " + type);
162-
}
163-
164162
if (!shouldCreatePersistentEntityFor(type)) {
165163
return null;
166164
}
167165

166+
if (strict) {
167+
throw new MappingException("Unknown persistent entity " + type);
168+
}
169+
168170
return addPersistentEntity(type);
169171
}
170172

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

@@ -191,6 +192,16 @@ public void exposesCopyOfPersistentEntitiesToAvoidConcurrentModificationExceptio
191192
}
192193
}
193194

195+
/**
196+
* @see DATACMNS-447
197+
*/
198+
@Test
199+
public void shouldReturnNullForSimpleTypesIfInStrictIsEnabled() {
200+
201+
context.setStrict(true);
202+
assertThat(context.getPersistentEntity(Integer.class), is(nullValue()));
203+
}
204+
194205
class Person {
195206
String name;
196207
}

0 commit comments

Comments
 (0)