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

adding JPA use case #21

Open
wants to merge 5 commits into
base: master
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package guru.springframework.spring5webapp.bootstrap;

import guru.springframework.spring5webapp.model.Author;
import guru.springframework.spring5webapp.model.Book;
import guru.springframework.spring5webapp.model.Publisher;
import guru.springframework.spring5webapp.repositories.AuthorRepository;
import guru.springframework.spring5webapp.repositories.BookRepository;
import guru.springframework.spring5webapp.repositories.PublisherRepository;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class DevBootstrap implements ApplicationListener<ContextRefreshedEvent>{

private AuthorRepository authorRepository;
private BookRepository bookRepository;
private PublisherRepository publisherRepository;

public DevBootstrap(AuthorRepository authorRepository, BookRepository bookRepository, PublisherRepository publisherRepository) {

this.authorRepository = authorRepository;
this.bookRepository = bookRepository;
this.publisherRepository = publisherRepository;
}


@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
initData();
}

private void initData(){
Publisher ericBookPublisher=new Publisher("Pearson Education","California,USA");
publisherRepository.save(ericBookPublisher);

Author eric=new Author("Eric","Evans");
Book ericBook=new Book("Domain Driven Design","1234",ericBookPublisher);
eric.getBooks().add(ericBook);
ericBook.getAuthors().add(eric);

authorRepository.save(eric);
bookRepository.save(ericBook);

Publisher rodBookPublisher=new Publisher("Macgraw Hill","Seattle,USA");
publisherRepository.save(rodBookPublisher);

Book rodBook=new Book("J2E Development Without EJB","1234",rodBookPublisher);
Author rod=new Author("Rod","Johnson");
rod.getBooks().add(rodBook);
rodBook.getAuthors().add(rod);

authorRepository.save(rod);
bookRepository.save(rodBook);
}
}
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 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 AuthorController {

private AuthorRepository authorRepository;

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

@RequestMapping("/authors")
public String getBooks(Model model){
model.addAttribute("authors",authorRepository.findAll());
return "authors";
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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 BookRepository bookRepository;

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

@RequestMapping("/books")
public String getBooks(Model model){
model.addAttribute("books",bookRepository.findAll());
return "books";
}
}
82 changes: 82 additions & 0 deletions src/main/java/guru/springframework/spring5webapp/model/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package guru.springframework.spring5webapp.model;

import javax.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<Book> books = new HashSet<>();

public Author(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public Author() {
}

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 boolean equals(Object o) {

if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Author author = (Author) o;

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

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

@Override
public String toString() {

return "Author{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", books=" + books +
'}';
}
}
104 changes: 104 additions & 0 deletions src/main/java/guru/springframework/spring5webapp/model/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package guru.springframework.spring5webapp.model;

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

// @OneToOne(fetch = FetchType.LAZY,mappedBy="book")
@OneToOne
private Publisher publisher;

public Book(String title, String isbn, Publisher publisher, Set<Author> authors) {
this.title = title;
this.isbn = isbn;
this.publisher = publisher;
this.authors = authors;
}

public Book(String title, String isbn, Publisher publisher) {

this.title = title;
this.isbn = isbn;
this.publisher = publisher;
}

public Book() {
}

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

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 Set<Author> getAuthors() {
return authors;
}

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

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public Publisher getPublisher() {
return publisher;
}

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

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

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

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

@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", isbn='" + isbn + '\'' +
", publisher='" + publisher + '\'' +
", authors=" + authors +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package guru.springframework.spring5webapp.model;

import javax.persistence.*;

@Entity
public class Publisher {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String address;

public Publisher(String name, String address) {
this.name = name;
this.address = address;
}

public Publisher() {
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

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

Publisher publisher = (Publisher) o;

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

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

@Override
public String toString() {
return "Publisher{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package guru.springframework.spring5webapp.repositories;

import guru.springframework.spring5webapp.model.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,7 @@
package guru.springframework.spring5webapp.repositories;

import guru.springframework.spring5webapp.model.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.spring5webapp.repositories;

import guru.springframework.spring5webapp.model.Publisher;
import org.springframework.data.repository.CrudRepository;

public interface PublisherRepository extends CrudRepository<Publisher,Long>{
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.h2.console.enabled = true
Loading