diff --git a/spring-6-webapp/README.md b/spring-6-webapp/README.md new file mode 100644 index 00000000..75752f43 --- /dev/null +++ b/spring-6-webapp/README.md @@ -0,0 +1,2 @@ +# spring-6-webapp +Spring 6 Web App \ No newline at end of file diff --git a/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java new file mode 100644 index 00000000..e8ee6d69 --- /dev/null +++ b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -0,0 +1,76 @@ +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.repository.AuthorRepository; +import guru.springframework.spring6webapp.repository.BookRepository; +import guru.springframework.spring6webapp.repository.PublisherRepository; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +public class BootstrapData implements CommandLineRunner { + private final PublisherRepository publisherRepository; + private final AuthorRepository authorRepository; + private final BookRepository bookRepository; + + public BootstrapData(AuthorRepository authorRepository, BookRepository bookRepository, PublisherRepository publisherRepository) { + this.authorRepository = authorRepository; + this.bookRepository = bookRepository; + this.publisherRepository = publisherRepository; + } + + @Override + public void run(String... args) throws Exception { + Author eric = new Author(); + eric.setFirstName("Eric"); + eric.setFirstName("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.setFirstName("Johnson"); + + Book noEJB = new Book(); + noEJB.setTitle("J2EE Development without EJB"); + noEJB.setIsbn("54757587"); + + Author rodSaved = authorRepository.save(rod); + Book noEJBSaved = bookRepository.save(noEJB); + + ericSaved.getBooks().add(dddSaved); + rodSaved.getBooks().add(noEJBSaved); + + + Publisher publisher = new Publisher(); + publisher.setAddress("The city Of Books"); + publisher.setPublisherName("Amazon Books"); + publisher.setCity("New Youk"); + Publisher savedPublisher = publisherRepository.save(publisher); + + dddSaved.setPublisher(savedPublisher); + noEJBSaved.setPublisher(publisher); + + System.out.println("Publisher count: "); + System.out.println(publisherRepository.count()); + + + authorRepository.save(ericSaved); + authorRepository.save(rodSaved); + bookRepository.save(dddSaved); + bookRepository.save(noEJBSaved); + + + + System.out.println("In Bootstrap"); + System.out.println("Author count: " + authorRepository.count()); + System.out.println("Book count: " + bookRepository.count()); + + } +} diff --git a/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/domain/Author.java new file mode 100644 index 00000000..3230a7f3 --- /dev/null +++ b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -0,0 +1,74 @@ +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 boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Author author)) return false; + + return getId() != null ? getId().equals(author.getId()) : author.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } + + + @Override + public String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", books=" + books + + '}'; + } +} diff --git a/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/domain/Book.java new file mode 100644 index 00000000..6230606c --- /dev/null +++ b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -0,0 +1,103 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.*; + +import java.util.HashSet; +import java.util.Set; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String title; + private String isbn; + + @ManyToOne + private Publisher publisher; + + public Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + this.publisher = publisher; + } + + @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 author) { + this.isbn = author; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Book book)) return false; + + return getId() != null ? getId().equals(book.getId()) : book.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", author='" + isbn + '\'' + + ", authors=" + authors + + '}'; + } +} + + + + + + + + + + + + + + diff --git a/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java new file mode 100644 index 00000000..5528300c --- /dev/null +++ b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java @@ -0,0 +1,101 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.*; + +import java.util.Set; + +@Entity +public class Publisher { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String publisherName; + private String address; + private String city; + private String state; + private String zipCode; + + @OneToMany(mappedBy = "publisher") + private Set books; + + 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 getPublisherName() { + return publisherName; + } + + public void setPublisherName(String publisherName) { + this.publisherName = publisherName; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getZipCode() { + return zipCode; + } + + public void setZipCode(String zip) { + this.zipCode = zip; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Publisher publisher)) return false; + + return getId() != null ? getId().equals(publisher.getId()) : publisher.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } + + @Override + public String toString() { + return "Publisher{" + + "id=" + id + + ", publisherName='" + publisherName + '\'' + + ", address='" + address + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", zip='" + zipCode + '\'' + + '}'; + } +} diff --git a/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/repository/AuthorRepository.java b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/repository/AuthorRepository.java new file mode 100644 index 00000000..b2686cdf --- /dev/null +++ b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/repository/AuthorRepository.java @@ -0,0 +1,8 @@ +package guru.springframework.spring6webapp.repository; + +import guru.springframework.spring6webapp.domain.Author; +import org.springframework.data.repository.CrudRepository; + +public interface AuthorRepository extends CrudRepository { + +} diff --git a/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/repository/BookRepository.java b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/repository/BookRepository.java new file mode 100644 index 00000000..5b019890 --- /dev/null +++ b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/repository/BookRepository.java @@ -0,0 +1,8 @@ +package guru.springframework.spring6webapp.repository; + +import guru.springframework.spring6webapp.domain.Book; +import org.springframework.data.repository.CrudRepository; + +public interface BookRepository extends CrudRepository { + +} diff --git a/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/repository/PublisherRepository.java b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/repository/PublisherRepository.java new file mode 100644 index 00000000..ac5c7932 --- /dev/null +++ b/spring-6-webapp/src/main/java/guru/springframework/spring6webapp/repository/PublisherRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring6webapp.repository; + +import guru.springframework.spring6webapp.domain.Publisher; +import org.springframework.data.repository.CrudRepository; + +public interface PublisherRepository extends CrudRepository { +} 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 00000000..b3abed06 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -0,0 +1,43 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +/** + * Created by jt, Spring Framework Guru. + */ +@Entity +public class Author { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String firstName; + private String lastName; + + 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; + } +} 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 00000000..7ff68c4c --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -0,0 +1,43 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +/** + * Created by jt, Spring Framework Guru. + */ +@Entity +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String title; + private String isbn; + + 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; + } +}