Skip to content

Commit a97fa5c

Browse files
adding bootstrap
1 parent fbd64f7 commit a97fa5c

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package guru.springframework.spring6webapp.bootstrap;
2+
3+
import guru.springframework.spring6webapp.domain.Author;
4+
import guru.springframework.spring6webapp.domain.Book;
5+
import guru.springframework.spring6webapp.repositories.AuthorRepository;
6+
import guru.springframework.spring6webapp.repositories.BookRepository;
7+
import org.springframework.boot.CommandLineRunner;
8+
import org.springframework.stereotype.Component;
9+
10+
/**
11+
* Created by jt, Spring Framework Guru.
12+
*/
13+
@Component
14+
public class BootstrapData implements CommandLineRunner {
15+
16+
private final AuthorRepository authorRepository;
17+
private final BookRepository bookRepository;
18+
19+
public BootstrapData(AuthorRepository authorRepository, BookRepository bookRepository) {
20+
this.authorRepository = authorRepository;
21+
this.bookRepository = bookRepository;
22+
}
23+
24+
@Override
25+
public void run(String... args) throws Exception {
26+
Author eric = new Author();
27+
eric.setFirstName("Eric");
28+
eric.setLastName("Evans");
29+
30+
Book ddd = new Book();
31+
ddd.setTitle("Domain Driven Design");
32+
ddd.setIsbn("123456");
33+
34+
Author ericSaved = authorRepository.save(eric);
35+
Book dddSaved = bookRepository.save(ddd);
36+
37+
Author rod = new Author();
38+
rod.setFirstName("Rod");
39+
rod.setLastName("Johnson");
40+
41+
Book noEJB = new Book();
42+
noEJB.setTitle("J2EE Development without EJB");
43+
noEJB.setIsbn("54757585");
44+
45+
Author rodSaved = authorRepository.save(rod);
46+
Book noEJBSaved = bookRepository.save(noEJB);
47+
48+
ericSaved.getBooks().add(dddSaved);
49+
rodSaved.getBooks().add(noEJBSaved);
50+
51+
System.out.println("In Bootstrap");
52+
System.out.println("Author Count: " + authorRepository.count());
53+
System.out.println("Book Count: " + bookRepository.count());
54+
55+
56+
}
57+
}
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+

src/main/java/guru/springframework/spring6webapp/domain/Author.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import jakarta.persistence.*;
44

5+
import java.util.HashSet;
56
import java.util.Set;
67

78
/**
@@ -17,7 +18,7 @@ public class Author {
1718
private String lastName;
1819

1920
@ManyToMany(mappedBy = "authors")
20-
private Set<Book> books;
21+
private Set<Book> books = new HashSet<>();
2122

2223
public Set<Book> getBooks() {
2324
return books;

src/main/java/guru/springframework/spring6webapp/domain/Book.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import jakarta.persistence.*;
44

5+
import java.util.HashSet;
56
import java.util.Set;
67

78
/**
@@ -19,7 +20,7 @@ public class Book {
1920
@ManyToMany
2021
@JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"),
2122
inverseJoinColumns = @JoinColumn(name = "author_id"))
22-
private Set<Author> authors;
23+
private Set<Author> authors = new HashSet<>();
2324

2425
public Set<Author> getAuthors() {
2526
return authors;

0 commit comments

Comments
 (0)