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
Expand Up @@ -5,7 +5,7 @@
import guru.springframework.spring6webapp.domain.Publisher;
import guru.springframework.spring6webapp.repositories.IAuthorRepositroy;
import guru.springframework.spring6webapp.repositories.IBookRepository;
import guru.springframework.spring6webapp.repositories.IPublisher;
import guru.springframework.spring6webapp.repositories.IPublisherRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

Expand All @@ -14,65 +14,71 @@ public class BootstrapData implements CommandLineRunner {

private final IAuthorRepositroy iAuthorRepositroy;
private final IBookRepository iBookRepository;
private final IPublisher iPublisher;
private final IPublisherRepository iPublisherRepository;

public BootstrapData(IAuthorRepositroy iAuthorRepositroy, IBookRepository iBookRepository, IPublisher iPublisher) {
public BootstrapData(IAuthorRepositroy iAuthorRepositroy, IBookRepository iBookRepository, IPublisherRepository iPublisherRepository) {
this.iAuthorRepositroy = iAuthorRepositroy;
this.iBookRepository = iBookRepository;
this.iPublisher = iPublisher;
this.iPublisherRepository = iPublisherRepository;
}


@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");

Publisher publisher = new Publisher();
publisher.setPublisherName("Spring Books Publishing");
publisher.setPublisherAddress("123 Spring St");
publisher.setPublisherCity("Springfield");
publisher.setPublisherState("Spring State");
publisher.setPublisherZip("12345");

Author authorSaved1 = iAuthorRepositroy.save(author1);
Book bookSaved1 = iBookRepository.save(book1);
Publisher publisherSaved = iPublisher.save(publisher);

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

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

Publisher publisher2 = new Publisher();
publisher2.setPublisherName("Spring Boot Books Publishing");
publisher2.setPublisherAddress("456 Spring Boot St");
publisher2.setPublisherCity("Spring Boot City");
publisher2.setPublisherState("Spring Boot State");
publisher2.setPublisherZip("54321");

Author authorSaved2 = iAuthorRepositroy.save(author2);
Book bookSaved2 = iBookRepository.save(book2);
Publisher publisherSaved2 = iPublisher.save(publisher2);

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

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

Author nabil = new Author(); // Create a new Author object
nabil.setFirstName("Nabil");
nabil.setLastName("Boutachrafine");

Book b_spring_f6 = new Book(); // Create a new Book object
b_spring_f6.setTitle("Spring Framework 6");
b_spring_f6.setIsbn("1234567890");

Publisher springPublisher = new Publisher(); // Create a new Publisher object
springPublisher.setPublisherName("Spring Books");
springPublisher.setPublisherAddress("123 Spring St");
springPublisher.setPublisherCity("Springfield");
springPublisher.setPublisherState("Spring State");
springPublisher.setPublisherZip("12345");

Author savedNabil = iAuthorRepositroy.save(nabil); // Save the Author object to the repository
Book savedBookSpring6 = iBookRepository.save(b_spring_f6); // Save the Book object to the repository
Publisher savedSpringPublisher = iPublisherRepository.save(springPublisher); // Save the Publisher object to the repository

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

Book book_java = new Book();
book_java.setTitle("Java Programming");
book_java.setIsbn("0987654321");

Publisher javaPublisher = new Publisher();
javaPublisher.setPublisherName("Java Books");
javaPublisher.setPublisherAddress("456 Java Ave");
javaPublisher.setPublisherCity("Java City");
javaPublisher.setPublisherState("Java State");
javaPublisher.setPublisherZip("67890");

Author savedTawfiq = iAuthorRepositroy.save(tawfiq); // Save the second Author object
Book savedBookJava = iBookRepository.save(book_java); // Save the second Book object
Publisher savedJavaPublisher = iPublisherRepository.save(javaPublisher); // Save the first Publisher object

savedNabil.getBooks().add(savedBookSpring6); // Add the first book to the first author
savedTawfiq.getBooks().add(savedBookJava); // Add the second book to the second author

savedBookSpring6.setPublisher(javaPublisher); // Set the publisher for the first book
savedBookJava.setPublisher(savedSpringPublisher); // Set the publisher for the second book

iAuthorRepositroy.save(savedNabil); // Save the updated first author
iAuthorRepositroy.save(savedTawfiq); // Save the updated second author
iBookRepository.save(savedBookSpring6); // Save the updated first book
iBookRepository.save(savedBookJava); // Save the updated second book

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

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/guru/springframework/spring6webapp/domain/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public class Book {
inverseJoinColumns = @JoinColumn(name = "author_id")) // This annotation defines the join table for the many-to-many relationship
private Set<Author> authors = new HashSet<>();

@ManyToOne
private Publisher publisher;

public Publisher getPublisher() {
return publisher;
}

public void setPublisher(Publisher publisher) {
this.publisher = publisher;
}

public Long getIdBook() {
return idBook;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package guru.springframework.spring6webapp.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;

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

@Entity
public class Publisher {
Expand All @@ -19,6 +17,9 @@ public class Publisher {
private String publisherState;
private String publisherZip;

@OneToMany(mappedBy = "publisher")
private Set<Book> books;

public Long getIdPublisher() {
return idPublisher;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import guru.springframework.spring6webapp.domain.Publisher;
import org.springframework.data.repository.CrudRepository;

public interface IPublisher extends CrudRepository<Publisher, Long> {
public interface IPublisherRepository extends CrudRepository<Publisher, Long> {
}