Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lessonn 18 done #4

Open
wants to merge 19 commits into
base: 2-jpa-relationships
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand All @@ -37,6 +41,7 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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.AuthorRepository;
import guru.springframework.spring6webapp.repositories.BookRepository;
import guru.springframework.spring6webapp.repositories.PublisherRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class BootstrapData implements CommandLineRunner {
private final AuthorRepository authorRepository;
private final BookRepository bookRepository;
private final PublisherRepository publisherRepository;

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.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);
dddSaved.getAuthors().add(ericSaved);
noEJBSaved.getAuthors().add(rodSaved);

// my Task:
Publisher john = new Publisher();
john.setPublisherName("John");
john.setAddress("Warynskiego 6");
Publisher savedPublisher = publisherRepository.save(john);

dddSaved.setPublisher(savedPublisher);
noEJB.setPublisher(savedPublisher);

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());
System.out.println("Publisher Count: "+ publisherRepository.count());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package guru.springframework.spring6webapp.controllers;

import guru.springframework.spring6webapp.services.AuthorService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class AuthorController {
private final AuthorService authorService;

public AuthorController(AuthorService authorService) {
this.authorService = authorService;
}
@RequestMapping("/authors")
public String getAuthors(Model model){

model.addAttribute("authors", authorService.findAll());
return "authors";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package guru.springframework.spring6webapp.controllers;

import guru.springframework.spring6webapp.services.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class BookController {
private final BookService bookService;

public BookController(BookService bookService) {
this.bookService = bookService;
}

@RequestMapping("/books")
public String getBooks(Model model){

model.addAttribute("books", bookService.findAll());

return "books";
}
}
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.Set;

/**
Expand All @@ -17,7 +18,7 @@ public class Author {
private String lastName;

@ManyToMany(mappedBy = "authors")
private Set<Book> books;
private Set<Book> books = new HashSet<>();

public Set<Book> getBooks() {
return books;
Expand Down Expand Up @@ -50,4 +51,29 @@ public String getLastName() {
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 instanceof Author)) return false;

Author author = (Author) o;

return getId() != null ? getId().equals(author.getId()) : author.getId() == null;
}

@Override
public int hashCode() {
return getId() != null ? getId().hashCode() : 0;
}
}
41 changes: 39 additions & 2 deletions src/main/java/guru/springframework/spring6webapp/domain/Book.java
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.Set;

/**
Expand All @@ -19,7 +20,18 @@ public class Book {
@ManyToMany
@JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"),
inverseJoinColumns = @JoinColumn(name = "author_id"))
private Set<Author> authors;
private Set<Author> authors = new HashSet<>();

@ManyToOne
private Publisher publisher;

public Publisher getPublisher() {
return publisher;
}

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

public Set<Author> getAuthors() {
return authors;
Expand Down Expand Up @@ -52,4 +64,29 @@ public String getIsbn() {
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 instanceof Book)) return false;

Book book = (Book) o;

return getId() != null ? getId().equals(book.getId()) : book.getId() == null;
}

@Override
public int hashCode() {
return getId() != null ? getId().hashCode() : 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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 zip;

@OneToMany(mappedBy = "publisher")
private Set<Book> 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 getZip() {
return zip;
}

public void setZip(String zip) {
this.zip = zip;
}

@Override
public String toString() {
return "Publisher{" +
"id=" + id +
", publisherName='" + publisherName + '\'' +
", address='" + address + '\'' +
", city='" + city + '\'' +
", state='" + state + '\'' +
", zip='" + zip + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Publisher)) return false;

Publisher publisher = (Publisher) o;

return getId() != null ? getId().equals(publisher.getId()) : publisher.getId() == null;
}

@Override
public int hashCode() {
return getId() != null ? getId().hashCode() : 0;
}
}
Original file line number Diff line number Diff line change
@@ -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<Author, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package guru.springframework.spring6webapp.repositories;

import guru.springframework.spring6webapp.domain.Book;
import org.springframework.data.repository.CrudRepository;

public interface BookRepository extends CrudRepository<Book,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.Publisher;
import org.springframework.data.repository.CrudRepository;

public interface PublisherRepository extends CrudRepository<Publisher, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package guru.springframework.spring6webapp.services;

import guru.springframework.spring6webapp.domain.Author;

public interface AuthorService {
Iterable<Author> findAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package guru.springframework.spring6webapp.services;

import guru.springframework.spring6webapp.domain.Author;
import guru.springframework.spring6webapp.repositories.AuthorRepository;
import org.springframework.stereotype.Service;

@Service
public class AuthorServiceImpl implements AuthorService{
private final AuthorRepository authorRepository;

public AuthorServiceImpl(AuthorRepository authorRepository) {
this.authorRepository = authorRepository;
}

@Override
public Iterable<Author> findAll() {
return authorRepository.findAll();
}
}
Loading