-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathBootstrapData.java
More file actions
68 lines (42 loc) · 1.85 KB
/
BootstrapData.java
File metadata and controls
68 lines (42 loc) · 1.85 KB
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
package guru.springframework.spring6webapp.bootstrap;
import guru.springframework.spring6webapp.domain.Author;
import guru.springframework.spring6webapp.domain.Book;
import guru.springframework.spring6webapp.repositories.AuthorRepository;
import guru.springframework.spring6webapp.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.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);
authorRepository.save(ericSaved);
authorRepository.save(rodSaved);
System.out.println("In Bootstrap");
System.out.println("Author Count: " + authorRepository.count());
System.out.println("Book Count:" + bookRepository.count());
}
}