MongoDB - Lab 4
In this lab, you will create Java classes for defining a repository interface and a main application class. The repository interface (AuthorRepository
) will extend PageableRepository
for data access, and the main application (App4
) will demonstrate how to use this repository to interact with a NoSQL database using Jakarta Data. Below are the step-by-step instructions for creating these classes:
1. Define the AuthorRepository
Interface
Steps
- Create an
interface
calledAuthorRepository
in theexpert.os.labs.persistence
package - Annotate it with
@Repository
from thejakarta.data.repository
package -
Extends the interface with the
PageableRepository
using theAuthor
class andLong
-
Add a custom query to find the author by name
-
Add a custom query to find a phone number
Solution
Click to see...
import jakarta.data.repository.PageableRepository;
import jakarta.data.repository.Repository;
import java.util.List;
import java.util.stream.Stream;
@Repository
public interface AuthorRepository extends PageableRepository<Author, Long> {
List<Author> findByName(String name);
Stream<Author> findByPhones(String phone);
}
2. Create the execution class
Steps
- Create a class called
AppMongoDBSubdocumentRepository
in theexpert.os.labs.persistence
package -
Add a main method
-
Create a
ThreadLocalRandom
instance and get the next value, associating it to an attribute -
Set up a try-with-resources block, inside the
main
method, to manage the Jakarta EESeContainer
that is responsible for dependency injection and managing resources -
Create a new instance of
Faker
-
Create a new instance of the
Author
class usingfaker
to provide data -
Create a new instance of the
Job
class usingfaker
to provide data -
Create a new instance of the
Author
class usingfaker
to provide data -
Obtain an instance of the
AuthorRepository
from the Jakarta SE container by selecting it:container.select(AuthorRepository.class).get()
-
Use the
AuthorRepository
to save theAuthor
instance into the NoSQL database -
Query the authors by name
-
Printout the result
-
Retrieve the same author by it's phone number
-
Printout the result
-
Define a private constructor for the
AppMongoDBSubdocumentRepository
class to prevent instantiation since it contains only static methods: -
Run the
main()
method
Expected results
-
The following output
Author by name = [Author{id=-6736921736259877587, name='Edward Koch', phones=[1-267-083-3844, (367) 346-8130], address=Address{street='Jin Ports', city='North Alysebury'}, job=Job{salary=12.12, occupation='IT Consultant'}}] Author's phone = [Author{id=-6736921736259877587, name='Edward Koch', phones=[1-267-083-3844, (367) 346-8130], address=Address{street='Jin Ports', city='North Alysebury'}, job=Job{salary=12.12, occupation='IT Consultant'}}]
Solution
Click to see...
import com.github.javafaker.Faker;
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class AppMongoDBSubdocumentRepository {
public static void main(String[] args) {
ThreadLocalRandom random = ThreadLocalRandom.current();
long id = random.nextLong();
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
Faker faker = new Faker();
Address address = new Address(faker.address().streetName(), faker.address().city());
Job job = new Job(12.12, faker.job().title());
Author author = Author.builder()
.phones(Arrays.asList(faker.phoneNumber().cellPhone(), faker.phoneNumber().cellPhone()))
.name(faker.name().fullName())
.address(address)
.job(job)
.id(id).build();
AuthorRepository template = container.select(AuthorRepository.class).get();
template.save(author);
List<Author> authorByName = template.findByName(author.getName());
System.out.println("Author by name = " + authorByName);
List<Author> list = template.findByPhones(author.getPhones().get(0)).toList();
System.out.println("Author's phone = " + list);
}
}
private AppMongoDBSubdocumentRepository() {
}
}