Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand All @@ -37,6 +39,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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<Book> 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<Book> getBooks() {
return books;
}

public void setBooks(Set<Book> books) {
this.books = books;
}
}
51 changes: 51 additions & 0 deletions src/main/java/guru/springframework/spring6webapp/domain/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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<Author> 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<Author> getAuthors() {
return authors;
}

public void setAuthors(Set<Author> authors) {
this.authors = authors;
}
}
6 changes: 6 additions & 0 deletions web/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>