|
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);
|
@@ -673,7 +678,8 @@ private Object createReferenceFor(Object source, MongoPersistentProperty propert
|
673 | 678 | return (DBRef) source;
|
674 | 679 | }
|
675 | 680 |
|
676 |
| - if(property != null && (property.isDocumentReference() || (!property.isDbReference() && property.findAnnotation(Reference.class) != null))) { |
| 681 | + if (property != null && (property.isDocumentReference() |
| 682 | + || (!property.isDbReference() && property.findAnnotation(Reference.class) != null))) { |
677 | 683 | return converter.toDocumentPointer(source, property).getPointer();
|
678 | 684 | }
|
679 | 685 |
|
@@ -1174,38 +1180,55 @@ private PersistentPropertyPath<MongoPersistentProperty> getPath(String pathExpre
|
1174 | 1180 | removePlaceholders(DOT_POSITIONAL_PATTERN, pathExpression));
|
1175 | 1181 |
|
1176 | 1182 | if (sourceProperty != null && sourceProperty.getOwner().equals(entity)) {
|
1177 |
| - return mappingContext |
1178 |
| - .getPersistentPropertyPath(PropertyPath.from(Pattern.quote(sourceProperty.getName()), entity.getTypeInformation())); |
| 1183 | + return mappingContext.getPersistentPropertyPath( |
| 1184 | + PropertyPath.from(Pattern.quote(sourceProperty.getName()), entity.getTypeInformation())); |
1179 | 1185 | }
|
1180 | 1186 |
|
1181 | 1187 | PropertyPath path = forName(rawPath);
|
1182 | 1188 | if (path == null || isPathToJavaLangClassProperty(path)) {
|
1183 | 1189 | return null;
|
1184 | 1190 | }
|
1185 | 1191 |
|
1186 |
| - try { |
| 1192 | + PersistentPropertyPath<MongoPersistentProperty> propertyPath = tryToResolvePersistentPropertyPath(path); |
1187 | 1193 |
|
1188 |
| - PersistentPropertyPath<MongoPersistentProperty> propertyPath = mappingContext.getPersistentPropertyPath(path); |
| 1194 | + if (propertyPath == null) { |
1189 | 1195 |
|
1190 |
| - Iterator<MongoPersistentProperty> iterator = propertyPath.iterator(); |
1191 |
| - boolean associationDetected = false; |
| 1196 | + if (QueryMapper.LOGGER.isInfoEnabled()) { |
| 1197 | + |
| 1198 | + String types = StringUtils.collectionToDelimitedString( |
| 1199 | + path.stream().map(it -> it.getType().getSimpleName()).collect(Collectors.toList()), " -> "); |
| 1200 | + QueryMapper.LOGGER.info( |
| 1201 | + "Could not map '{}'. Maybe a fragment in '{}' is considered a simple type. Mapper continues with {}.", |
| 1202 | + path, types, pathExpression); |
| 1203 | + } |
| 1204 | + return null; |
| 1205 | + } |
1192 | 1206 |
|
1193 |
| - while (iterator.hasNext()) { |
| 1207 | + Iterator<MongoPersistentProperty> iterator = propertyPath.iterator(); |
| 1208 | + boolean associationDetected = false; |
1194 | 1209 |
|
1195 |
| - MongoPersistentProperty property = iterator.next(); |
| 1210 | + while (iterator.hasNext()) { |
1196 | 1211 |
|
1197 |
| - if (property.isAssociation()) { |
1198 |
| - associationDetected = true; |
1199 |
| - continue; |
1200 |
| - } |
| 1212 | + MongoPersistentProperty property = iterator.next(); |
1201 | 1213 |
|
1202 |
| - if (associationDetected && !property.isIdProperty()) { |
1203 |
| - throw new MappingException(String.format(INVALID_ASSOCIATION_REFERENCE, pathExpression)); |
1204 |
| - } |
| 1214 | + if (property.isAssociation()) { |
| 1215 | + associationDetected = true; |
| 1216 | + continue; |
1205 | 1217 | }
|
1206 | 1218 |
|
1207 |
| - return propertyPath; |
1208 |
| - } catch (InvalidPersistentPropertyPath e) { |
| 1219 | + if (associationDetected && !property.isIdProperty()) { |
| 1220 | + throw new MappingException(String.format(INVALID_ASSOCIATION_REFERENCE, pathExpression)); |
| 1221 | + } |
| 1222 | + } |
| 1223 | + |
| 1224 | + return propertyPath; |
| 1225 | + } |
| 1226 | + |
| 1227 | + private PersistentPropertyPath<MongoPersistentProperty> tryToResolvePersistentPropertyPath(PropertyPath path) { |
| 1228 | + |
| 1229 | + try { |
| 1230 | + return mappingContext.getPersistentPropertyPath(path); |
| 1231 | + } catch (MappingException e) { |
1209 | 1232 | return null;
|
1210 | 1233 | }
|
1211 | 1234 | }
|
|
0 commit comments