diff --git a/src/main/java/org/springframework/data/couchbase/core/convert/DefaultCouchbaseTypeMapper.java b/src/main/java/org/springframework/data/couchbase/core/convert/DefaultCouchbaseTypeMapper.java index 3600044e..91749a83 100644 --- a/src/main/java/org/springframework/data/couchbase/core/convert/DefaultCouchbaseTypeMapper.java +++ b/src/main/java/org/springframework/data/couchbase/core/convert/DefaultCouchbaseTypeMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors + * Copyright 2012-2021 the original author or authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,13 @@ package org.springframework.data.couchbase.core.convert; +import java.util.Collections; + import org.springframework.data.convert.DefaultTypeMapper; import org.springframework.data.convert.TypeAliasAccessor; import org.springframework.data.couchbase.core.mapping.CouchbaseDocument; import org.springframework.data.mapping.Alias; +import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.util.TypeInformation; /** @@ -27,6 +30,7 @@ import org.springframework.data.util.TypeInformation; * * @author Michael Nitschinger * @author Mark Paluch + * @author Michael Reiche */ public class DefaultCouchbaseTypeMapper extends DefaultTypeMapper implements CouchbaseTypeMapper { @@ -43,7 +47,8 @@ public class DefaultCouchbaseTypeMapper extends DefaultTypeMapper type) { + TypeAlias[] typeAlias = type.getType().getAnnotationsByType(TypeAlias.class); + + if (typeAlias.length == 1) { + return Alias.of(typeAlias[0].value()); + } + + return super.createAliasFor(type); + } +} diff --git a/src/main/java/org/springframework/data/couchbase/repository/query/StringN1qlQueryCreator.java b/src/main/java/org/springframework/data/couchbase/repository/query/StringN1qlQueryCreator.java index 1e7d8e29..65f38996 100644 --- a/src/main/java/org/springframework/data/couchbase/repository/query/StringN1qlQueryCreator.java +++ b/src/main/java/org/springframework/data/couchbase/repository/query/StringN1qlQueryCreator.java @@ -27,6 +27,7 @@ import org.springframework.data.couchbase.core.query.Query; import org.springframework.data.couchbase.core.query.QueryCriteria; import org.springframework.data.couchbase.core.query.StringQuery; import org.springframework.data.domain.Sort; +import org.springframework.data.mapping.Alias; import org.springframework.data.mapping.PersistentPropertyPath; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.repository.core.NamedQueries; @@ -36,6 +37,8 @@ import org.springframework.data.repository.query.QueryMethodEvaluationContextPro import org.springframework.data.repository.query.parser.AbstractQueryCreator; import org.springframework.data.repository.query.parser.Part; import org.springframework.data.repository.query.parser.PartTree; +import org.springframework.data.util.ClassTypeInformation; +import org.springframework.data.util.TypeInformation; import org.springframework.expression.spel.standard.SpelExpressionParser; import com.couchbase.client.java.json.JsonArray; @@ -81,8 +84,15 @@ public class StringN1qlQueryCreator extends AbstractQueryCreator typeInfo = ClassTypeInformation.from(javaType); + Alias alias = couchbaseConverter.getTypeAlias(typeInfo); + if (alias != null && alias.isPresent()) { + typeValue = alias.toString(); + } this.queryParser = new StringBasedN1qlQueryParser(queryString, queryMethod, bucketName, couchbaseConverter, - getTypeField(), getTypeValue(), accessor, spelExpressionParser, evaluationContextProvider); + getTypeField(), typeValue, accessor, spelExpressionParser, evaluationContextProvider); this.parser = spelExpressionParser; this.parsedExpression = this.queryParser.parsedExpression; } @@ -95,8 +105,8 @@ public class StringN1qlQueryCreator extends AbstractQueryCreator airports = couchbaseTemplate.findByQuery(Airport.class) + .matching(new Query(QueryCriteria.where(N1QLExpression.x("_class")).is("airport"))).all(); + assertFalse(airports.isEmpty(), "should have found aiport"); + } finally { + airportRepository.delete(vie); + } + } + @Test void findByEnum() { Airport vie = null; @@ -204,6 +220,7 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr airportRepository.saveAll( Arrays.stream(iatas).map((iata) -> new Airport("airports::" + iata, iata, iata.toLowerCase(Locale.ROOT))) .collect(Collectors.toSet())); + couchbaseTemplate.findByQuery(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all(); Long count = airportRepository.countFancyExpression(asList("JFK"), asList("jfk"), false); assertEquals(1, count);