Neo4J - Lab 2
In this lab, we will explore more about Neo4J and Java.
1. Define the Book Entity Class
Steps
- Open the
06-graphproject and navigate to thesrc/main/java - Create a class called
Bookin theexpert.os.labs.persistencepackage - Define the class with the
@Entityannotation from thejakarta.nosqlpackage -
Add the following
privatefields:@Id Long id@Column String name
-
Add a static method to create a book by a given
name -
Implement constructors, getter methods,
equals,hashCode, andtoStringmethods
Solution
Click to see...
import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;
@Entity
public class Book {
@Id
private Long id;
@Column
private String name;
public Book() {
}
public Book(String name) {
this.name = name;
}
public static Book of(String name) {
return new Book(name);
}
// other methods
}
2. Create the Category Class
Steps
- Create a class called
Categoryin theexpert.os.labs.persistencepackage - Define the class with the
@Entityannotation from thejakarta.nosqlpackage -
Add the following
privatefields:@Id Long id@Column String name
-
Add a static method to create a book by a given
name -
Implement constructors, getter methods,
equals,hashCode, andtoStringmethods
Solution
Click to see...
import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;
@Entity
public class Category {
@Id
private Long id;
@Column
private String name;
public Category() {
}
public Category(String name) {
this.name = name;
}
public static Category of(String name) {
return new Category(name);
}
// other methods
}
3. Create the LibraryLabels Enum
Steps
- Create an
enumcalledLibraryLabelsin theexpert.os.labs.persistencepackage - It implements the
Supplier<String>interface to provide a custom label for the relationship between books and categories - Add the constant
IS - Associate a value to the constant with the same name
is - Define a
privateattribute to get the constant value - Define a constructor to set the value
- Override the
get()method from theSupplierreturning the value attribute
Solution
Click to see...
4. Create the BookService Class
Steps
- Create a class called
BookServicein theexpert.os.labs.persistencepackage - Annotate it using the
@ApplicationScopedfrom thejakarta.enterprise.contextpackage -
Create a private field with the
GraphTemplatetype from theorg.eclipse.jnosql.mapping.graphpackage- name is as
graph - add the annotation
@Injectfrom thejakarta.injectpackage
- name is as
-
Create a method named
categorythat receives anameas input, retrieving or inserting it in the graph -
Create a method named
bookthat receives anameas input, retrieving or inserting it in the graph -
Create a method named
categoryto establish a relationship betweenBookandCategory, so they will become the parameters-
use the
graph.edge()method to establish theISrelationship
-
-
Create a method named
categoryto establish a relationship between other categories, creating a sub-category- use the
graph.edge()method to establish theISrelationship
- use the
-
Create a method to retrieve a list of categories by their name related to "Software"
-
Create a method to retrieve a list of books by their name related to "Software"
-
Create a method to retrieve a list of books by its name related to "Software" and "NoSQL"
5. Create the execution class
Steps
- Create a class called
AppBookin 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 -
Obtain a
BookServiceinstance fromjakarta.nosql.documentpackage by selecting it:container.select(BookService.class).get() -
Create the following categories by using the
BookServiceinstance- Software
- Romance
- Java
- NoSQL
-
Micro Service
-
Create the following books by using the
BookServiceinstance- Effective Java
- NoSQL Distilled
- Migrating to Microservice Databases
-
The Shack
-
Associate the sub-categories within the categories
- Java and Software
- NoSQL and Software
-
Microservice and Software
-
Associate the books with the categories
- Effective Java and Software
- NoSQL Distilled and Software
-
MicroServices and Software
-
Associate the sub-category with the books
- Effective Java and Java
- NoSQL Distilled and NoSQL
- Migrating to Microservices to MicroService
-
Shack to Romance
-
Filter the software books by category using the
softwareCategories()method and printout the result -
Filter all the software books by using the
softwareBooks()method and printout the result -
Filter all the software and NoSQL books by using the
softwareNoSQLBooks()method and printout the result -
Define a private constructor for the
AppBookclass to prevent instantiation since it contains only static methods: -
Run the
main()method
Expected results
-
The following output
Solution
Click to see...
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import java.util.List;
public class AppBook {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
BookService service = container.select(BookService.class).get();
Category software = service.category("Software");
Category romance = service.category("Romance");
Category java = service.category("Java");
Category nosql =service.category("NoSQL");
Category microService =service.category("MicroService");
Book effectiveJava = service.book("Effective Java");
Book nosqlDistilled = service.book("NoSQL Distilled");
Book migratingMicroservice = service.book("Migrating to Microservice Databases");
Book shack = service.book("The Shack");
service.category(java, software);
service.category(nosql, software);
service.category(microService, software);
service.category(effectiveJava, software);
service.category(nosqlDistilled, software);
service.category(migratingMicroservice, software);
service.category(effectiveJava, java);
service.category(nosqlDistilled, nosql);
service.category(migratingMicroservice, microService);
service.category(shack, romance);
List<String> softwareCategories =service.softwareCategories();
System.out.println("Software Categories = " + softwareCategories);
List<String> softwareBooks = service.softwareBooks();
System.out.println("Software Books = " + softwareBooks);
List<String> softwareNoSQLBooks = service.softwareNoSQLBooks();
System.out.println("Software and NoSQL Books = " + softwareNoSQLBooks);
}
}
private AppBook() {
}
}