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
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<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 All @@ -45,6 +50,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package guru.springframework.spring6webapp.bootstrap;

import guru.springframework.spring6webapp.domain.Author;
import guru.springframework.spring6webapp.domain.Book;
import guru.springframework.spring6webapp.repositories.IAuthorRepositroy;
import guru.springframework.spring6webapp.repositories.IBookRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component // This annotation indicates that this class is a Spring component and will be automatically detected by Spring's component scanning
public class BootstrapData implements CommandLineRunner {

private final IAuthorRepositroy iAuthorRepositroy;
private final IBookRepository iBookRepository;

public BootstrapData(IAuthorRepositroy iAuthorRepositroy, IBookRepository iBookRepository) {
this.iAuthorRepositroy = iAuthorRepositroy;
this.iBookRepository = iBookRepository;
}


@Override
public void run(String... args) throws Exception {
Author author1 = new Author();
author1.setFirstName("Nabil");
author1.setLastName("Boutachrafine");

Book book1 = new Book();
book1.setTitle("Spring Framework 6");
book1.setIsbn("1234567890");

Author authorSaved1 = iAuthorRepositroy.save(author1);
Book bookSaved1 = iBookRepository.save(book1);

Author author2 = new Author();
author2.setFirstName("Tawfiq");
author2.setLastName("Boutachrafine");

Book book2 = new Book();
book2.setTitle("Spring Boot 3");
book2.setIsbn("0987654321");

Author authorSaved2 = iAuthorRepositroy.save(author2);
Book bookSaved2 = iBookRepository.save(book2);

authorSaved1.getBooks().add(bookSaved1);
authorSaved2.getBooks().add(bookSaved2);

iAuthorRepositroy.save(authorSaved1);
iAuthorRepositroy.save(authorSaved2);

System.out.println("Bootstrap Data Loaded");
System.out.println("Number of Authors: " + iAuthorRepositroy.count());
System.out.println("Number of Books: " + iBookRepository.count());
}
}






Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package guru.springframework.spring6webapp.domain;

import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Objects;
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 = new HashSet<>();

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;
}

@Override
public String toString() {
return "Author{" +
"idAuthor=" + idAuthor +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}

@Override
public final boolean equals(Object o) {
if (!(o instanceof Author author)) return false;

return Objects.equals(getIdAuthor(), author.getIdAuthor());
}

@Override
public int hashCode() {
return Objects.hashCode(getIdAuthor());
}

}





79 changes: 79 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,79 @@
package guru.springframework.spring6webapp.domain;

import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Objects;
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 = new HashSet<>();

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;
}

@Override
public String toString() {
return "Book{" +
"idBook=" + idBook +
", title='" + title + '\'' +
", isbn='" + isbn + '\'' +
'}';
}

@Override
public final boolean equals(Object o) {
if (!(o instanceof Book book)) return false;

return Objects.equals(getIdBook(), book.getIdBook());
}

@Override
public int hashCode() {
return Objects.hashCode(getIdBook());
}

}




Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package guru.springframework.spring6webapp.repositories;

import guru.springframework.spring6webapp.domain.Author;
import org.springframework.data.repository.CrudRepository;

public interface IAuthorRepositroy extends CrudRepository<Author, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package guru.springframework.spring6webapp.repositories;

import guru.springframework.spring6webapp.domain.Book;
import org.springframework.data.repository.CrudRepository;

public interface IBookRepository extends CrudRepository<Book, Long> {
}
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

server.port=9090
//spring.jpa.open-in-view=false
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>