|
|
|
@ -21,13 +21,16 @@ import static org.springframework.data.mongodb.core.query.Query.*; |
|
|
|
|
|
|
|
|
|
|
|
import lombok.Data; |
|
|
|
import lombok.Data; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
|
|
|
|
import org.springframework.data.annotation.Id; |
|
|
|
import org.springframework.data.annotation.Id; |
|
|
|
|
|
|
|
import org.springframework.data.mongodb.core.convert.LazyLoadingTestUtils; |
|
|
|
import org.springframework.data.mongodb.core.mapping.DBRef; |
|
|
|
import org.springframework.data.mongodb.core.mapping.DBRef; |
|
|
|
import org.springframework.data.mongodb.core.mapping.Document; |
|
|
|
import org.springframework.data.mongodb.core.mapping.Document; |
|
|
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.mongodb.MongoClient; |
|
|
|
import com.mongodb.MongoClient; |
|
|
|
|
|
|
|
|
|
|
|
@ -40,6 +43,7 @@ import com.mongodb.MongoClient; |
|
|
|
public class MongoTemplateDbRefTests { |
|
|
|
public class MongoTemplateDbRefTests { |
|
|
|
|
|
|
|
|
|
|
|
MongoTemplate template; |
|
|
|
MongoTemplate template; |
|
|
|
|
|
|
|
MongoTemplate otherDbTemplate; |
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
@Before |
|
|
|
public void setUp() { |
|
|
|
public void setUp() { |
|
|
|
@ -49,6 +53,13 @@ public class MongoTemplateDbRefTests { |
|
|
|
template.dropCollection(RefCycleLoadingIntoDifferentTypeRoot.class); |
|
|
|
template.dropCollection(RefCycleLoadingIntoDifferentTypeRoot.class); |
|
|
|
template.dropCollection(RefCycleLoadingIntoDifferentTypeIntermediate.class); |
|
|
|
template.dropCollection(RefCycleLoadingIntoDifferentTypeIntermediate.class); |
|
|
|
template.dropCollection(RefCycleLoadingIntoDifferentTypeRootView.class); |
|
|
|
template.dropCollection(RefCycleLoadingIntoDifferentTypeRootView.class); |
|
|
|
|
|
|
|
template.dropCollection(WithRefToAnotherDb.class); |
|
|
|
|
|
|
|
template.dropCollection(WithLazyRefToAnotherDb.class); |
|
|
|
|
|
|
|
template.dropCollection(WithListRefToAnotherDb.class); |
|
|
|
|
|
|
|
template.dropCollection(WithLazyListRefToAnotherDb.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
otherDbTemplate = new MongoTemplate(new MongoClient(), "mongo-template-dbref-tests-other-db"); |
|
|
|
|
|
|
|
otherDbTemplate.dropCollection(JustSomeType.class); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-1703
|
|
|
|
@Test // DATAMONGO-1703
|
|
|
|
@ -82,6 +93,83 @@ public class MongoTemplateDbRefTests { |
|
|
|
assertThat(loaded.getRefToIntermediate().getRefToRootView().getContent()).isEqualTo("jon snow"); |
|
|
|
assertThat(loaded.getRefToIntermediate().getRefToRootView().getContent()).isEqualTo("jon snow"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-2223
|
|
|
|
|
|
|
|
public void shouldResolveSingleDBRefToAnotherDb() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JustSomeType one = new JustSomeType(); |
|
|
|
|
|
|
|
one.value = "one"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
otherDbTemplate.insert(one); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WithRefToAnotherDb source = new WithRefToAnotherDb(); |
|
|
|
|
|
|
|
source.value = one; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template.save(source); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WithRefToAnotherDb target = template.findOne(query(where("id").is(source.id)), WithRefToAnotherDb.class); |
|
|
|
|
|
|
|
assertThat(target.getValue()).isEqualTo(one); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-2223
|
|
|
|
|
|
|
|
public void shouldResolveSingleLazyDBRefToAnotherDb() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JustSomeType one = new JustSomeType(); |
|
|
|
|
|
|
|
one.value = "one"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
otherDbTemplate.insert(one); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WithLazyRefToAnotherDb source = new WithLazyRefToAnotherDb(); |
|
|
|
|
|
|
|
source.value = one; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template.save(source); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WithLazyRefToAnotherDb target = template.findOne(query(where("id").is(source.id)), WithLazyRefToAnotherDb.class); |
|
|
|
|
|
|
|
LazyLoadingTestUtils.assertProxyIsResolved(target.value, false); |
|
|
|
|
|
|
|
assertThat(target.getValue()).isEqualTo(one); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-2223
|
|
|
|
|
|
|
|
public void shouldResolveListDBRefToAnotherDb() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JustSomeType one = new JustSomeType(); |
|
|
|
|
|
|
|
one.value = "one"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JustSomeType two = new JustSomeType(); |
|
|
|
|
|
|
|
two.value = "two"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
otherDbTemplate.insertAll(Arrays.asList(one, two)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WithListRefToAnotherDb source = new WithListRefToAnotherDb(); |
|
|
|
|
|
|
|
source.value = Arrays.asList(one, two); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template.save(source); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WithListRefToAnotherDb target = template.findOne(query(where("id").is(source.id)), WithListRefToAnotherDb.class); |
|
|
|
|
|
|
|
assertThat(target.getValue()).containsExactlyInAnyOrder(one, two); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-2223
|
|
|
|
|
|
|
|
public void shouldResolveLazyListDBRefToAnotherDb() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JustSomeType one = new JustSomeType(); |
|
|
|
|
|
|
|
one.value = "one"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JustSomeType two = new JustSomeType(); |
|
|
|
|
|
|
|
two.value = "two"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
otherDbTemplate.insertAll(Arrays.asList(one, two)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WithLazyListRefToAnotherDb source = new WithLazyListRefToAnotherDb(); |
|
|
|
|
|
|
|
source.value = Arrays.asList(one, two); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template.save(source); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WithLazyListRefToAnotherDb target = template.findOne(query(where("id").is(source.id)), |
|
|
|
|
|
|
|
WithLazyListRefToAnotherDb.class); |
|
|
|
|
|
|
|
LazyLoadingTestUtils.assertProxyIsResolved(target.value, false); |
|
|
|
|
|
|
|
assertThat(target.getValue()).containsExactlyInAnyOrder(one, two); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
@Data |
|
|
|
@Document("cycle-with-different-type-root") |
|
|
|
@Document("cycle-with-different-type-root") |
|
|
|
static class RefCycleLoadingIntoDifferentTypeRoot { |
|
|
|
static class RefCycleLoadingIntoDifferentTypeRoot { |
|
|
|
@ -107,4 +195,42 @@ public class MongoTemplateDbRefTests { |
|
|
|
String content; |
|
|
|
String content; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
|
|
|
|
static class WithRefToAnotherDb { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Id String id; |
|
|
|
|
|
|
|
@org.springframework.data.mongodb.core.mapping.DBRef(db = "mongo-template-dbref-tests-other-db") JustSomeType value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
|
|
|
|
static class WithLazyRefToAnotherDb { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Id String id; |
|
|
|
|
|
|
|
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true, |
|
|
|
|
|
|
|
db = "mongo-template-dbref-tests-other-db") JustSomeType value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
|
|
|
|
static class WithListRefToAnotherDb { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Id String id; |
|
|
|
|
|
|
|
@org.springframework.data.mongodb.core.mapping.DBRef( |
|
|
|
|
|
|
|
db = "mongo-template-dbref-tests-other-db") List<JustSomeType> value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
|
|
|
|
static class WithLazyListRefToAnotherDb { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Id String id; |
|
|
|
|
|
|
|
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true, |
|
|
|
|
|
|
|
db = "mongo-template-dbref-tests-other-db") List<JustSomeType> value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
|
|
|
|
static class JustSomeType { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Id String id; |
|
|
|
|
|
|
|
String value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|