Skip to content

Commit 87b0b51

Browse files
committed
Restore projection of class.
Restore projection of class in queries. This is necessary for abstract repositories. Closes #1315.
1 parent 7e45fa2 commit 87b0b51

File tree

10 files changed

+352
-92
lines changed

10 files changed

+352
-92
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ 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) {
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 key;
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 key;
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 key, final String firstname, final String lastname) {
33+
this.key = key;
34+
this.firstname = firstname;
35+
this.lastname = lastname;
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,7 @@
1616

1717
package org.springframework.data.couchbase.domain;
1818

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;
2619
import org.springframework.data.annotation.PersistenceConstructor;
27-
import org.springframework.data.annotation.Transient;
28-
import org.springframework.data.annotation.Version;
2920
import org.springframework.data.couchbase.core.mapping.Document;
3021

3122
/**
@@ -36,79 +27,13 @@
3627
*/
3728

3829
@Document
39-
public class User extends ComparableEntity {
40-
41-
@Version long version;
42-
@Id private String id;
43-
private String firstname;
44-
private String lastname;
45-
@Transient private String transientInfo;
46-
@CreatedBy private String createdBy;
47-
@CreatedDate private long createdDate;
48-
@LastModifiedBy private String lastModifiedBy;
49-
@LastModifiedDate private long lastModifiedDate;
30+
public class User extends AbstractUser {
5031

5132
@PersistenceConstructor
52-
public User(final String id, final String firstname, final String lastname) {
53-
this.id = id;
33+
public User(final String key, final String firstname, final String lastname) {
34+
this.key = key;
5435
this.firstname = firstname;
5536
this.lastname = lastname;
5637
}
5738

58-
public String getId() {
59-
return id;
60-
}
61-
62-
public String getFirstname() {
63-
return firstname;
64-
}
65-
66-
public String getLastname() {
67-
return lastname;
68-
}
69-
70-
public long getCreatedDate() {
71-
return createdDate;
72-
}
73-
74-
public void setCreatedDate(long createdDate) {
75-
this.createdDate = createdDate;
76-
}
77-
78-
public String getCreatedBy() {
79-
return createdBy;
80-
}
81-
82-
public void setCreatedBy(String createdBy) {
83-
this.createdBy = createdBy;
84-
}
85-
86-
public long getLastModifiedDate() {
87-
return lastModifiedDate;
88-
}
89-
90-
public String getLastModifiedBy() {
91-
return lastModifiedBy;
92-
}
93-
94-
public long getVersion() {
95-
return version;
96-
}
97-
98-
public void setVersion(long version) {
99-
this.version = version;
100-
}
101-
102-
@Override
103-
public int hashCode() {
104-
return Objects.hash(id, firstname, lastname);
105-
}
106-
107-
public String getTransientInfo() {
108-
return transientInfo;
109-
}
110-
111-
public void setTransientInfo(String something) {
112-
transientInfo = something;
113-
}
11439
}

0 commit comments

Comments
 (0)