From fbd7fd2d5735f575d48646b213338bf57d605108 Mon Sep 17 00:00:00 2001 From: JOAONUNES96 <116366430+JOAONUNES96@users.noreply.github.com> Date: Mon, 12 Jun 2023 14:54:30 +0100 Subject: [PATCH 1/5] Create Book.java --- .../spring5webapp/domain/Book.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/main/java/guru/springframework/spring5webapp/domain/Book.java diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Book.java b/src/main/java/guru/springframework/spring5webapp/domain/Book.java new file mode 100644 index 0000000000..19ab3e268a --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -0,0 +1,57 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String title; + private String isbn; + private String publisher; + + public Book() { + } + + public Book(String title, String isbn, String publisher) { + this.title = title; + this.isbn = isbn; + this.publisher = publisher; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + public String getPublisher() { + return publisher; + } + + public void setPublisher(String publisher) { + this.publisher = publisher; + } +} From 8a06d86d7d2a12844519301c5b372100c5d36818 Mon Sep 17 00:00:00 2001 From: JOAONUNES96 <116366430+JOAONUNES96@users.noreply.github.com> Date: Mon, 12 Jun 2023 15:36:24 +0100 Subject: [PATCH 2/5] Update Book.java --- .../spring5webapp/domain/Book.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Book.java b/src/main/java/guru/springframework/spring5webapp/domain/Book.java index 19ab3e268a..5ddf014346 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -4,6 +4,7 @@ import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import java.util.Objects; @Entity public class Book { @@ -17,6 +18,21 @@ public class Book { public Book() { } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Book book = (Book) o; + + return Objects.equals(id, book.id); + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } + public Book(String title, String isbn, String publisher) { this.title = title; this.isbn = isbn; From eb3d2238e9e8f9a5d25fda4b7666303f8c745488 Mon Sep 17 00:00:00 2001 From: JOAONUNES96 <116366430+JOAONUNES96@users.noreply.github.com> Date: Mon, 12 Jun 2023 16:10:16 +0100 Subject: [PATCH 3/5] Initializing Data with Spring --- .../bootstrap/DataInitializer.java | 40 +++++++++++++++++++ .../repositories/BookRepository.java | 9 +++++ 2 files changed, 49 insertions(+) create mode 100644 src/main/java/guru/springframework/spring5webapp/bootstrap/DataInitializer.java create mode 100644 src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/DataInitializer.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/DataInitializer.java new file mode 100644 index 0000000000..bd06e26e39 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/DataInitializer.java @@ -0,0 +1,40 @@ +package guru.springframework.spring5webapp.bootstrap; + +import guru.springframework.spring5webapp.domain.Book; +import guru.springframework.spring5webapp.repositories.BookRepository; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +/** + * Created by João Nunes on 12/06/2023. + */ +@Component +public class DataInitializer implements CommandLineRunner { + + private final BookRepository bookRepository; + + public DataInitializer(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + + @Override + public void run(String... args) throws Exception { + Book bookDDD = new Book("Domain Driven Design", "123", "RandomHouse"); + + System.out.println("Id: " + bookDDD.getId()); + + Book savedDDD = bookRepository.save(bookDDD); + + System.out.println("Id: " + savedDDD.getId()); + + Book bookSIA = new Book("Spring in Action", "123", "RandomHouse"); + + Book savedSIA = bookRepository.save(bookSIA); + + bookRepository.findAll().forEach(book -> { + System.out.println("Book Id: " + book.getId()); + System.out.println("Book Title: " + book.getTitle()); + }); + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java new file mode 100644 index 0000000000..66460e728b --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java @@ -0,0 +1,9 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.domain.Book; +import org.springframework.data.jpa.repository.JpaRepository; + + +// I could also have used the annotations +public interface BookRepository extends JpaRepository { +} From d247cced9c0b4aeb7e7f2e2f196c1baa578dae1d Mon Sep 17 00:00:00 2001 From: JOAONUNES96 <116366430+JOAONUNES96@users.noreply.github.com> Date: Mon, 12 Jun 2023 17:45:57 +0100 Subject: [PATCH 4/5] H2 Database Console --- pom.xml | 2 +- src/main/resources/application.properties | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f34c1936df..eb12eaf80d 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.2.RELEASE + 2.1.0.RELEASE diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29bb2..b1d26ba35c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1,16 @@ +#spring.jpa.show-sql=true + +#Show SQL +spring.jpa.properties.hibernate.show_sql=true + +#Format SQL +spring.jpa.properties.hibernate.format_sql=true + +#Show bind values +logging.level.org.hibernate.type.descriptor.sql=trace + +spring.h2.console.enabled=true + + + + From 81ff89defe0ebe4b780fb3f737f6b585122b571d Mon Sep 17 00:00:00 2001 From: JOAONUNES96 <116366430+JOAONUNES96@users.noreply.github.com> Date: Tue, 13 Jun 2023 14:29:08 +0100 Subject: [PATCH 5/5] Update Spring5webappApplicationTests.java --- .../Spring5webappApplicationTests.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/test/java/guru/springframework/spring5webapp/Spring5webappApplicationTests.java b/src/test/java/guru/springframework/spring5webapp/Spring5webappApplicationTests.java index e78ac1b703..2c7184d61c 100644 --- a/src/test/java/guru/springframework/spring5webapp/Spring5webappApplicationTests.java +++ b/src/test/java/guru/springframework/spring5webapp/Spring5webappApplicationTests.java @@ -1,16 +1,26 @@ package guru.springframework.spring5webapp; +import guru.springframework.spring5webapp.repositories.BookRepository; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + @RunWith(SpringRunner.class) @SpringBootTest public class Spring5webappApplicationTests { - @Test - public void contextLoads() { - } + @Autowired + BookRepository bookRepository; + + @Test + public void contextLoads() { + long count = bookRepository.count(); + assertThat(count).isGreaterThan(0); + + } }