|
62 | 62 | * {@link DocumentReference} related integration tests for {@link MongoTemplate}.
|
63 | 63 | *
|
64 | 64 | * @author Christoph Strobl
|
| 65 | + * @author Julia Lee |
65 | 66 | */
|
66 | 67 | @ExtendWith(MongoClientExtension.class)
|
67 | 68 | public class MongoTemplateDocumentReferenceTests {
|
@@ -1270,6 +1271,32 @@ void readWriteTypeReferenceHavingFixedStringIdTargetType() {
|
1270 | 1271 | .isEqualTo(new ObjectRefHavingStringIdTargetType(id.toHexString(), "me-the-referenced-object"));
|
1271 | 1272 | }
|
1272 | 1273 |
|
| 1274 | + @Test // GH-4484 |
| 1275 | + void resolveReferenceForOneToManyLookupWithSelfVariableWhenUsedInCtorArgument() { |
| 1276 | + |
| 1277 | + OneToManyStylePublisherWithRequiredArgsCtor publisher = new OneToManyStylePublisherWithRequiredArgsCtor("p-100", null); |
| 1278 | + template.save(publisher); |
| 1279 | + |
| 1280 | + OneToManyStyleBook book1 = new OneToManyStyleBook(); |
| 1281 | + book1.id = "id-1"; |
| 1282 | + book1.publisherId = publisher.id; |
| 1283 | + |
| 1284 | + OneToManyStyleBook book2 = new OneToManyStyleBook(); |
| 1285 | + book2.id = "id-2"; |
| 1286 | + book2.publisherId = "p-200"; |
| 1287 | + |
| 1288 | + OneToManyStyleBook book3 = new OneToManyStyleBook(); |
| 1289 | + book3.id = "id-3"; |
| 1290 | + book3.publisherId = publisher.id; |
| 1291 | + |
| 1292 | + template.save(book1); |
| 1293 | + template.save(book2); |
| 1294 | + template.save(book3); |
| 1295 | + |
| 1296 | + OneToManyStylePublisherWithRequiredArgsCtor target = template.findOne(query(where("id").is(publisher.id)), OneToManyStylePublisherWithRequiredArgsCtor.class); |
| 1297 | + assertThat(target.books).containsExactlyInAnyOrder(book1, book3); |
| 1298 | + } |
| 1299 | + |
1273 | 1300 | @Data
|
1274 | 1301 | static class SingleRefRoot {
|
1275 | 1302 |
|
@@ -1614,4 +1641,40 @@ public static class WithListOfRefs {
|
1614 | 1641 |
|
1615 | 1642 | @DocumentReference private List<WithListOfRefs> refs;
|
1616 | 1643 | }
|
| 1644 | + |
| 1645 | + static class OneToManyStylePublisherWithRequiredArgsCtor { |
| 1646 | + |
| 1647 | + @Id |
| 1648 | + String id; |
| 1649 | + |
| 1650 | + @ReadOnlyProperty |
| 1651 | + @DocumentReference(lookup="{'publisherId':?#{#self._id} }") |
| 1652 | + List<OneToManyStyleBook> books; |
| 1653 | + |
| 1654 | + public OneToManyStylePublisherWithRequiredArgsCtor(String id, List<OneToManyStyleBook> books) { |
| 1655 | + this.id = id; |
| 1656 | + this.books = books; |
| 1657 | + } |
| 1658 | + |
| 1659 | + public String getId() { |
| 1660 | + return this.id; |
| 1661 | + } |
| 1662 | + |
| 1663 | + public List<OneToManyStyleBook> getBooks() { |
| 1664 | + return this.books; |
| 1665 | + } |
| 1666 | + |
| 1667 | + public void setId(String id) { |
| 1668 | + this.id = id; |
| 1669 | + } |
| 1670 | + |
| 1671 | + public void setBooks(List<OneToManyStyleBook> books) { |
| 1672 | + this.books = books; |
| 1673 | + } |
| 1674 | + |
| 1675 | + public String toString() { |
| 1676 | + return "MongoTemplateDocumentReferenceTests.OneToManyStylePublisherWithRequiredArgsCtor(id=" + this.getId() + ", book=" |
| 1677 | + + this.getBooks() + ")"; |
| 1678 | + } |
| 1679 | + } |
1617 | 1680 | }
|
0 commit comments