Skip to content

Commit b02959d

Browse files
committed
Support derived queries on repositories defined with an abstract entity class. (#1366)
Motivation: Currently an abstract entity class specified in the repository definition can be used for the predicate typeKey = typeAlias(of abstract entity class) in queries. Since documents are stored with typeKey = typeAlias(of concrete class) those queries will never match any documents. To allow this to work, all of the abstract entity class an all concrete subclasses must use the same typeAlias. Once those documents are found, regardless of their concrete class, they will all have the same typeKey = typeAlias, instead of having the typeAlias specific to the concrete class. Additional information in the stored document is needed to identify the concrete class (subtype in the example test case), as well as a TypeMapper to interpret that information. Changes: This allows a common TypeAlias to be used for the purpose of the predicate typeKey = typeAlias, and the determination of the concrete type by implementing an AbstractingMappingCouchbaseConverter that inspects the 'subtype' property. Closes #1365. Co-authored-by: Michael Reiche <[email protected]>
1 parent 5f03d1e commit b02959d

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@
3030
import org.springframework.data.couchbase.core.mapping.Document;
3131

3232
import java.io.Serializable;
33+
import java.util.Objects;
34+
35+
import org.springframework.data.annotation.CreatedBy;
36+
import org.springframework.data.annotation.CreatedDate;
37+
import org.springframework.data.annotation.LastModifiedBy;
38+
import org.springframework.data.annotation.LastModifiedDate;
39+
import org.springframework.data.annotation.PersistenceConstructor;
40+
import org.springframework.data.annotation.Transient;
41+
import org.springframework.data.annotation.TypeAlias;
42+
import org.springframework.data.annotation.Version;
43+
import org.springframework.data.couchbase.core.mapping.Document;
3344

3445
/**
3546
* User entity for tests

src/test/java/org/springframework/data/couchbase/repository/query/StringN1qlQueryCreatorMockedTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void createsQueryCorrectly() throws Exception {
8585

8686
Query query = creator.createQuery();
8787
assertEquals(
88-
"SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `firstname`, `lastname`, `createdBy`, `createdDate`, `lastModifiedBy`, `lastModifiedDate` FROM `travel-sample` where `_class` = \"org.springframework.data.couchbase.domain.User\" and firstname = $1 and lastname = $2",
88+
"SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `_class`, `createdBy`, `createdDate`, `lastModifiedBy`, `lastModifiedDate`, `firstname`, `lastname`, `subtype` FROM `travel-sample` where `_class` = \"abstractuser\" and firstname = $1 and lastname = $2",
8989
query.toN1qlSelectString(couchbaseTemplate.reactive(), User.class, false));
9090
}
9191

@@ -104,7 +104,7 @@ void createsQueryCorrectly2() throws Exception {
104104

105105
Query query = creator.createQuery();
106106
assertEquals(
107-
"SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `firstname`, `lastname`, `createdBy`, `createdDate`, `lastModifiedBy`, `lastModifiedDate` FROM `travel-sample` where `_class` = \"org.springframework.data.couchbase.domain.User\" and (firstname = $first or lastname = $last)",
107+
"SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `_class`, `createdBy`, `createdDate`, `lastModifiedBy`, `lastModifiedDate`, `firstname`, `lastname`, `subtype` FROM `travel-sample` where `_class` = \"abstractuser\" and (firstname = $first or lastname = $last)",
108108
query.toN1qlSelectString(couchbaseTemplate.reactive(), User.class, false));
109109
}
110110

0 commit comments

Comments
 (0)