Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: springframeworkguru/spring5webapp
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: DraconYale/spring5webapp
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 10 commits
  • 12 files changed
  • 1 contributor

Commits on Apr 13, 2024

  1. JPA Entities

    DraconYale committed Apr 13, 2024
    Copy the full SHA
    aaa218e View commit details
  2. Equality in Hibernate

    DraconYale committed Apr 13, 2024
    Copy the full SHA
    0374c92 View commit details
  3. Spring Data Repositories

    DraconYale committed Apr 13, 2024
    Copy the full SHA
    8b043ff View commit details
  4. Copy the full SHA
    7a3cd39 View commit details
  5. Add Publisher Entity

    DraconYale committed Apr 13, 2024
    Copy the full SHA
    cdae793 View commit details
  6. Publisher Relationships

    DraconYale committed Apr 13, 2024
    Copy the full SHA
    d3526da View commit details

Commits on Apr 14, 2024

  1. H2 Database console

    DraconYale committed Apr 14, 2024
    Copy the full SHA
    80f6c35 View commit details
  2. Copy the full SHA
    324b0d6 View commit details
  3. Thymeleaf Templates

    DraconYale committed Apr 14, 2024
    Copy the full SHA
    25ec88f View commit details
  4. Display List of Authors

    DraconYale committed Apr 14, 2024
    Copy the full SHA
    803c1c7 View commit details
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package guru.springframework.spring5webapp.bootstrap;

import guru.springframework.spring5webapp.domain.Author;
import guru.springframework.spring5webapp.domain.Book;
import guru.springframework.spring5webapp.domain.Publisher;
import guru.springframework.spring5webapp.repositories.AuthorRepository;
import guru.springframework.spring5webapp.repositories.BookRepository;
import guru.springframework.spring5webapp.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 {


System.out.println("Started in Bootstrap");


Author eric = new Author("Eric", "Evans");
Book ddd = new Book("Domain Driven Design", "123123");
eric.getBooks().add(ddd);
ddd.getAuthors().add(eric);

Publisher publisher = new Publisher();
publisher.setName("SFG Publishing");
publisher.setCity("St Petersburg");
publisher.setState("FL");

publisherRepository.save(publisher);

System.out.println("Publisher Count: " + publisherRepository.count());

ddd.setPublisher(publisher);
publisher.getBooks().add(ddd);

authorRepository.save(eric);
bookRepository.save(ddd);
publisherRepository.save(publisher);

Author rod = new Author("Rod", "Johnson");
Book noEJB = new Book("J2EE Development without EJB", "4423241241");
rod.getBooks().add(noEJB);
noEJB.getAuthors().add(rod);

noEJB.setPublisher(publisher);
publisher.getBooks().add(noEJB);

authorRepository.save(rod);
bookRepository.save(noEJB);
publisherRepository.save(publisher);

System.out.println("Number of Books: " + bookRepository.count());
System.out.println("Publisher number of books: " + publisher.getBooks().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package guru.springframework.spring5webapp.controllers;

import guru.springframework.spring5webapp.repositories.AuthorRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class AuthorsController {

private final AuthorRepository authorRepository;

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

@RequestMapping("/authors")
public String getAuthors(Model model){

model.addAttribute("authors", authorRepository.findAll());

return "authors/list";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package guru.springframework.spring5webapp.controllers;

import guru.springframework.spring5webapp.repositories.BookRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class BookController {

private final BookRepository bookRepository;

public BookController(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}

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

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

return "books/list";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package guru.springframework.spring5webapp.domain;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
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<Book> books = new HashSet<>();

public Author(){
}

public Author(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = 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;
}

public Set<Book> getBooks() {
return books;
}

public void setBooks(Set<Book> books) {
this.books = books;
}

@Override
public String toString() {
return "Author{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Author author = (Author) o;

return Objects.equals(id, author.id);
}

@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}


}
95 changes: 95 additions & 0 deletions src/main/java/guru/springframework/spring5webapp/domain/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package guru.springframework.spring5webapp.domain;

import javax.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;

@ManyToOne
private Publisher publisher;

@ManyToMany
@JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"),
inverseJoinColumns = @JoinColumn(name = "author_id"))
private Set<Author> authors = new HashSet<>();

public Book() {
}

public Book(String title, String isbn) {
this.title = title;
this.isbn = isbn;
}

public Publisher getPublisher() {
return publisher;
}

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

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;
}

public Set<Author> getAuthors() {
return authors;
}

public void setAuthors(Set<Author> authors) {
this.authors = authors;
}

@Override
public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", isbn='" + isbn + '\'' +
'}';
}

@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;
}
}
Loading