Search in sources :

Example 1 with Author

use of dev.morphia.test.models.Author in project morphia by mongodb.

the class TestReferences method testAggregationLookups.

@Test
public void testAggregationLookups() {
    final Author author = new Author("Jane Austen");
    getDs().save(author);
    List<Book> books = List.of(new Book("Sense and Sensibility", author), new Book("Pride and Prejudice", author), new Book("Mansfield Park", author), new Book("Emma", author), new Book("Northanger Abbey", author));
    getDs().save(books);
    author.setList(books);
    author.setSet(new HashSet<>(books));
    // Map<String, Book> map = addBookMap(author);
    getDs().save(author);
    Aggregation<Author> aggregation = getDs().aggregate(Author.class).lookup(lookup(Book.class).as("set").foreignField("_id").localField("set")).lookup(lookup(Book.class).as("list").foreignField("_id").localField("list"));
    final Author loaded = aggregation.execute(Author.class).tryNext();
    assertListEquals(author.list, loaded.list);
    assertListEquals(author.set, loaded.set);
    // validateMap(map, loaded);
    Book foundBook = getDs().aggregate(Book.class).lookup(lookup(Author.class).as("author").foreignField("_id").localField("author")).unwind(unwind("author")).execute(Book.class).next();
    assertTrue(foundBook.author.isResolved());
    assertEquals(author, foundBook.author.get());
}
Also used : Book(dev.morphia.test.models.Book) Author(dev.morphia.test.models.Author) Test(org.testng.annotations.Test)

Example 2 with Author

use of dev.morphia.test.models.Author in project morphia by mongodb.

the class TestMapping method testReferenceWithoutIdValue.

@Test
public void testReferenceWithoutIdValue() {
    assertThrows(ReferenceException.class, () -> {
        getMapper().map(Book.class, Author.class);
        final Book book = new Book();
        book.setAuthor(new Author());
        getDs().save(book);
    });
}
Also used : Book(dev.morphia.test.models.Book) Author(dev.morphia.test.models.Author) Test(org.testng.annotations.Test)

Aggregations

Author (dev.morphia.test.models.Author)2 Book (dev.morphia.test.models.Book)2 Test (org.testng.annotations.Test)2