forked from springframeworkguru/spring5webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BootStrapData.java
61 lines (49 loc) · 2.25 KB
/
BootStrapData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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;
/**
* Created by jt on 12/23/19.
*/
@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", "Evans");
Book ddd = new Book("Domain Driven Design", "123123");
eric.getBooks().add(ddd);
ddd.getAuthors().add(eric);
Author rod = new Author("Rod", "Johnson");
Book noEJB = new Book("J2EE Development without EJB", "3939459459");
rod.getBooks().add(noEJB);
noEJB.getAuthors().add(rod);
authorRepository.save(eric);
authorRepository.save(rod);
bookRepository.save(ddd);
bookRepository.save(noEJB);
Publisher oreilly = new Publisher("OReilly", "1200 Maple Drive", "CA", "99870");
publisherRepository.save(oreilly);
oreilly.getBooks().add(ddd);
oreilly.getBooks().add(noEJB);
ddd.setPublisher(oreilly);
noEJB.setPublisher(oreilly);
bookRepository.save(ddd);
bookRepository.save(noEJB);
System.out.println("Started in Bootstrap");
System.out.println("Number of Books: " + bookRepository.count());
bookRepository.findAll().forEach(book->{System.out.println(book.getPublisher().getName());});
}
}