Project: Entities
The data model.
Every layer depends on the data model. So we start with the Book entity, and the records that carry data in, and out.
- Start with the data
- The Book entity
- Records in and out
Book.java
At Entity maps Book to a table. The id is generated by the database. The constructor without arguments is for JPA. And the setters let the service change a book.
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
private double price;
protected Book() {}
public Book(String title, String author, double price) {
this.title = title; this.author = author; this.price = price;
}
// getters, plus setTitle, setAuthor and setPrice
}The DTOs
Create Book carries the input, with validation rules. Book Response is what we send back. And its from method converts an entity, into a response.
public record CreateBook(
@NotBlank String title,
@NotBlank String author,
@Positive double price
) {}
public record BookResponse(Long id, String title, String author, double price) {
static BookResponse from(Book b) {
return new BookResponse(b.getId(), b.getTitle(), b.getAuthor(), b.getPrice());
}
}Is the data there?
Data dot sql seeds eight books, when the app starts. This runner counts them, and prints the first three. Run it. Eight books are in the shop. And the ids were generated, one, two, and three.
@Component
public class SeedCheck implements CommandLineRunner {
private final BookRepository books;
public SeedCheck(BookRepository books) { this.books = books; }
@Override
public void run(String... args) {
System.out.println("Books in the shop: " + books.count());
books.findAll().stream().limit(3)
.forEach(b -> System.out.println(b.getId() + ": " + b.getTitle()));
}
}Books in the shop: 8 1: Dune 2: The Hobbit 3: Circe
Also uses Book.java
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
private double price;
protected Book() {}
public Book(String title, String author, double price) {
this.title = title; this.author = author; this.price = price;
}
public Long getId() { return id; }
public String getTitle() { return title; }
public String getAuthor() { return author; }
public double getPrice() { return price; }
public void setTitle(String title) { this.title = title; }
public void setAuthor(String author) { this.author = author; }
public void setPrice(double price) { this.price = price; }
}Also uses BookRepository.java
public interface BookRepository extends JpaRepository<Book, Long> {
List<Book> findByAuthorContainingIgnoreCase(String author);
}Also uses CreateBook.java
public record CreateBook(
@NotBlank String title,
@NotBlank String author,
@Positive double price
) {}Also uses BookResponse.java
public record BookResponse(Long id, String title, String author, double price) {
static BookResponse from(Book b) {
return new BookResponse(b.getId(), b.getTitle(), b.getAuthor(), b.getPrice());
}
}Also uses NewPrice.java
public record NewPrice(@Positive double price) {}Also uses BookNotFoundException.java
@ResponseStatus(HttpStatus.NOT_FOUND)
public class BookNotFoundException extends RuntimeException {
public BookNotFoundException(long id) { super("Book " + id + " was not found"); }
}Also uses data.sql
INSERT INTO book (title, author, price) VALUES
('Dune', 'Frank Herbert', 9.99),
('The Hobbit', 'J.R.R. Tolkien', 8.99),
('Circe', 'Madeline Miller', 10.99),
('Children of Dune', 'Frank Herbert', 8.49),
('Song of Achilles', 'Madeline Miller', 9.49),
('The Fellowship of the Ring', 'J.R.R. Tolkien', 11.99),
('Emma', 'Jane Austen', 6.99),
('Persuasion', 'Jane Austen', 7.49);Also uses application.properties
spring.jpa.defer-datasource-initialization=trueSummary
The Book entity maps to a table, and records carry data in, and out. Data dot sql seeds eight books. Next, the repository, and the service.
Key takeaways- @Entity Book
- CreateBook, BookResponse
- data.sql