Skip to content
Merged
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
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
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

Expand All @@ -14,7 +15,7 @@ public class Author {
private String lastName;

@ManyToMany(mappedBy = "authors") // this annotation indicates a many-to-many relationship with the Book entity
private Set<Book> books;
private Set<Book> books = new HashSet<>();

public Long getIdAuthor() {
return idAuthor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

Expand All @@ -16,7 +17,7 @@ public class Book {
@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;
private Set<Author> authors = new HashSet<>();

public Long getIdBook() {
return idBook;
Expand Down
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