Skip to content

Commit 642be86

Browse files
committed
Restore projection of class. (#1319)
Restore projection of class in queries. This is necessary for abstract repositories. Closes #1315.
1 parent 26636d4 commit 642be86

File tree

10 files changed

+361
-97
lines changed

10 files changed

+361
-97
lines changed

src/main/java/org/springframework/data/couchbase/repository/query/StringBasedN1qlQueryParser.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,12 @@ public N1qlSpelValues createN1qlSpelValues(String bucketName, String collection,
162162
String b = collection != null ? collection : bucketName;
163163
Assert.isTrue(!(distinctFields != null && fields != null),
164164
"only one of project(fields) and distinct(distinctFields) can be specified");
165-
String entity = "META(" + i(b) + ").id AS " + SELECT_ID + ", META(" + i(b) + ").cas AS " + SELECT_CAS;
165+
String entity = "META(" + i(b) + ").id AS " + SELECT_ID + ", META(" + i(b) + ").cas AS " + SELECT_CAS + ", "
166+
+ i(typeField);
166167
String count = "COUNT(*) AS " + CountFragment.COUNT_ALIAS;
167168
String selectEntity;
168169
if (distinctFields != null) {
169-
String distinctFieldsStr = getProjectedOrDistinctFields(b, domainClass, fields, distinctFields);
170+
String distinctFieldsStr = getProjectedOrDistinctFields(b, domainClass, typeField, fields, distinctFields);
170171
if (isCount) {
171172
selectEntity = "SELECT COUNT( DISTINCT {" + distinctFieldsStr + "} ) " + CountFragment.COUNT_ALIAS + " FROM "
172173
+ i(b);
@@ -176,7 +177,7 @@ public N1qlSpelValues createN1qlSpelValues(String bucketName, String collection,
176177
} else if (isCount) {
177178
selectEntity = "SELECT " + count + " FROM " + i(b);
178179
} else {
179-
String projectedFields = getProjectedOrDistinctFields(b, domainClass, fields, distinctFields);
180+
String projectedFields = getProjectedOrDistinctFields(b, domainClass, typeField, fields, distinctFields);
180181
selectEntity = "SELECT " + entity + (!projectedFields.isEmpty() ? ", " : " ") + projectedFields + " FROM " + i(b);
181182
}
182183
String typeSelection = "`" + typeField + "` = \"" + typeValue + "\"";
@@ -187,22 +188,22 @@ public N1qlSpelValues createN1qlSpelValues(String bucketName, String collection,
187188
return new N1qlSpelValues(selectEntity, entity, i(b).toString(), typeSelection, delete, returning);
188189
}
189190

190-
private String getProjectedOrDistinctFields(String b, Class resultClass, String[] fields, String[] distinctFields) {
191+
private String getProjectedOrDistinctFields(String b, Class resultClass, String typeField, String[] fields, String[] distinctFields) {
191192
if (distinctFields != null && distinctFields.length != 0) {
192193
return i(distinctFields).toString();
193194
}
194195
String projectedFields = i(b) + ".*";
195196
if (resultClass != null) {
196197
PersistentEntity persistentEntity = couchbaseConverter.getMappingContext().getPersistentEntity(resultClass);
197198
StringBuilder sb = new StringBuilder();
198-
getProjectedFieldsInternal(b, null, sb, persistentEntity, fields, distinctFields != null);
199+
getProjectedFieldsInternal(b, null, sb, persistentEntity, typeField, fields, distinctFields != null);
199200
projectedFields = sb.toString();
200201
}
201202
return projectedFields;
202203
}
203204

204205
private void getProjectedFieldsInternal(String bucketName, CouchbasePersistentProperty parent, StringBuilder sb,
205-
PersistentEntity persistentEntity, String[] fields, boolean forDistinct) {
206+
PersistentEntity persistentEntity, String typeField, String[] fields, boolean forDistinct) {
206207

207208
if (persistentEntity != null) {
208209
Set<String> fieldList = fields != null ? new HashSet<>(Arrays.asList(fields)) : null;
@@ -216,6 +217,8 @@ private void getProjectedFieldsInternal(String bucketName, CouchbasePersistentPr
216217
if (prop == persistentEntity.getVersionProperty() && parent == null) {
217218
return;
218219
}
220+
if( prop.getFieldName().equals(typeField)) // typeField already projected
221+
return;
219222
// for distinct when no distinctFields were provided, do not include the expiration field.
220223
if (forDistinct && prop.findAnnotation(Expiration.class) != null && parent == null) {
221224
return;
@@ -253,8 +256,10 @@ private void getProjectedFieldsInternal(String bucketName, CouchbasePersistentPr
253256
}
254257
} else {
255258
for (String field : fields) {
256-
if (sb.length() > 0) {
257-
sb.append(", ");
259+
if (!field.equals(typeField)) { // typeField is already projected
260+
if (sb.length() > 0) {
261+
sb.append(", ");
262+
}
258263
}
259264
sb.append(x(field));
260265
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.couchbase.domain;
18+
19+
import java.util.Objects;
20+
21+
import org.springframework.data.annotation.CreatedBy;
22+
import org.springframework.data.annotation.CreatedDate;
23+
import org.springframework.data.annotation.Id;
24+
import org.springframework.data.annotation.LastModifiedBy;
25+
import org.springframework.data.annotation.LastModifiedDate;
26+
import org.springframework.data.annotation.PersistenceConstructor;
27+
import org.springframework.data.annotation.Transient;
28+
import org.springframework.data.annotation.Version;
29+
import org.springframework.data.couchbase.core.mapping.Document;
30+
31+
/**
32+
* User entity for tests
33+
*
34+
* @author Michael Reiche
35+
*/
36+
37+
public abstract class AbstractUser extends ComparableEntity {
38+
39+
@Version protected long version;
40+
@Id protected String id;
41+
protected String firstname;
42+
protected String lastname;
43+
@Transient protected String transientInfo;
44+
@CreatedBy protected String createdBy;
45+
@CreatedDate protected long createdDate;
46+
@LastModifiedBy protected String lastModifiedBy;
47+
@LastModifiedDate protected long lastModifiedDate;
48+
49+
public String getId() {
50+
return id;
51+
}
52+
53+
public String getFirstname() {
54+
return firstname;
55+
}
56+
57+
public String getLastname() {
58+
return lastname;
59+
}
60+
61+
public long getCreatedDate() {
62+
return createdDate;
63+
}
64+
65+
public void setCreatedDate(long createdDate) {
66+
this.createdDate = createdDate;
67+
}
68+
69+
public String getCreatedBy() {
70+
return createdBy;
71+
}
72+
73+
public void setCreatedBy(String createdBy) {
74+
this.createdBy = createdBy;
75+
}
76+
77+
public long getLastModifiedDate() {
78+
return lastModifiedDate;
79+
}
80+
81+
public String getLastModifiedBy() {
82+
return lastModifiedBy;
83+
}
84+
85+
public long getVersion() {
86+
return version;
87+
}
88+
89+
public void setVersion(long version) {
90+
this.version = version;
91+
}
92+
93+
@Override
94+
public int hashCode() {
95+
return Objects.hash(getId(), firstname, lastname);
96+
}
97+
98+
public String getTransientInfo() {
99+
return transientInfo;
100+
}
101+
102+
public void setTransientInfo(String something) {
103+
transientInfo = something;
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.couchbase.domain;
18+
19+
import java.util.List;
20+
import java.util.stream.Stream;
21+
22+
import com.couchbase.client.java.query.QueryScanConsistency;
23+
import org.springframework.data.couchbase.repository.CouchbaseRepository;
24+
import org.springframework.data.couchbase.repository.Query;
25+
import org.springframework.data.couchbase.repository.ScanConsistency;
26+
import org.springframework.data.repository.query.Param;
27+
import org.springframework.stereotype.Repository;
28+
29+
import com.couchbase.client.java.json.JsonArray;
30+
31+
/**
32+
* AbstractUser Repository for tests
33+
*
34+
* @author Michael Reiche
35+
*/
36+
@Repository
37+
@ScanConsistency(query=QueryScanConsistency.REQUEST_PLUS)
38+
public interface AbstractUserRepository extends CouchbaseRepository<AbstractUser, String> {
39+
40+
@Query("#{#n1ql.selectEntity} where (meta().id = $1)")
41+
AbstractUser myFindById(String id);
42+
43+
List<User> findByFirstname(String firstname);
44+
45+
Stream<User> findByLastname(String lastname);
46+
47+
List<User> findByFirstnameIn(String... firstnames);
48+
49+
List<User> findByFirstnameIn(JsonArray firstnames);
50+
51+
List<User> findByFirstnameAndLastname(String firstname, String lastname);
52+
53+
@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and firstname = $1 and lastname = $2")
54+
List<User> getByFirstnameAndLastname(String firstname, String lastname);
55+
56+
@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and (firstname = $first or lastname = $last)")
57+
List<User> getByFirstnameOrLastname(@Param("first") String firstname, @Param("last") String lastname);
58+
59+
List<User> findByIdIsNotNullAndFirstnameEquals(String firstname);
60+
61+
List<User> findByVersionEqualsAndFirstnameEquals(Long version, String firstname);
62+
63+
}

src/test/java/org/springframework/data/couchbase/domain/Airport.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
@Document
3636
@TypeAlias("airport")
3737
public class Airport extends ComparableEntity {
38-
@Id String id;
38+
@Id String key;
3939

4040
String iata;
4141

@@ -49,14 +49,14 @@ public class Airport extends ComparableEntity {
4949
long size;
5050

5151
@PersistenceConstructor
52-
public Airport(String id, String iata, String icao) {
53-
this.id = id;
52+
public Airport(String key, String iata, String icao) {
53+
this.key = key;
5454
this.iata = iata;
5555
this.icao = icao;
5656
}
5757

5858
public String getId() {
59-
return id;
59+
return key;
6060
}
6161

6262
public String getIata() {

src/test/java/org/springframework/data/couchbase/domain/AirportRepository.java

+3
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ Long countFancyExpression(@Param("projectIds") List<String> projectIds, @Param("
174174
+ " #{#planIds != null ? 'AND blahblah IN $2' : ''} " + " #{#active != null ? 'AND false = $3' : ''} ")
175175
Long countOne();
176176

177+
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
178+
Airport findByKey(String id);
179+
177180
@Retention(RetentionPolicy.RUNTIME)
178181
@Target({ ElementType.METHOD, ElementType.TYPE })
179182
// @Meta
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2022 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.couchbase.domain;
18+
19+
import org.springframework.data.annotation.PersistenceConstructor;
20+
import org.springframework.data.couchbase.core.mapping.Document;
21+
22+
/**
23+
* OtherUser entity for tests. Both User and OtherUser extend AbstractUser
24+
*
25+
* @author Michael Reiche
26+
*/
27+
28+
@Document
29+
public class OtherUser extends AbstractUser {
30+
31+
@PersistenceConstructor
32+
public OtherUser(final String id, final String firstname, final String lastname) {
33+
this.id = id;
34+
this.firstname = firstname;
35+
this.lastname = lastname;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)