|
57 | 57 | * {@link DocumentReference} related integration tests for {@link MongoTemplate}.
|
58 | 58 | *
|
59 | 59 | * @author Christoph Strobl
|
| 60 | + * @author Julia Lee |
60 | 61 | */
|
61 | 62 | @ExtendWith(MongoClientExtension.class)
|
62 | 63 | public class MongoTemplateDocumentReferenceTests {
|
@@ -1265,6 +1266,32 @@ void readWriteTypeReferenceHavingFixedStringIdTargetType() {
|
1265 | 1266 | .isEqualTo(new ObjectRefHavingStringIdTargetType(id.toHexString(), "me-the-referenced-object"));
|
1266 | 1267 | }
|
1267 | 1268 |
|
| 1269 | + @Test // GH-4484 |
| 1270 | + void resolveReferenceForOneToManyLookupWithSelfVariableWhenUsedInCtorArgument() { |
| 1271 | + |
| 1272 | + OneToManyStylePublisherWithRequiredArgsCtor publisher = new OneToManyStylePublisherWithRequiredArgsCtor("p-100", null); |
| 1273 | + template.save(publisher); |
| 1274 | + |
| 1275 | + OneToManyStyleBook book1 = new OneToManyStyleBook(); |
| 1276 | + book1.id = "id-1"; |
| 1277 | + book1.publisherId = publisher.id; |
| 1278 | + |
| 1279 | + OneToManyStyleBook book2 = new OneToManyStyleBook(); |
| 1280 | + book2.id = "id-2"; |
| 1281 | + book2.publisherId = "p-200"; |
| 1282 | + |
| 1283 | + OneToManyStyleBook book3 = new OneToManyStyleBook(); |
| 1284 | + book3.id = "id-3"; |
| 1285 | + book3.publisherId = publisher.id; |
| 1286 | + |
| 1287 | + template.save(book1); |
| 1288 | + template.save(book2); |
| 1289 | + template.save(book3); |
| 1290 | + |
| 1291 | + OneToManyStylePublisherWithRequiredArgsCtor target = template.findOne(query(where("id").is(publisher.id)), OneToManyStylePublisherWithRequiredArgsCtor.class); |
| 1292 | + assertThat(target.books).containsExactlyInAnyOrder(book1, book3); |
| 1293 | + } |
| 1294 | + |
1268 | 1295 | static class SingleRefRoot {
|
1269 | 1296 |
|
1270 | 1297 | String id;
|
@@ -2249,4 +2276,40 @@ public String toString() {
|
2249 | 2276 | return "MongoTemplateDocumentReferenceTests.WithListOfRefs(id=" + this.getId() + ", refs=" + this.getRefs() + ")";
|
2250 | 2277 | }
|
2251 | 2278 | }
|
| 2279 | + |
| 2280 | + static class OneToManyStylePublisherWithRequiredArgsCtor { |
| 2281 | + |
| 2282 | + @Id |
| 2283 | + String id; |
| 2284 | + |
| 2285 | + @ReadOnlyProperty |
| 2286 | + @DocumentReference(lookup="{'publisherId':?#{#self._id} }") |
| 2287 | + List<OneToManyStyleBook> books; |
| 2288 | + |
| 2289 | + public OneToManyStylePublisherWithRequiredArgsCtor(String id, List<OneToManyStyleBook> books) { |
| 2290 | + this.id = id; |
| 2291 | + this.books = books; |
| 2292 | + } |
| 2293 | + |
| 2294 | + public String getId() { |
| 2295 | + return this.id; |
| 2296 | + } |
| 2297 | + |
| 2298 | + public List<OneToManyStyleBook> getBooks() { |
| 2299 | + return this.books; |
| 2300 | + } |
| 2301 | + |
| 2302 | + public void setId(String id) { |
| 2303 | + this.id = id; |
| 2304 | + } |
| 2305 | + |
| 2306 | + public void setBooks(List<OneToManyStyleBook> books) { |
| 2307 | + this.books = books; |
| 2308 | + } |
| 2309 | + |
| 2310 | + public String toString() { |
| 2311 | + return "MongoTemplateDocumentReferenceTests.OneToManyStylePublisherWithRequiredArgsCtor(id=" + this.getId() + ", book=" |
| 2312 | + + this.getBooks() + ")"; |
| 2313 | + } |
| 2314 | + } |
2252 | 2315 | }
|
0 commit comments