6
6
import org .springframework .boot .autoconfigure .SpringBootApplication ;
7
7
import org .springframework .context .annotation .Bean ;
8
8
9
+ import java .time .LocalDateTime ;
9
10
import java .util .List ;
10
11
11
12
@ SpringBootApplication
@@ -15,15 +16,13 @@ public static void main(String[] args) {
15
16
SpringApplication .run (Application .class , args );
16
17
}
17
18
18
- /*Code running after the application started*/
19
19
@ Bean
20
- CommandLineRunner commandLineRunner (StudentRepository studentRepository ) {
21
- return args -> generateRandomStudents (studentRepository );
22
- }
20
+ CommandLineRunner commandLineRunner (
21
+ StudentRepository studentRepository ,
22
+ StudentIdCardRepository studentIdCardRepository ) {
23
+ return args -> {
24
+ Faker faker = new Faker ();
23
25
24
- private void generateRandomStudents (StudentRepository studentRepository ) {
25
- Faker faker = new Faker ();
26
- for (int i = 0 ; i < 20 ; i ++) {
27
26
String firstName = faker .name ().firstName ();
28
27
String lastName = faker .name ().lastName ();
29
28
String email =
String .
format (
"%s.%[email protected] " ,
firstName ,
lastName );
@@ -32,8 +31,57 @@ private void generateRandomStudents(StudentRepository studentRepository) {
32
31
lastName ,
33
32
email ,
34
33
faker .number ().numberBetween (17 , 55 ));
34
+
35
+ student .addBook (
36
+ new Book ("Clean Code" , LocalDateTime .now ().minusDays (4 )));
37
+
38
+ student .addBook (
39
+ new Book ("Think and Grow Rich" , LocalDateTime .now ()));
40
+
41
+ student .addBook (
42
+ new Book ("Spring Data JPA" , LocalDateTime .now ().minusYears (1 )));
43
+
44
+ StudentIdCard studentIdCard = new StudentIdCard (
45
+ "123456789" ,
46
+ student );
47
+
48
+ student .setStudentIdCard (studentIdCard );
49
+
50
+ student .addEnrollment (new Enrollment (
51
+ new EnrollmentId (1L , 1L ),
52
+ student ,
53
+ new Course ("Computer Science" , "IT" ),
54
+ LocalDateTime .now ()
55
+ ));
56
+
57
+ student .addEnrollment (new Enrollment (
58
+ new EnrollmentId (1L , 2L ),
59
+ student ,
60
+ new Course ("Amigoscode Spring Data JPA" , "IT" ),
61
+ LocalDateTime .now ().minusDays (18 )
62
+ ));
63
+
64
+ student .addEnrollment (new Enrollment (
65
+ new EnrollmentId (1L , 2L ),
66
+ student ,
67
+ new Course ("Amigoscode Spring Data JPA" , "IT" ),
68
+ LocalDateTime .now ().minusDays (18 )
69
+ ));
70
+
71
+
72
+
35
73
studentRepository .save (student );
36
- }
37
- }
38
74
39
- }
75
+ studentRepository .findById (1L )
76
+ .ifPresent (s -> {
77
+ System .out .println ("fetch book lazy..." );
78
+ List <Book > books = student .getBooks ();
79
+ books .forEach (book -> {
80
+ System .out .println (
81
+ s .getFirstName () + " borrowed " + book .getBookName ());
82
+ });
83
+ });
84
+
85
+ };
86
+ }
87
+ }
0 commit comments