MongoDB - Lab 3
In this lab, you will create a Java application that utilizes Jakarta Data's repository framework to manage a library of books. The application will allow you to save books, retrieve books by title, and perform pageable queries on the book data.
1. Define the Library Repository Interface
Steps
-
Create an interface called
Libraryand annotate it with@Repositoryfrom thejakarta.data.repositorypackage -
Extend the
PageableRepository<Book, String>interface, specifying that it's a repository for managingBookentities withStringas the type of the ID field -
Define a custom query method
findByTitle()that will take atitleasStringwhere the return is aListofBook
Expected results
- Entity
Librarycreated
Solution
Click to see...
2. Add a factory data method to Book
Steps
- Open the
Bookclass -
Add the following method
public static Book of(Faker faker) { return new Book(faker.code().isbn13(), faker.book().title(), faker.number().numberBetween(1, 10), faker.number().numberBetween(1900, 2022)); }Note
The method
of()will take theFakerclass that's from the DataFaker library to generate random data when its accessed.
Expected results
- Class
Bookwith an additional method to generate its instance using random data
Solution
Click to see...
import com.github.javafaker.Faker;
import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;
@Entity
public record Book(@Id String isbn, @Column String title, @Column int edition, @Column int year) {
public static Book of(Faker faker) {
return new Book(faker.code().isbn13(), faker.book().title(), faker.number().numberBetween(1, 10),
faker.number().numberBetween(1900, 2022));
}
}
3. Create the execution class to insert data
Steps
- Create a class called
AppMongoDBPagablein theexpert.os.labs.persistencepackage -
Add a main method
-
Set up a try-with-resources block, inside the
mainmethod, to manage the Jakarta EESeContainerthat is responsible for dependency injection and managing resources -
Using the Jakarta SE container, obtain an instance of the
Libraryrepository by selecting it:container.select(Library.class).get() -
Instantiate the
Fakerclass to be able to, later on, save the book with some data -
Create a look saving 100 different books using the
libary.save()method -
Create a
Pageableobject namedpageableusingPageable.ofSize(10).sortBy(Sort.asc("title"), Sort.desc("year")). This sets options for retrieving books in pages of size 10, sorted by title in ascending order and year in descending order- both
PageableandSortclasses are from thejakarta.data.repositorypackage
- both
-
Find all books on the filter we created above, which will contain 10 entries, using the
Pageclass fromjakarta.data.repositorypackage -
Printout the content of the
pageattribute using thecontent()method -
Create a second
Pageableobject namednextPageto retrieve the next page of books -
Find all books from the next page
-
Printout the content of the
page2attribute using thecontent()method -
Use the custom query
findByTitle()to find the book "Effective Java" and printout the result -
Define a private constructor for the
AppMongoDBPagableclass to prevent instantiation since it contains only static methods: -
Run the
main()method
Expected results
-
The following output
Page = [Book[isbn=2fc4801f-fbed-4878-9e85-538487feae65, title=Effective Java, edition=1, year=2019], Book[isbn=9790753509363, title=If Not Now, When?, edition=5, year=2017], Book[isbn=9780972389006, title=FranΓ§oise Sagan, edition=4, year=2016], Book[isbn=9781430782025, title=The Little Foxes, edition=9, year=2012], Book[isbn=9790835585384, title=If Not Now, When?, edition=6, year=2010], Book[isbn=9780771303333, title=The World, the Flesh and the Devil, edition=7, year=2009], Book[isbn=9791077784467, title=To Sail Beyond the Sunset, edition=9, year=2008], Book[isbn=9780705967556, title=That Hideous Strength, edition=3, year=2006], Book[isbn=9790949723771, title=An Instant In The Wind, edition=5, year=2006], Book[isbn=9781470218720, title=The Violent Bear It Away, edition=7, year=2005]] Page 2 = NoSQLPage{entities=[Book[isbn=9781470218720, title=The Violent Bear It Away, edition=7, year=2005], Book[isbn=9780878082858, title=Where Angels Fear to Tread, edition=3, year=2003], Book[isbn=9791962492460, title=The Moon by Night, edition=4, year=2002], Book[isbn=9780736251754, title=East of Eden, edition=4, year=2002], Book[isbn=9781087074238, title=Nectar in a Sieve, edition=8, year=2001], Book[isbn=9790880353556, title=To Say Nothing of the Dog, edition=4, year=2001], Book[isbn=9781884451362, title=Clouds of Witness, edition=8, year=2000], Book[isbn=9791584073665, title=Have His Carcase, edition=3, year=2000], Book[isbn=9781961487482, title=The Wind's Twelve Quarters, edition=7, year=1999], Book[isbn=9790332536162, title=Edna O'Brien, edition=9, year=1998]], pageable=Pageable{page=2, size=10, title ASC, year DESC}} Book[isbn=2fc4801f-fbed-4878-9e85-538487feae65, title=Effective Java, edition=1, year=2019]
Solution
Click to see...
import com.github.javafaker.Faker;
import jakarta.data.repository.Page;
import jakarta.data.repository.Pageable;
import jakarta.data.repository.Sort;
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import java.util.stream.IntStream;
public class AppMongoDBPagable {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
Library library = container.select(Library.class).get();
Faker faker = new Faker();
IntStream.range(0, 100).mapToObj(index -> Book.of(faker)).forEach(library::save);
Pageable pageable = Pageable.ofSize(10).sortBy(Sort.asc("title"), Sort.desc("year"));
Page<Book> page = library.findAll(pageable);
System.out.println("Page = " + page.content());
Pageable nextPage = pageable.next();
Page<Book> page2 = library.findAll(nextPage);
System.out.println("Page 2 = " + page2);
library.findByTitle("Effective Java").forEach(System.out::println);
}
}
private AppMongoDBPagable() {
}
}