-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathBootstrapData.java
More file actions
72 lines (58 loc) · 2.66 KB
/
BootstrapData.java
File metadata and controls
72 lines (58 loc) · 2.66 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
61
62
63
64
65
66
67
68
69
70
71
72
package guru.springframework.spring6webapp.bootstrap;
import guru.springframework.spring6webapp.domain.Author;
import guru.springframework.spring6webapp.domain.Book;
import guru.springframework.spring6webapp.domain.Publisher;
import guru.springframework.spring6webapp.repositories.AuthorRepository;
import guru.springframework.spring6webapp.repositories.BookRepository;
import guru.springframework.spring6webapp.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 {
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);
dddSaved.getAuthors().add(ericSaved);
noEJBSaved.getAuthors().add(rodSaved);
// my Task:
Publisher john = new Publisher();
john.setPublisherName("John");
john.setAddress("Warynskiego 6");
Publisher savedPublisher = publisherRepository.save(john);
dddSaved.setPublisher(savedPublisher);
noEJB.setPublisher(savedPublisher);
authorRepository.save(ericSaved);
authorRepository.save(rodSaved);
bookRepository.save(dddSaved);
bookRepository.save(noEJBSaved);
System.out.println("In Bootstrap");
System.out.println("Author Count: "+ authorRepository.count());
System.out.println("Book Count: "+ bookRepository.count());
System.out.println("Publisher Count: "+ publisherRepository.count());
}
}