Skip to content

Commit

Permalink
Initializing Data wih Spring
Browse files Browse the repository at this point in the history
  • Loading branch information
DraconYale committed Apr 13, 2024
1 parent 8b043ff commit 7a3cd39
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package guru.springframework.spring5webapp.bootstrap;

import guru.springframework.spring5webapp.domain.Author;
import guru.springframework.spring5webapp.domain.Book;
import guru.springframework.spring5webapp.repositories.AuthorRepository;
import guru.springframework.spring5webapp.repositories.BookRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class BootStrapData implements CommandLineRunner {

private final AuthorRepository authorRepository;
private final BookRepository bookRepository;

public BootStrapData(AuthorRepository authorRepository, BookRepository bookRepository) {
this.authorRepository = authorRepository;
this.bookRepository = bookRepository;
}

@Override
public void run(String... args) throws Exception {

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

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

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

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

System.out.println("Started in Bootstrap");
System.out.println("Number of Books: " + bookRepository.count());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package guru.springframework.spring5webapp.domain;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

Expand All @@ -12,15 +13,14 @@ public class Author {
private String firstName;
private String lastName;
@ManyToMany(mappedBy = "authors")
private Set<Book> books;
private Set<Book> books = new HashSet<>();

public Author(){
}

public Author(String firstName, String lastName, Set<Book> books) {
public Author(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.books = books;
}

public Long getId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package guru.springframework.spring5webapp.domain;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
@Entity
Expand All @@ -14,15 +15,14 @@ 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<>();

public Book() {
}

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

public Long getId() {
Expand Down

0 comments on commit 7a3cd39

Please sign in to comment.