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

Practice #15

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
15c2a5a
adding example of JPA Entities
springframeworkguru Jul 20, 2022
efec0cf
adding relationships example
springframeworkguru Jul 20, 2022
c46332e
adding hibernate equity
springframeworkguru Jul 21, 2022
fbd64f7
adding hibernate equity
springframeworkguru Jul 21, 2022
a97fa5c
adding bootstrap
springframeworkguru Jul 21, 2022
d3b153d
adding publisher
springframeworkguru Jul 22, 2022
3841492
adding publisher adding save
springframeworkguru Jul 22, 2022
c61401f
Merge branch 'bootstrap-data' into assn-add-publisher
springframeworkguru Jul 22, 2022
ce25586
adding publisher relationship
springframeworkguru Jul 22, 2022
0d97c14
enable h2 console
springframeworkguru Jul 22, 2022
0c547bf
adding book service layer
springframeworkguru Jul 24, 2022
52bd3bf
adding book controller
springframeworkguru Jul 24, 2022
85651f8
adding list books
springframeworkguru Jul 24, 2022
28d096c
Merge branch 'main' into jpa-entities
springframeworkguru Feb 4, 2023
db0e571
Merge branch '1-jpa-entities' into jpa-relationships
springframeworkguru Feb 4, 2023
6caa7ed
Merge branch '2-jpa-relationships' into jpa-repositories
springframeworkguru Feb 4, 2023
c841ad7
Merge branch '3-jpa-repositories' into bootstrap-data
springframeworkguru Feb 4, 2023
5d0c81f
Merge branch '4-bootstrap-data' into assn-add-publisher
springframeworkguru Feb 4, 2023
f46beee
Merge branch '5-assn-add-publisher' into publisher-relationship
springframeworkguru Feb 4, 2023
c3e8e57
Merge branch '6-publisher-relationship' into enable-h2-console
springframeworkguru Feb 4, 2023
d0685ab
Merge branch '7-enable-h2-console' into add-book-service
springframeworkguru Feb 4, 2023
c8689c8
Merge branch '8-add-book-service' into add-controller
springframeworkguru Feb 4, 2023
480e592
Merge branch '9-add-controller' into add-books-listview
springframeworkguru Feb 4, 2023
4af908c
just added my name
PanGian97 Oct 7, 2023
f04997f
adding relationships example
springframeworkguru Jul 20, 2022
a687ed9
Implimented part 17
springframeworkguru Jul 20, 2022
fc03c82
Merge remote-tracking branch 'origin/3-jpa-repositories' into practice
PanGian97 Oct 10, 2023
f660fda
adding bootstrap
springframeworkguru Jul 21, 2022
2935b09
adding publisher adding save
springframeworkguru Jul 22, 2022
2141d48
Modified
PanGian97 Oct 26, 2023
9fc3ad5
Modified
PanGian97 Oct 26, 2023
3d8451d
adding publisher
springframeworkguru Jul 22, 2022
27bc013
Merge remote-tracking branch 'origin/6-publisher-relationship' into p…
PanGian97 Oct 26, 2023
f9a83e9
Modified
PanGian97 Oct 30, 2023
17bb46d
adding list books
springframeworkguru Jul 24, 2022
0fe1716
Merge remote-tracking branch 'origin/10-add-books-listview' into prac…
PanGian97 Oct 30, 2023
595dbfb
Modified
PanGian97 Oct 30, 2023
a9a8db4
Modified
PanGian97 Oct 30, 2023
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
30 changes: 27 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<version>3.0.0-M3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>guru.springframework</groupId>
<artifactId>spring-6-webapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-6-webapp</name>
<description>Spring 6 Web App</description>

<properties>
<java.version>17</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -27,6 +25,11 @@
<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 @@ -47,4 +50,25 @@
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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;

/**
* Created by jt, Spring Framework Guru.
*/
@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);


Publisher publisher = new Publisher();
publisher.setPublisherName("My Publisher");
publisher.setAddress("123 Main");
Publisher savedPublisher = publisherRepository.save(publisher);

dddSaved.setPublisher(savedPublisher);
noEJBSaved.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,24 @@
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;

/**
* Created by jt, Spring Framework Guru.
*/
@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,28 @@
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;

/**
* Created by jt, Spring Framework Guru.
*/
@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
@@ -0,0 +1,90 @@
package guru.springframework.spring6webapp.domain;

import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Set;

/**
* Created by jt, Spring Framework Guru.
*/
@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 Set<Book> getBooks() {
return books;
}

public void setBooks(Set<Book> 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 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;
}
}











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

import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Set;

/**
* Created by jt, Spring Framework Guru.
*/
@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<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;
}

public void setAuthors(Set<Author> 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 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;
}
}








Loading