Skip to content

Commit de8ebda

Browse files
Add data-mongo test for lazy loading collection like DBRef.
See: spring-projects/spring-data-mongodb#4351
1 parent 676da04 commit de8ebda

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

data/data-mongodb/src/appTest/java/com/example/data/mongodb/DataMongoDbApplicationAotTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ void documentReference(AssertableOutput output) {
4747
});
4848
}
4949

50+
@Test
51+
void lazyDbRef(AssertableOutput output) {
52+
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
53+
assertThat(output).hasLineMatching(
54+
".*List\\(\\d\\$LazyLoadingProxy\\)\\{\\W?com\\.example\\.data\\.mongodb\\.Coupon@.*\\W?\\}.*");
55+
});
56+
}
57+
5058
@Test
5159
void queryByExample(AssertableOutput output) {
5260
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {

data/data-mongodb/src/main/java/com/example/data/mongodb/CLR.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,15 @@ private void runWithDbRefs(LineItem product1, LineItem product2, LineItem produc
259259
order.setCoupon(coupon);
260260
order.setReduction(coupon);
261261
order.setSimpleRef(coupon);
262+
order.setAllCoupons(List.of(coupon));
262263
orderRepository.save(order);
263264

264265
Optional<Order> loaded = orderRepository.findById(order.getId());
265266
log("simple ref (no proxy): %s", loaded.get().getSimpleRef().getCode());
266267
log("lazyLoading (aot): %s", loaded.get().getCoupon().getCode());
267268
log("lazyLoading (jdk): %s", loaded.get().getReduction().getId());
269+
log("lazyLoading - lists (jdk): List(%s){ %s }", loaded.get().getAllCoupons(),
270+
loaded.get().getAllCoupons().get(0));
268271

269272
log("-----------------\n\n\n");
270273
}

data/data-mongodb/src/main/java/com/example/data/mongodb/Order.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public class Order {
5959
@DBRef(lazy = true) // JdkProxy
6060
private PriceReduction reduction;
6161

62+
@DBRef(lazy = true) // JdkProxy
63+
private List<Coupon> allCoupons;
64+
6265
@DocumentReference
6366
private Discount documentRef;
6467

@@ -185,6 +188,14 @@ public void setLazyDocumentRef(Discount lazyDocumentRef) {
185188
this.lazyDocumentRef = lazyDocumentRef;
186189
}
187190

191+
public void setAllCoupons(List<Coupon> allCoupons) {
192+
this.allCoupons = allCoupons;
193+
}
194+
195+
public List<Coupon> getAllCoupons() {
196+
return allCoupons;
197+
}
198+
188199
@Override
189200
public String toString() {
190201
return "Order{" + "id='" + id + '\'' + ", customerId='" + customerId + '\'' + ", orderDate=" + orderDate

0 commit comments

Comments
 (0)