Skip to content

Commit 01105d2

Browse files
committed
#369 - Fall back to column name in QueryMapper if path expression maps into simple type property.
We now fall back to the column name when resolving a property path expression that maps into a simple-typed property. PropertyPath.from(…) fails internally as it attempts to look up a simple type from the MappingContext and this fails for primitive and simple types.
1 parent 7cfac09 commit 01105d2

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/main/java/org/springframework/data/r2dbc/query/QueryMapper.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
import java.util.HashMap;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.regex.Pattern;
2324

2425
import org.springframework.data.domain.Sort;
26+
import org.springframework.data.mapping.MappingException;
2527
import org.springframework.data.mapping.PersistentPropertyPath;
2628
import org.springframework.data.mapping.PropertyPath;
2729
import org.springframework.data.mapping.PropertyReferenceException;
28-
import org.springframework.data.mapping.context.InvalidPersistentPropertyPath;
2930
import org.springframework.data.mapping.context.MappingContext;
3031
import org.springframework.data.r2dbc.convert.R2dbcConverter;
3132
import org.springframework.data.r2dbc.dialect.BindMarker;
@@ -626,7 +627,7 @@ protected static class MetadataBackedField extends Field {
626627

627628
private final RelationalPersistentEntity<?> entity;
628629
private final MappingContext<? extends RelationalPersistentEntity<?>, RelationalPersistentProperty> mappingContext;
629-
private final RelationalPersistentProperty property;
630+
private final @Nullable RelationalPersistentProperty property;
630631
private final @Nullable PersistentPropertyPath<RelationalPersistentProperty> path;
631632

632633
/**
@@ -675,26 +676,35 @@ public SqlIdentifier getMappedColumnName() {
675676
/**
676677
* Returns the {@link PersistentPropertyPath} for the given {@code pathExpression}.
677678
*
678-
* @param pathExpression
679+
* @param pathExpression the path expression to use.
679680
* @return
680681
*/
681682
@Nullable
682683
private PersistentPropertyPath<RelationalPersistentProperty> getPath(String pathExpression) {
683684

684685
try {
685686

686-
PropertyPath path = PropertyPath.from(pathExpression, this.entity.getTypeInformation());
687+
PropertyPath path = forName(pathExpression);
687688

688689
if (isPathToJavaLangClassProperty(path)) {
689690
return null;
690691
}
691692

692693
return this.mappingContext.getPersistentPropertyPath(path);
693-
} catch (PropertyReferenceException | InvalidPersistentPropertyPath e) {
694+
} catch (MappingException | PropertyReferenceException e) {
694695
return null;
695696
}
696697
}
697698

699+
private PropertyPath forName(String path) {
700+
701+
if (entity.getPersistentProperty(path) != null) {
702+
return PropertyPath.from(Pattern.quote(path), entity.getTypeInformation());
703+
}
704+
705+
return PropertyPath.from(path, entity.getTypeInformation());
706+
}
707+
698708
private boolean isPathToJavaLangClassProperty(PropertyPath path) {
699709
return path.getType().equals(Class.class) && path.getLeafProperty().getOwningType().getType().equals(Class.class);
700710
}

src/test/java/org/springframework/data/r2dbc/query/QueryMapperUnitTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,25 @@ public void shouldMapSort() {
369369
assertThat(mapped.getOrderFor("alternative")).isNull();
370370
}
371371

372+
@Test // gh-369
373+
public void mapSortForPropertyPathInPrimitiveShouldFallBackToColumnName() {
374+
375+
Sort sort = Sort.by(desc("alternative_name"));
376+
377+
Sort mapped = mapper.getMappedObject(sort, context.getRequiredPersistentEntity(Person.class));
378+
assertThat(mapped.getOrderFor("alternative_name")).isEqualTo(desc("alternative_name"));
379+
}
380+
381+
@Test // gh-369
382+
public void mapQueryForPropertyPathInPrimitiveShouldFallBackToColumnName() {
383+
384+
Criteria criteria = Criteria.where("alternative_name").is("a");
385+
386+
BoundCondition bindings = map(criteria);
387+
388+
assertThat(bindings.getCondition().toString()).isEqualTo("person.alternative_name = ?[$1]");
389+
}
390+
372391
private BoundCondition map(Criteria criteria) {
373392

374393
BindMarkersFactory markers = BindMarkersFactory.indexed("$", 1);

0 commit comments

Comments
 (0)