|
35 | 35 | import org.springframework.data.mongodb.core.mapping.DBRef;
|
36 | 36 | import org.springframework.data.mongodb.core.mapping.Document;
|
37 | 37 | import org.springframework.data.mongodb.core.mapping.MongoId;
|
| 38 | +import org.springframework.data.mongodb.core.query.Query; |
38 | 39 | import org.springframework.data.mongodb.test.util.MongoTemplateExtension;
|
39 | 40 | import org.springframework.data.mongodb.test.util.MongoTestTemplate;
|
40 | 41 | import org.springframework.data.mongodb.test.util.Template;
|
@@ -232,6 +233,53 @@ public void shouldResolveLazyListDBRefToAnotherDb() {
|
232 | 233 | assertThat(target.getValue()).containsExactlyInAnyOrder(one, two);
|
233 | 234 | }
|
234 | 235 |
|
| 236 | + @Test // GH-2191 |
| 237 | + void shouldAllowToSliceCollectionOfDbRefs() { |
| 238 | + |
| 239 | + JustSomeType one = new JustSomeType(); |
| 240 | + one.value = "one"; |
| 241 | + |
| 242 | + JustSomeType two = new JustSomeType(); |
| 243 | + two.value = "two"; |
| 244 | + |
| 245 | + template.insertAll(Arrays.asList(one, two)); |
| 246 | + |
| 247 | + WithCollectionDbRef source = new WithCollectionDbRef(); |
| 248 | + source.refs = Arrays.asList(one, two); |
| 249 | + |
| 250 | + template.save(source); |
| 251 | + |
| 252 | + Query theQuery = query(where("id").is(source.id)); |
| 253 | + theQuery.fields().slice("refs", 1, 1); |
| 254 | + |
| 255 | + WithCollectionDbRef target = template.findOne(theQuery, WithCollectionDbRef.class); |
| 256 | + assertThat(target.getRefs()).containsExactly(two); |
| 257 | + } |
| 258 | + |
| 259 | + @Test // GH-2191 |
| 260 | + void shouldAllowToSliceCollectionOfLazyDbRefs() { |
| 261 | + |
| 262 | + JustSomeType one = new JustSomeType(); |
| 263 | + one.value = "one"; |
| 264 | + |
| 265 | + JustSomeType two = new JustSomeType(); |
| 266 | + two.value = "two"; |
| 267 | + |
| 268 | + template.insertAll(Arrays.asList(one, two)); |
| 269 | + |
| 270 | + WithCollectionDbRef source = new WithCollectionDbRef(); |
| 271 | + source.lazyrefs = Arrays.asList(one, two); |
| 272 | + |
| 273 | + template.save(source); |
| 274 | + |
| 275 | + Query theQuery = query(where("id").is(source.id)); |
| 276 | + theQuery.fields().slice("lazyrefs", 1, 1); |
| 277 | + |
| 278 | + WithCollectionDbRef target = template.findOne(theQuery, WithCollectionDbRef.class); |
| 279 | + LazyLoadingTestUtils.assertProxyIsResolved(target.lazyrefs, false); |
| 280 | + assertThat(target.getLazyrefs()).containsExactly(two); |
| 281 | + } |
| 282 | + |
235 | 283 | @Data
|
236 | 284 | @Document("cycle-with-different-type-root")
|
237 | 285 | static class RefCycleLoadingIntoDifferentTypeRoot {
|
@@ -264,6 +312,16 @@ static class RawStringId {
|
264 | 312 | String value;
|
265 | 313 | }
|
266 | 314 |
|
| 315 | + @Data |
| 316 | + static class WithCollectionDbRef { |
| 317 | + |
| 318 | + @Id String id; |
| 319 | + |
| 320 | + @DBRef List<JustSomeType> refs; |
| 321 | + |
| 322 | + @DBRef(lazy = true) List<JustSomeType> lazyrefs; |
| 323 | + } |
| 324 | + |
267 | 325 | @Data
|
268 | 326 | static class WithDBRefOnRawStringId {
|
269 | 327 |
|
|
0 commit comments