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,89 @@
package guru.springframework.spring6webapp.bootstrap;

import guru.springframework.spring6webapp.domain.Author;
import guru.springframework.spring6webapp.domain.Book;
import guru.springframework.spring6webapp.domain.Publisher;
import guru.springframework.spring6webapp.repositories.IAuthorRepositroy;
import guru.springframework.spring6webapp.repositories.IBookRepository;
import guru.springframework.spring6webapp.repositories.IPublisherRepository;
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;
private final IPublisherRepository iPublisherRepository;

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


@Override
public void run(String... args) throws Exception {

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: " + iPublisherRepository.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());
}

}





90 changes: 90 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,90 @@
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<>();

@ManyToOne
private Publisher publisher;

public Publisher getPublisher() {
return publisher;
}

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

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

import jakarta.persistence.*;

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

@Entity
public class Publisher {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long idPublisher;
private String publisherName;
private String publisherAddress;
private String publisherCity;
private String publisherState;
private String publisherZip;

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

public Long getIdPublisher() {
return idPublisher;
}

public void setIdPublisher(Long idPublisher) {
this.idPublisher = idPublisher;
}

public String getPublisherName() {
return publisherName;
}

public void setPublisherName(String publisherName) {
this.publisherName = publisherName;
}

public String getPublisherAddress() {
return publisherAddress;
}

public void setPublisherAddress(String publisherAddress) {
this.publisherAddress = publisherAddress;
}

public String getPublisherCity() {
return publisherCity;
}

public void setPublisherCity(String publisherCity) {
this.publisherCity = publisherCity;
}

public String getPublisherState() {
return publisherState;
}

public void setPublisherState(String publisherState) {
this.publisherState = publisherState;
}

public String getPublisherZip() {
return publisherZip;
}

public void setPublisherZip(String publisherZip) {
this.publisherZip = publisherZip;
}

@Override
public String toString() {
return "Publisher{" +
"idPublisher=" + idPublisher +
", publisherName='" + publisherName + '\'' +
", publisherAddress='" + publisherAddress + '\'' +
", publisherCity='" + publisherCity + '\'' +
", publisherState='" + publisherState + '\'' +
", publisherZip='" + publisherZip + '\'' +
'}';
}

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

return Objects.equals(getIdPublisher(), publisher.getIdPublisher());
}

@Override
public int hashCode() {
return Objects.hashCode(getIdPublisher());
}
}
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> {
}
Loading