|
1 | 1 | package com.example.hibernate;
|
2 | 2 |
|
3 |
| -import com.example.hibernate.model.Driver; |
4 |
| -import com.example.hibernate.model.DrivingLicense; |
5 |
| -import com.example.hibernate.repository.DriverRepository; |
6 |
| -import com.example.hibernate.repository.DrivingLicenseRepository; |
| 3 | +import com.example.hibernate.model.Gender; |
| 4 | +import com.example.hibernate.model.User; |
| 5 | +import com.example.hibernate.model.UserProfile; |
| 6 | +import com.example.hibernate.repository.UserRepository; |
| 7 | +import com.example.hibernate.repository.UserProfileRepository; |
7 | 8 | import org.springframework.beans.factory.annotation.Autowired;
|
8 | 9 | import org.springframework.boot.CommandLineRunner;
|
9 | 10 | import org.springframework.boot.SpringApplication;
|
10 | 11 | import org.springframework.boot.autoconfigure.SpringBootApplication;
|
| 12 | +import org.springframework.transaction.annotation.Transactional; |
11 | 13 |
|
12 | 14 | import java.util.Calendar;
|
13 | 15 |
|
14 | 16 | @SpringBootApplication
|
15 | 17 | public class HibernateOneToOneDemoApplication implements CommandLineRunner {
|
16 | 18 |
|
17 | 19 | @Autowired
|
18 |
| - private DriverRepository driverRepository; |
| 20 | + private UserRepository userRepository; |
19 | 21 |
|
20 | 22 | @Autowired
|
21 |
| - private DrivingLicenseRepository drivingLicenseRepository; |
| 23 | + private UserProfileRepository userProfileRepository; |
22 | 24 |
|
23 | 25 | public static void main(String[] args) {
|
24 | 26 | SpringApplication.run(HibernateOneToOneDemoApplication.class, args);
|
25 | 27 | }
|
26 | 28 |
|
27 | 29 | @Override
|
28 | 30 | public void run(String... args) throws Exception {
|
29 |
| - // Clean up database tables |
30 |
| - drivingLicenseRepository.deleteAllInBatch(); |
31 |
| - driverRepository.deleteAllInBatch(); |
| 31 | + // Clean up database tables |
| 32 | + userProfileRepository.deleteAllInBatch(); |
| 33 | + userRepository.deleteAllInBatch(); |
32 | 34 |
|
33 |
| - //========================================= |
| 35 | + //========================================= |
34 | 36 |
|
35 |
| - // Create a Driver instance |
36 |
| - Driver driver = new Driver("Rajeev Kumar Singh", "rajeev@callicoder.com", |
37 |
| - "+91-9999999999"); |
| 37 | + // Create a User instance |
| 38 | + User user = new User("Rajeev", "Singh", "rajeev@callicoder.com", |
| 39 | + "MY_SUPER_SECRET_PASSWORD"); |
38 | 40 |
|
39 |
| - // Create a Driving License instance |
40 |
| - Calendar issueDate = Calendar.getInstance(); |
41 |
| - issueDate.set(2017, 7, 21); |
| 41 | + Calendar dateOfBirth = Calendar.getInstance(); |
| 42 | + dateOfBirth.set(1992, 7, 21); |
42 | 43 |
|
43 |
| - Calendar expiryDate = Calendar.getInstance(); |
44 |
| - expiryDate.set(2027, 7, 21); |
| 44 | + // Create a UserProfile instance |
| 45 | + UserProfile userProfile = new UserProfile("+91-8197882053", Gender.MALE, dateOfBirth.getTime(), |
| 46 | + "747", "2nd Cross", "Golf View Road, Kodihalli", "Bangalore", |
| 47 | + "Karnataka", "India", "560008"); |
45 | 48 |
|
46 |
| - DrivingLicense drivingLicense = new DrivingLicense("MH-383321-323-8080", |
47 |
| - issueDate.getTime(), expiryDate.getTime()); |
48 | 49 |
|
49 |
| - // Set child reference(drivingLicense) in parent entity(driver) |
50 |
| - driver.setDrivingLicense(drivingLicense); |
| 50 | + // Set child reference(userProfile) in parent entity(user) |
| 51 | + user.setUserProfile(userProfile); |
51 | 52 |
|
52 |
| - // Set parent reference(driver) in child entity(drivingLicense) |
53 |
| - drivingLicense.setDriver(driver); |
| 53 | + // Set parent reference(user) in child entity(userProfile) |
| 54 | + userProfile.setUser(user); |
54 | 55 |
|
55 |
| - // Save Parent Reference (which will save the child as well) |
56 |
| - driverRepository.save(driver); |
| 56 | + // Save Parent Reference (which will save the child as well) |
| 57 | + userRepository.save(user); |
57 | 58 |
|
58 |
| - //========================================= |
| 59 | + //========================================= |
59 | 60 | }
|
| 61 | + |
| 62 | + |
60 | 63 | }
|
0 commit comments