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

Eddig működik #253

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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 a Bootstrap");
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());

Author eric= new Author("Eric","Evans");
Book ddd= new Book("Domain driven design","12345");
eric.getBooks().add(ddd);
ddd.getAuthors().add(eric);

//miután beállítottuk a Book és a PUblisher közötti sok-egy kapcsolatot, és Joincolumnot is beállítottuk
ddd.setPublisher(publisher);
publisher.getBooks().add(ddd);


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


Author lewis= new Author("C.S","Lewis");
Book sbj= new Book("Suprised by joy","12347");
lewis.getBooks().add(sbj);
sbj.getAuthors().add(lewis);

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

// fontos a mentési sorrend ha 'publisherRepository.save(publisher);' feljebb van nem működik
authorRepository.save(lewis);
bookRepository.save(sbj);
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,29 @@
package guru.springframework.spring5webapp.controllers;

// Spring MVC 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";
}


}
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.Set;
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO) //automatikus id generálás (mint az adatb-ban)
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 + '\'' +
", books=" + books +
'}';
}

@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;
}
}
98 changes: 98 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,98 @@
package guru.springframework.spring5webapp.domain;

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;
@ManyToOne // sok könyv lehet, de egy könyvhöz csak egy kiadó tartozik
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 + '\'' +
", authors=" + authors +
'}';
}

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