Neo4J - Lab 2
In this lab, we will explore more about Neo4J and Java.
1. Define the Book
Entity Class
Steps
- Open the
06-graph
project and navigate to thesrc/main/java
- Create a class called
Book
in theexpert.os.labs.persistence
package - Define the class with the
@Entity
annotation from thejakarta.nosql
package -
Add the following
private
fields:@Id Long id
@Column String name
-
Add a static method to create a book by a given
name
-
Implement constructors, getter methods,
equals
,hashCode
, andtoString
methods
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
Category
in theexpert.os.labs.persistence
package - Define the class with the
@Entity
annotation from thejakarta.nosql
package -
Add the following
private
fields:@Id Long id
@Column String name
-
Add a static method to create a book by a given
name
-
Implement constructors, getter methods,
equals
,hashCode
, andtoString
methods
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
enum
calledLibraryLabels
in theexpert.os.labs.persistence
package - 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
private
attribute to get the constant value - Define a constructor to set the value
- Override the
get()
method from theSupplier
returning the value attribute
Solution
Click to see...
4. Create the BookService
Class
Steps
- Create a class called
BookService
in theexpert.os.labs.persistence
package - Annotate it using the
@ApplicationScoped
from thejakarta.enterprise.context
package -
Create a private field with the
GraphTemplate
type from theorg.eclipse.jnosql.mapping.graph
package- name is as
graph
- add the annotation
@Inject
from thejakarta.inject
package
- name is as
-
Create a method named
category
that receives aname
as input, retrieving or inserting it in the graph -
Create a method named
book
that receives aname
as input, retrieving or inserting it in the graph -
Create a method named
category
to establish a relationship betweenBook
andCategory
, so they will become the parameters-
use the
graph.edge()
method to establish theIS
relationship
-
-
Create a method named
category
to establish a relationship between other categories, creating a sub-category- use the
graph.edge()
method to establish theIS
relationship
- 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
AppBook
in theexpert.os.labs.persistence
package -
Add a main method
-
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 -
Obtain a
BookService
instance fromjakarta.nosql.document
package by selecting it:container.select(BookService.class).get()
-
Create the following categories by using the
BookService
instance- Software
- Romance
- Java
- NoSQL
-
Micro Service
-
Create the following books by using the
BookService
instance- 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
AppBook
class 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() {
}
}