|
19 | 19 | import java.util.Map.Entry;
|
20 | 20 | import java.util.regex.Matcher;
|
21 | 21 | import java.util.regex.Pattern;
|
| 22 | +import java.util.stream.Collectors; |
22 | 23 |
|
23 | 24 | import org.bson.BsonValue;
|
24 | 25 | import org.bson.Document;
|
25 | 26 | import org.bson.conversions.Bson;
|
26 | 27 | import org.bson.types.ObjectId;
|
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
27 | 30 | import org.springframework.core.convert.ConversionService;
|
28 | 31 | import org.springframework.core.convert.converter.Converter;
|
29 | 32 | import org.springframework.data.annotation.Reference;
|
|
69 | 72 | */
|
70 | 73 | public class QueryMapper {
|
71 | 74 |
|
| 75 | + protected static final Logger LOGGER = LoggerFactory.getLogger(QueryMapper.class); |
| 76 | + |
72 | 77 | private static final List<String> DEFAULT_ID_NAMES = Arrays.asList("id", "_id");
|
73 | 78 | private static final Document META_TEXT_SCORE = new Document("$meta", "textScore");
|
74 | 79 | static final ClassTypeInformation<?> NESTED_DOCUMENT = ClassTypeInformation.from(NestedDocument.class);
|
@@ -677,7 +682,8 @@ private Object createReferenceFor(Object source, MongoPersistentProperty propert
|
677 | 682 | return (DBRef) source;
|
678 | 683 | }
|
679 | 684 |
|
680 |
| - if(property != null && (property.isDocumentReference() || (!property.isDbReference() && property.findAnnotation(Reference.class) != null))) { |
| 685 | + if (property != null && (property.isDocumentReference() |
| 686 | + || (!property.isDbReference() && property.findAnnotation(Reference.class) != null))) { |
681 | 687 | return converter.toDocumentPointer(source, property).getPointer();
|
682 | 688 | }
|
683 | 689 |
|
@@ -1178,38 +1184,55 @@ private PersistentPropertyPath<MongoPersistentProperty> getPath(String pathExpre
|
1178 | 1184 | removePlaceholders(DOT_POSITIONAL_PATTERN, pathExpression));
|
1179 | 1185 |
|
1180 | 1186 | if (sourceProperty != null && sourceProperty.getOwner().equals(entity)) {
|
1181 |
| - return mappingContext |
1182 |
| - .getPersistentPropertyPath(PropertyPath.from(Pattern.quote(sourceProperty.getName()), entity.getTypeInformation())); |
| 1187 | + return mappingContext.getPersistentPropertyPath( |
| 1188 | + PropertyPath.from(Pattern.quote(sourceProperty.getName()), entity.getTypeInformation())); |
1183 | 1189 | }
|
1184 | 1190 |
|
1185 | 1191 | PropertyPath path = forName(rawPath);
|
1186 | 1192 | if (path == null || isPathToJavaLangClassProperty(path)) {
|
1187 | 1193 | return null;
|
1188 | 1194 | }
|
1189 | 1195 |
|
1190 |
| - try { |
| 1196 | + PersistentPropertyPath<MongoPersistentProperty> propertyPath = tryToResolvePersistentPropertyPath(path); |
1191 | 1197 |
|
1192 |
| - PersistentPropertyPath<MongoPersistentProperty> propertyPath = mappingContext.getPersistentPropertyPath(path); |
| 1198 | + if (propertyPath == null) { |
1193 | 1199 |
|
1194 |
| - Iterator<MongoPersistentProperty> iterator = propertyPath.iterator(); |
1195 |
| - boolean associationDetected = false; |
| 1200 | + if (QueryMapper.LOGGER.isInfoEnabled()) { |
| 1201 | + |
| 1202 | + String types = StringUtils.collectionToDelimitedString( |
| 1203 | + path.stream().map(it -> it.getType().getSimpleName()).collect(Collectors.toList()), " -> "); |
| 1204 | + QueryMapper.LOGGER.info( |
| 1205 | + "Could not map '{}'. Maybe a fragment in '{}' is considered a simple type. Mapper continues with {}.", |
| 1206 | + path, types, pathExpression); |
| 1207 | + } |
| 1208 | + return null; |
| 1209 | + } |
1196 | 1210 |
|
1197 |
| - while (iterator.hasNext()) { |
| 1211 | + Iterator<MongoPersistentProperty> iterator = propertyPath.iterator(); |
| 1212 | + boolean associationDetected = false; |
1198 | 1213 |
|
1199 |
| - MongoPersistentProperty property = iterator.next(); |
| 1214 | + while (iterator.hasNext()) { |
1200 | 1215 |
|
1201 |
| - if (property.isAssociation()) { |
1202 |
| - associationDetected = true; |
1203 |
| - continue; |
1204 |
| - } |
| 1216 | + MongoPersistentProperty property = iterator.next(); |
1205 | 1217 |
|
1206 |
| - if (associationDetected && !property.isIdProperty()) { |
1207 |
| - throw new MappingException(String.format(INVALID_ASSOCIATION_REFERENCE, pathExpression)); |
1208 |
| - } |
| 1218 | + if (property.isAssociation()) { |
| 1219 | + associationDetected = true; |
| 1220 | + continue; |
1209 | 1221 | }
|
1210 | 1222 |
|
1211 |
| - return propertyPath; |
1212 |
| - } catch (InvalidPersistentPropertyPath e) { |
| 1223 | + if (associationDetected && !property.isIdProperty()) { |
| 1224 | + throw new MappingException(String.format(INVALID_ASSOCIATION_REFERENCE, pathExpression)); |
| 1225 | + } |
| 1226 | + } |
| 1227 | + |
| 1228 | + return propertyPath; |
| 1229 | + } |
| 1230 | + |
| 1231 | + private PersistentPropertyPath<MongoPersistentProperty> tryToResolvePersistentPropertyPath(PropertyPath path) { |
| 1232 | + |
| 1233 | + try { |
| 1234 | + return mappingContext.getPersistentPropertyPath(path); |
| 1235 | + } catch (MappingException e) { |
1213 | 1236 | return null;
|
1214 | 1237 | }
|
1215 | 1238 | }
|
|
0 commit comments