Define Domain Entities

In the current module we will create a REST service for managing a library.
We will have two domain entities: Book and Author.
Each book can have one or more authors. Each author can write zero or more books.
UML diagrams
This is the first version of the UML diagram for our domain entities:
Also, we need id fields. Using them we could delete and update entities.
Next, let’s add types to the class fields.
For title, isbn, description and name fields we will use String type.
For id fields we will use UUID type.
For publicationDate field we will use LocalDate type.
Here is the updated UML diagram with types:
Install Lombok
Lombok is a Java library that provides annotations to reduce boilerplate code in Java classes. It automates common tasks such as getters, setters, constructors, and more, making Java development more concise and readable.
Add a dependency to pom.xml:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
Add configuration for annotation processor:
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
Create Domain Classes
Create domain classes in dto package.
Use Data annotation from Lombok library to automatically generate getters, setters, toString, equals, and hashCode methods.
import lombok.Data;
import java.util.UUID;
@Data
public class AuthorDTO {
private final UUID id;
private final String name;
}
import lombok.Data;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
@Data
public class BookDTO {
private final UUID id;
private final String title;
private final String isbn;
private final String description;
private final List<AuthorDTO> authors;
private final LocalDate publicationDate;
}
We will use this AuthorDTO and BookDTO for fetching data.
But for creating and updating of the entities we need other classes:
- we don’t need
idfields- we will generate
idduring entity creation - we will pass
idin the URL while updating an entity
- we will generate
- we don’t need
List<AuthorDTO> authorsfield inBookentity, we need only the list ofids
Here are domain classes for entities creation and update:
import lombok.Data;
@Data
public class SaveAuthorDTO {
private final String name;
}
import lombok.Data;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
@Data
public class SaveBookDTO {
private final String title;
private final String isbn;
private final String description;
private final List<UUID> authorIds;
private final LocalDate publicationDate;
}