@@ -162,11 +162,12 @@ public N1qlSpelValues createN1qlSpelValues(String bucketName, String collection,
162
162
String b = collection != null ? collection : bucketName ;
163
163
Assert .isTrue (!(distinctFields != null && fields != null ),
164
164
"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 );
166
167
String count = "COUNT(*) AS " + CountFragment .COUNT_ALIAS ;
167
168
String selectEntity ;
168
169
if (distinctFields != null ) {
169
- String distinctFieldsStr = getProjectedOrDistinctFields (b , domainClass , fields , distinctFields );
170
+ String distinctFieldsStr = getProjectedOrDistinctFields (b , domainClass , typeField , fields , distinctFields );
170
171
if (isCount ) {
171
172
selectEntity = "SELECT COUNT( DISTINCT {" + distinctFieldsStr + "} ) " + CountFragment .COUNT_ALIAS + " FROM "
172
173
+ i (b );
@@ -176,7 +177,7 @@ public N1qlSpelValues createN1qlSpelValues(String bucketName, String collection,
176
177
} else if (isCount ) {
177
178
selectEntity = "SELECT " + count + " FROM " + i (b );
178
179
} else {
179
- String projectedFields = getProjectedOrDistinctFields (b , domainClass , fields , distinctFields );
180
+ String projectedFields = getProjectedOrDistinctFields (b , domainClass , typeField , fields , distinctFields );
180
181
selectEntity = "SELECT " + entity + (!projectedFields .isEmpty () ? ", " : " " ) + projectedFields + " FROM " + i (b );
181
182
}
182
183
String typeSelection = "`" + typeField + "` = \" " + typeValue + "\" " ;
@@ -187,22 +188,22 @@ public N1qlSpelValues createN1qlSpelValues(String bucketName, String collection,
187
188
return new N1qlSpelValues (selectEntity , entity , i (b ).toString (), typeSelection , delete , returning );
188
189
}
189
190
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 ) {
191
192
if (distinctFields != null && distinctFields .length != 0 ) {
192
193
return i (distinctFields ).toString ();
193
194
}
194
195
String projectedFields = i (b ) + ".*" ;
195
196
if (resultClass != null ) {
196
197
PersistentEntity persistentEntity = couchbaseConverter .getMappingContext ().getPersistentEntity (resultClass );
197
198
StringBuilder sb = new StringBuilder ();
198
- getProjectedFieldsInternal (b , null , sb , persistentEntity , fields , distinctFields != null );
199
+ getProjectedFieldsInternal (b , null , sb , persistentEntity , typeField , fields , distinctFields != null );
199
200
projectedFields = sb .toString ();
200
201
}
201
202
return projectedFields ;
202
203
}
203
204
204
205
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 ) {
206
207
207
208
if (persistentEntity != null ) {
208
209
Set <String > fieldList = fields != null ? new HashSet <>(Arrays .asList (fields )) : null ;
@@ -216,6 +217,8 @@ private void getProjectedFieldsInternal(String bucketName, CouchbasePersistentPr
216
217
if (prop == persistentEntity .getVersionProperty () && parent == null ) {
217
218
return ;
218
219
}
220
+ if ( prop .getFieldName ().equals (typeField )) // typeField already projected
221
+ return ;
219
222
// for distinct when no distinctFields were provided, do not include the expiration field.
220
223
if (forDistinct && prop .findAnnotation (Expiration .class ) != null && parent == null ) {
221
224
return ;
@@ -253,8 +256,10 @@ private void getProjectedFieldsInternal(String bucketName, CouchbasePersistentPr
253
256
}
254
257
} else {
255
258
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
+ }
258
263
}
259
264
sb .append (x (field ));
260
265
}
0 commit comments