diff --git a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java new file mode 100644 index 000000000..a2d184444 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -0,0 +1,68 @@ +package guru.springframework.spring6webapp.bootstrap; + +import guru.springframework.spring6webapp.domain.Author; +import guru.springframework.spring6webapp.domain.Book; +import guru.springframework.spring6webapp.repositories.AuthorRepository; +import guru.springframework.spring6webapp.repositories.BookRepository; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +public class BootstrapData implements CommandLineRunner { + + + private final AuthorRepository authorRepository; + private final BookRepository bookRepository; + + public BootstrapData(AuthorRepository authorRepository, BookRepository bookRepository) { + this.authorRepository = authorRepository; + this.bookRepository = bookRepository; + } + + @Override + public void run(String... args) throws Exception { + Author eric = new Author(); + eric.setFirstName("Eric"); + eric.setLastName("Evans"); + + Book ddd = new Book(); + ddd.setTitle("Domain Driven Design"); + ddd.setIsbn("123456"); + + + Author ericSaved = authorRepository.save(eric); + Book dddSaved = bookRepository.save(ddd); + + Author rod = new Author(); + rod.setFirstName("Rod"); + rod.setLastName("Johnson"); + + Book noEJB = new Book(); + noEJB.setTitle("J2EE Development without EJB"); + noEJB.setIsbn("54757585"); + + Author rodSaved = authorRepository.save(rod); + Book noEJBSaved = bookRepository.save(noEJB); + + ericSaved.getBooks().add(dddSaved); + rodSaved.getBooks().add(noEJBSaved); + + authorRepository.save(ericSaved); + authorRepository.save(rodSaved); + + System.out.println("In Bootstrap"); + System.out.println("Author Count: " + authorRepository.count()); + System.out.println("Book Count:" + bookRepository.count()); + + + + } +} + + + + + + + + diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java new file mode 100644 index 000000000..2878f9f80 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -0,0 +1,83 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.*; + +import java.util.HashSet; +import java.util.Set; + +@Entity +public class Author { + + @Id + @GeneratedValue(strategy= GenerationType.AUTO) + private long id; + private String firstName; + private String lastName; + + @ManyToMany(mappedBy = "authors") + private Set books = new HashSet<>(); + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + 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; + } + + + @Override + public String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", books=" + books + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Author author = (Author) o; + + return id == author.id; + } + + @Override + public int hashCode() { + return (int) (id ^ (id >>> 32)); + } +} + + + + + + diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java new file mode 100644 index 000000000..4e220d10f --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -0,0 +1,85 @@ +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 id; + private String title; + private String isbn; + + @ManyToMany + @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), + inverseJoinColumns = @JoinColumn(name = "author_id")) + private Set authors = new HashSet<>(); + + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + 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; + } + + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", isbn='" + isbn + '\'' + + ", authors=" + authors + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Book book = (Book) o; + + return Objects.equals(id, book.id); + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } +} + + + + + diff --git a/src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java b/src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java new file mode 100644 index 000000000..444b5fcd2 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring6webapp.repositories; + +import guru.springframework.spring6webapp.domain.Author; +import org.springframework.data.repository.CrudRepository; + +public interface AuthorRepository extends CrudRepository{ +} diff --git a/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java b/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java new file mode 100644 index 000000000..7524fa013 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring6webapp.repositories; + +import guru.springframework.spring6webapp.domain.Book; +import org.springframework.data.repository.CrudRepository; + +public interface BookRepository extends CrudRepository { +}