From 7079f8fa78ab6af0f73d4e27c0e074be3d4c48e8 Mon Sep 17 00:00:00 2001 From: Boutachrafine Date: Sun, 3 Aug 2025 14:03:14 +0100 Subject: [PATCH 1/2] Adding JPA Entities --- pom.xml | 5 ++ .../spring6webapp/domain/Author.java | 49 +++++++++++++++++ .../spring6webapp/domain/Book.java | 52 +++++++++++++++++++ web/WEB-INF/web.xml | 6 +++ 4 files changed, 112 insertions(+) create mode 100644 src/main/java/guru/springframework/spring6webapp/domain/Author.java create mode 100644 src/main/java/guru/springframework/spring6webapp/domain/Book.java create mode 100644 web/WEB-INF/web.xml diff --git a/pom.xml b/pom.xml index 922b9c237..a3ff74962 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,11 @@ spring-boot-starter-test test + + jakarta.persistence + jakarta.persistence-api + 3.1.0 + diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java new file mode 100644 index 000000000..dccb300e0 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -0,0 +1,49 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.*; +import java.util.Set; + +@Entity // this annotation indicates that this class is a JPA entity +public class Author { + + @Id // This annotation indicates that this field is the primary key of the entity + @GeneratedValue (strategy = GenerationType.AUTO) // this annotation specifies that the primary key will be generated automatically + private Long idAuthor; + private String firstName; + private String lastName; + + @ManyToMany(mappedBy = "authors") // this annotation indicates a many-to-many relationship with the Book entity + private Set books; + + public Long getIdAuthor() { + return idAuthor; + } + + public void setIdAuthor(Long idAuthor) { + this.idAuthor = idAuthor; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java new file mode 100644 index 000000000..a3834c2b4 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -0,0 +1,52 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.*; + +import java.util.Set; + +@Entity +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long idBook; + private String title; + private String isbn; + + @ManyToMany + @JoinTable(name ="author_book", joinColumns = @JoinColumn(name = "book_id"), + inverseJoinColumns = @JoinColumn(name = "author_id")) // This annotation defines the join table for the many-to-many relationship + private Set authors; + + public Long getIdBook() { + return idBook; + } + + public void setIdBook(Long idBook) { + this.idBook = idBook; + } + + 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 Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } +} diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml new file mode 100644 index 000000000..d80081d13 --- /dev/null +++ b/web/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file From fe09d4ec2497f85f8a4063cc396e737c8ebadc74 Mon Sep 17 00:00:00 2001 From: Boutachrafine Date: Sun, 3 Aug 2025 14:10:29 +0100 Subject: [PATCH 2/2] Adding JPA entities --- pom.xml | 3 +++ .../java/guru/springframework/spring6webapp/domain/Author.java | 1 - .../java/guru/springframework/spring6webapp/domain/Book.java | 1 - 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index a3ff74962..3dc3f576b 100644 --- a/pom.xml +++ b/pom.xml @@ -23,10 +23,12 @@ org.springframework.boot spring-boot-starter-data-jpa + org.springframework.boot spring-boot-starter-web + com.h2database h2 @@ -37,6 +39,7 @@ spring-boot-starter-test test + jakarta.persistence jakarta.persistence-api diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index dccb300e0..f2d72db16 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -5,7 +5,6 @@ @Entity // this annotation indicates that this class is a JPA entity public class Author { - @Id // This annotation indicates that this field is the primary key of the entity @GeneratedValue (strategy = GenerationType.AUTO) // this annotation specifies that the primary key will be generated automatically private Long idAuthor; diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index a3834c2b4..294698c78 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -6,7 +6,6 @@ @Entity public class Book { - @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long idBook;