|
50 | 50 | import org.springframework.data.mapping.MappingException;
|
51 | 51 | import org.springframework.data.mapping.PersistentPropertyAccessor;
|
52 | 52 | import org.springframework.data.mapping.PreferredConstructor;
|
| 53 | +import org.springframework.data.mapping.SimplePropertyHandler; |
53 | 54 | import org.springframework.data.mapping.context.MappingContext;
|
54 | 55 | import org.springframework.data.mapping.model.*;
|
55 | 56 | import org.springframework.data.util.ClassTypeInformation;
|
@@ -354,7 +355,7 @@ private <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String, Ob
|
354 | 355 |
|
355 | 356 | if (source instanceof SearchDocument) {
|
356 | 357 | SearchDocument searchDocument = (SearchDocument) source;
|
357 |
| - populateScriptFields(result, searchDocument); |
| 358 | + populateScriptFields(targetEntity, result, searchDocument); |
358 | 359 | }
|
359 | 360 |
|
360 | 361 | return result;
|
@@ -555,27 +556,17 @@ private Object getPotentiallyConvertedSimpleRead(@Nullable Object value, @Nullab
|
555 | 556 | return conversionService.convert(value, target);
|
556 | 557 | }
|
557 | 558 |
|
558 |
| - private <T> void populateScriptFields(T result, SearchDocument searchDocument) { |
| 559 | + private <T> void populateScriptFields(ElasticsearchPersistentEntity<?> entity, T result, SearchDocument searchDocument) { |
559 | 560 | Map<String, List<Object>> fields = searchDocument.getFields();
|
560 |
| - if (!fields.isEmpty()) { |
561 |
| - for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) { |
562 |
| - ScriptedField scriptedField = field.getAnnotation(ScriptedField.class); |
563 |
| - if (scriptedField != null) { |
564 |
| - String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name(); |
565 |
| - Object value = searchDocument.getFieldValue(name); |
566 |
| - if (value != null) { |
567 |
| - field.setAccessible(true); |
568 |
| - try { |
569 |
| - field.set(result, value); |
570 |
| - } catch (IllegalArgumentException e) { |
571 |
| - throw new MappingException("failed to set scripted field: " + name + " with value: " + value, e); |
572 |
| - } catch (IllegalAccessException e) { |
573 |
| - throw new MappingException("failed to access scripted field: " + name, e); |
574 |
| - } |
575 |
| - } |
576 |
| - } |
| 561 | + entity.doWithProperties((SimplePropertyHandler) property -> { |
| 562 | + if (property.isAnnotationPresent(ScriptedField.class) && fields.containsKey(property.getName())) { |
| 563 | + ScriptedField scriptedField = property.findAnnotation(ScriptedField.class); |
| 564 | + String name = scriptedField.name().isEmpty() ? property.getName() : scriptedField.name(); |
| 565 | + Object value = searchDocument.getFieldValue(name); |
| 566 | + |
| 567 | + entity.getPropertyAccessor(result).setProperty(property, value); |
577 | 568 | }
|
578 |
| - } |
| 569 | + }); |
579 | 570 | }
|
580 | 571 |
|
581 | 572 | /**
|
|
0 commit comments