Created
February 13, 2015 19:33
-
-
Save christophstrobl/9a73062c153300757a4e to your computer and use it in GitHub Desktop.
Stackoverflow-28504457
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(classes = SomeConfig.class) | |
public class SO28504457 { | |
@Configuration | |
@EnableMongoRepositories(considerNestedRepositories = true) | |
public static class SomeConfig extends AbstractMongoConfiguration { | |
@Override | |
protected String getDatabaseName() { | |
return "db-ref-test"; | |
} | |
@Override | |
public Mongo mongo() throws Exception { | |
return new MongoClient(); | |
} | |
} | |
@Autowired MongoTemplate template; | |
@Autowired SomePersonRepository personRepo; | |
@Before | |
public void setUp() { | |
template.dropCollection(Person.class); | |
template.dropCollection(Tag.class); | |
} | |
@Test | |
public void so28504457() { | |
Tag t1 = new Tag(); | |
t1.id = "t1"; | |
t1.tagName = "tag-1"; | |
Tag t2 = new Tag(); | |
t2.id = "t2"; | |
t2.tagName = "tag-2"; | |
template.save(t1); | |
template.save(t2); | |
Person p1 = new Person(); | |
p1.id = "p1"; | |
p1.tag = t1; | |
Person p2 = new Person(); | |
p2.id = "p2"; | |
p2.tag = t2; | |
Person p3 = new Person(); | |
p3.id = "p3"; | |
p3.tag = t2; | |
template.save(p1); | |
template.save(p2); | |
template.save(p3); | |
Page<Person> page_t1 = personRepo.findByTagId(t1.id, new PageRequest(0, 1)); | |
assertThat(page_t1.getTotalElements(), equalTo(1L)); | |
assertThat(page_t1.getNumberOfElements(), equalTo(1)); | |
assertThat(page_t1.getTotalPages(), equalTo(1)); | |
Page<Person> page_t2 = personRepo.findByTagId(t2.id, new PageRequest(0, 1)); | |
assertThat(page_t2.getTotalElements(), equalTo(2L)); | |
assertThat(page_t2.getNumberOfElements(), equalTo(1)); | |
assertThat(page_t2.getTotalPages(), equalTo(2)); | |
} | |
static interface SomePersonRepository extends CrudRepository<Person, String> { | |
Page<Person> findByTagId(String id, Pageable page); | |
} | |
@Document | |
static class Person { | |
@Id String id; | |
String name; | |
@org.springframework.data.mongodb.core.mapping.DBRef Tag tag; | |
} | |
@Document | |
static class Tag { | |
@Id String id; | |
String tagName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment