From 3a2c693b0052c2b806ec639d696c94b985f3bba5 Mon Sep 17 00:00:00 2001
From: Hazar Gul Nazari <hazar.nazari@favendo.com>
Date: Tue, 20 Oct 2020 21:50:23 +0200
Subject: [PATCH 1/2] _refactor

---
 jpa-one-to-one-demo/Readme.md                 |  11 --
 jpa-one-to-one-demo/pom.xml                   |  16 +--
 .../jpa/JpaOneToOneDemoApplication.java       |  56 +++------
 .../main/java/com/example/jpa/model/User.java |  71 +----------
 .../com/example/jpa/model/UserProfile.java    | 115 +-----------------
 .../src/main/resources/application.properties |  21 ++--
 .../src/main/resources/data.sql               |   9 ++
 7 files changed, 56 insertions(+), 243 deletions(-)
 create mode 100644 jpa-one-to-one-demo/src/main/resources/data.sql

diff --git a/jpa-one-to-one-demo/Readme.md b/jpa-one-to-one-demo/Readme.md
index 88b4d9d..e69de29 100644
--- a/jpa-one-to-one-demo/Readme.md
+++ b/jpa-one-to-one-demo/Readme.md
@@ -1,11 +0,0 @@
-# Hibernate One to One Mapping Example with Spring Boot and JPA
-
-Read the Tutorial - https://www.callicoder.com/hibernate-spring-boot-jpa-one-to-one-mapping-example/
-
-## Setup the Application
-
-1. Create a database named `hibernate_one_to_one_demo`.
-
-2. Open `src/main/resources/application.properties` and change `spring.datasource.username` and `spring.datasource.password` properties as per your MySQL installation.
-
-3. Type `mvn spring-boot:run` from the root directory of the project to run the application.
\ No newline at end of file
diff --git a/jpa-one-to-one-demo/pom.xml b/jpa-one-to-one-demo/pom.xml
index e8c313d..da00b61 100644
--- a/jpa-one-to-one-demo/pom.xml
+++ b/jpa-one-to-one-demo/pom.xml
@@ -4,17 +4,15 @@
 	<modelVersion>4.0.0</modelVersion>
 
 	<groupId>com.example</groupId>
-	<artifactId>hibernate-one-to-one-demo</artifactId>
+	<artifactId>jpa-one-to-one-demo</artifactId>
 	<version>0.0.1-SNAPSHOT</version>
-	<packaging>jar</packaging>
-
 	<name>hibernate-one-to-one-demo</name>
 	<description>Demo project for Spring Boot</description>
 
 	<parent>
 		<groupId>org.springframework.boot</groupId>
 		<artifactId>spring-boot-starter-parent</artifactId>
-		<version>2.2.1.RELEASE</version>
+		<version>2.3.4.RELEASE</version>
 		<relativePath/> <!-- lookup parent from repository -->
 	</parent>
 
@@ -33,10 +31,14 @@
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-web</artifactId>
 		</dependency>
-
 		<dependency>
-			<groupId>mysql</groupId>
-			<artifactId>mysql-connector-java</artifactId>
+			<groupId>org.projectlombok</groupId>
+			<artifactId>lombok</artifactId>
+			<optional>true</optional>
+		</dependency>
+		<dependency>
+			<groupId>com.h2database</groupId>
+			<artifactId>h2</artifactId>
 			<scope>runtime</scope>
 		</dependency>
 		<dependency>
diff --git a/jpa-one-to-one-demo/src/main/java/com/example/jpa/JpaOneToOneDemoApplication.java b/jpa-one-to-one-demo/src/main/java/com/example/jpa/JpaOneToOneDemoApplication.java
index 72de4c6..08d9c85 100644
--- a/jpa-one-to-one-demo/src/main/java/com/example/jpa/JpaOneToOneDemoApplication.java
+++ b/jpa-one-to-one-demo/src/main/java/com/example/jpa/JpaOneToOneDemoApplication.java
@@ -1,61 +1,41 @@
 package com.example.jpa;
 
-import com.example.jpa.model.Gender;
 import com.example.jpa.model.User;
 import com.example.jpa.model.UserProfile;
-import com.example.jpa.repository.UserRepository;
 import com.example.jpa.repository.UserProfileRepository;
+import com.example.jpa.repository.UserRepository;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import java.util.Calendar;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.Collection;
+import java.util.List;
 
 @SpringBootApplication
-public class JpaOneToOneDemoApplication implements CommandLineRunner {
+@RestController
+public class JpaOneToOneDemoApplication {
+
 
 	@Autowired
 	private UserRepository userRepository;
 
 	@Autowired
-    private UserProfileRepository userProfileRepository;
+	private UserProfileRepository userProfileRepository;
 
 	public static void main(String[] args) {
 		SpringApplication.run(JpaOneToOneDemoApplication.class, args);
 	}
 
-	@Override
-	public void run(String... args) throws Exception {
-		// Clean up database tables
-		userProfileRepository.deleteAllInBatch();
-		userRepository.deleteAllInBatch();
-
-		//=========================================
-
-		// Create a User instance
-		User user = new User("Rajeev", "Singh", "rajeev@callicoder.com",
-				"MY_SUPER_SECRET_PASSWORD");
-
-		Calendar dateOfBirth = Calendar.getInstance();
-		dateOfBirth.set(1992, 7, 21);
-
-		// Create a UserProfile instance
-		UserProfile userProfile = new UserProfile("+91-8197882053", Gender.MALE, dateOfBirth.getTime(),
-				"747", "2nd Cross", "Golf View Road, Kodihalli", "Bangalore",
-				"Karnataka", "India", "560008");
-
-
-		// Set child reference(userProfile) in parent entity(user)
-		user.setUserProfile(userProfile);
-
-		// Set parent reference(user) in child entity(userProfile)
-		userProfile.setUser(user);
-
-		// Save Parent Reference (which will save the child as well)
-		userRepository.save(user);
-
-		//=========================================
+	@GetMapping("/users")
+	public Collection<User> getUsers(){
+		return userRepository.findAll();
 	}
 
-
+	@GetMapping("/user_profiles")
+	public Collection<UserProfile> getUserProfiles(){
+		return userProfileRepository.findAll();
+	}
 }
diff --git a/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/User.java b/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/User.java
index f41fc36..73cfb11 100644
--- a/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/User.java
+++ b/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/User.java
@@ -1,9 +1,10 @@
 package com.example.jpa.model;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
 import javax.persistence.*;
-import javax.validation.constraints.Email;
-import javax.validation.constraints.NotNull;
-import javax.validation.constraints.Size;
 import java.io.Serializable;
 
 /**
@@ -11,28 +12,22 @@
  */
 @Entity
 @Table(name = "users")
+@Data
+@NoArgsConstructor
 public class User implements Serializable {
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;
 
-    @NotNull
-    @Size(max = 65)
     @Column(name = "first_name")
     private String firstName;
 
-    @Size(max = 65)
     @Column(name = "last_name")
     private String lastName;
 
-    @NotNull
-    @Email
-    @Size(max = 100)
     @Column(unique = true)
     private String email;
 
-    @NotNull
-    @Size(max = 128)
     private String password;
 
     @OneToOne(fetch = FetchType.LAZY,
@@ -40,64 +35,10 @@ public class User implements Serializable {
             mappedBy = "user")
     private UserProfile userProfile;
 
-    // Hibernate requires a no-arg constructor
-    public User() {
-
-    }
-
     public User(String firstName, String lastName, String email, String password) {
         this.firstName = firstName;
         this.lastName = lastName;
         this.email = email;
         this.password = password;
     }
-
-    public Long getId() {
-        return id;
-    }
-
-    public void setId(Long id) {
-        this.id = id;
-    }
-
-    public String getFirstName() {
-        return firstName;
-    }
-
-    public void setFirstName(String firstName) {
-        this.firstName = firstName;
-    }
-
-    public String getLastName() {
-        return lastName;
-    }
-
-    public void setLastName(String lastName) {
-        this.lastName = lastName;
-    }
-
-    public String getEmail() {
-        return email;
-    }
-
-    public void setEmail(String email) {
-        this.email = email;
-    }
-
-    public String getPassword() {
-        return password;
-    }
-
-    public void setPassword(String password) {
-        this.password = password;
-    }
-
-
-    public UserProfile getUserProfile() {
-        return userProfile;
-    }
-
-    public void setUserProfile(UserProfile userProfile) {
-        this.userProfile = userProfile;
-    }
 }
diff --git a/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/UserProfile.java b/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/UserProfile.java
index 099a759..1074c11 100644
--- a/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/UserProfile.java
+++ b/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/UserProfile.java
@@ -1,7 +1,9 @@
 package com.example.jpa.model;
 
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
 import javax.persistence.*;
-import javax.validation.constraints.Size;
 import java.io.Serializable;
 import java.util.Date;
 
@@ -10,53 +12,42 @@
  */
 @Entity
 @Table(name = "user_profiles")
+@Data
+@NoArgsConstructor
 public class UserProfile implements Serializable {
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;
 
     @Column(name = "phone_number")
-    @Size(max = 15)
     private String phoneNumber;
 
     @Enumerated(EnumType.STRING)
-    @Column(length = 10)
     private Gender gender;
 
     @Temporal(TemporalType.DATE)
     @Column(name = "dob")
     private Date dateOfBirth;
 
-    @Size(max = 100)
     private String address1;
 
-    @Size(max = 100)
     private String address2;
 
-    @Size(max = 100)
     private String street;
 
-    @Size(max = 100)
     private String city;
 
-    @Size(max = 100)
     private String state;
 
-    @Size(max = 100)
     private String country;
 
     @Column(name = "zip_code")
-    @Size(max = 32)
     private String zipCode;
 
     @OneToOne(fetch = FetchType.LAZY, optional = false)
     @JoinColumn(name = "user_id", nullable = false)
     private User user;
 
-    public UserProfile() {
-
-    }
-
     public UserProfile(String phoneNumber, Gender gender, Date dateOfBirth,
                        String address1, String address2, String street, String city,
                        String state, String country, String zipCode) {
@@ -71,100 +62,4 @@ public UserProfile(String phoneNumber, Gender gender, Date dateOfBirth,
         this.country = country;
         this.zipCode = zipCode;
     }
-
-    public Long getId() {
-        return id;
-    }
-
-    public void setId(Long id) {
-        this.id = id;
-    }
-
-    public String getPhoneNumber() {
-        return phoneNumber;
-    }
-
-    public void setPhoneNumber(String phoneNumber) {
-        this.phoneNumber = phoneNumber;
-    }
-
-    public Gender getGender() {
-        return gender;
-    }
-
-    public void setGender(Gender gender) {
-        this.gender = gender;
-    }
-
-    public Date getDateOfBirth() {
-        return dateOfBirth;
-    }
-
-    public void setDateOfBirth(Date dateOfBirth) {
-        this.dateOfBirth = dateOfBirth;
-    }
-
-    public String getAddress1() {
-        return address1;
-    }
-
-    public void setAddress1(String address1) {
-        this.address1 = address1;
-    }
-
-    public String getAddress2() {
-        return address2;
-    }
-
-    public void setAddress2(String address2) {
-        this.address2 = address2;
-    }
-
-    public String getStreet() {
-        return street;
-    }
-
-    public void setStreet(String street) {
-        this.street = street;
-    }
-
-    public String getCity() {
-        return city;
-    }
-
-    public void setCity(String city) {
-        this.city = city;
-    }
-
-    public String getState() {
-        return state;
-    }
-
-    public void setState(String state) {
-        this.state = state;
-    }
-
-    public String getCountry() {
-        return country;
-    }
-
-    public void setCountry(String country) {
-        this.country = country;
-    }
-
-    public String getZipCode() {
-        return zipCode;
-    }
-
-    public void setZipCode(String zipCode) {
-        this.zipCode = zipCode;
-    }
-
-    public User getUser() {
-        return user;
-    }
-
-    public void setUser(User user) {
-        this.user = user;
-    }
 }
diff --git a/jpa-one-to-one-demo/src/main/resources/application.properties b/jpa-one-to-one-demo/src/main/resources/application.properties
index addc41d..42c733d 100644
--- a/jpa-one-to-one-demo/src/main/resources/application.properties
+++ b/jpa-one-to-one-demo/src/main/resources/application.properties
@@ -1,15 +1,12 @@
-# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
-spring.datasource.url=jdbc:mysql://localhost:3306/jpa_one_to_one_demo?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
-spring.datasource.username=root
-spring.datasource.password=callicoder
 
-# Hibernate
+spring.jpa.hibernate.ddl-auto=update
+spring.datasource.initialization-mode=always
 
-# The SQL dialect makes Hibernate generate better SQL for the chosen database
-spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
+spring.h2.console.enabled=true
+spring.datasource.platform=h2
+spring.datasource.driverClassName = org.h2.Driver
+spring.datasource.url=jdbc:h2:mem:testdb
+spring.datasource.username=sa
+spring.datasource.password=
 
-# Hibernate ddl auto (create, create-drop, validate, update)
-spring.jpa.hibernate.ddl-auto = update
-
-logging.level.org.hibernate.SQL=DEBUG
-logging.level.org.hibernate.type=TRACE
\ No newline at end of file
+spring.output.ansi.enabled=ALWAYS
\ No newline at end of file
diff --git a/jpa-one-to-one-demo/src/main/resources/data.sql b/jpa-one-to-one-demo/src/main/resources/data.sql
new file mode 100644
index 0000000..6c8a733
--- /dev/null
+++ b/jpa-one-to-one-demo/src/main/resources/data.sql
@@ -0,0 +1,9 @@
+
+INSERT INTO USERS VALUES (1, 'email1@example.com', 'firstname1', 'lastname1', 'password1'),
+                         (2, 'email2@example.com', 'firstname1', 'lastname1', 'password1'),
+                         (3, 'email3@example.com', 'firstname1', 'lastname1', 'password1');
+
+--ID  	ADDRESS1  	ADDRESS2  	CITY  	COUNTRY  	DOB  	GENDER  	PHONE_NUMBER  	STATE  	STREET  	ZIP_CODE  	USER_ID
+INSERT INTO USER_PROFILES VALUES (1, 'address 1', 'Address 1', 'City 1', 'Country 1', '1995-05-05', 'MALE', 'Phone Number 1', 'State 1', 'Street 1', '12545', 1),
+                                 (2, 'address 2', 'Address 2', 'City 2', 'Country 2', '1995-02-02', 'FEMALE', 'Phone Number 2', 'State 2', 'Street 2', '12545', 2),
+                                 (3, 'address 3', 'Address 3', 'City 3', 'Country 3', '1995-03-03', 'MALE', 'Phone Number 3', 'State 3', 'Street 3', '12545', 3);

From 54fba95ac9539dd48443cb6ef5b9b5ae5b8e482a Mon Sep 17 00:00:00 2001
From: Hazar Gul Nazari <hazar.nazari@favendo.com>
Date: Tue, 20 Oct 2020 22:17:19 +0200
Subject: [PATCH 2/2] update

---
 .../jpa/JpaOneToOneDemoApplication.java       | 35 ++++++++++++++++++-
 .../main/java/com/example/jpa/model/User.java |  1 +
 .../com/example/jpa/model/UserProfile.java    |  1 +
 .../src/main/resources/application.properties |  4 +--
 .../src/main/resources/data.sql               |  9 -----
 5 files changed, 38 insertions(+), 12 deletions(-)
 delete mode 100644 jpa-one-to-one-demo/src/main/resources/data.sql

diff --git a/jpa-one-to-one-demo/src/main/java/com/example/jpa/JpaOneToOneDemoApplication.java b/jpa-one-to-one-demo/src/main/java/com/example/jpa/JpaOneToOneDemoApplication.java
index 08d9c85..8db534e 100644
--- a/jpa-one-to-one-demo/src/main/java/com/example/jpa/JpaOneToOneDemoApplication.java
+++ b/jpa-one-to-one-demo/src/main/java/com/example/jpa/JpaOneToOneDemoApplication.java
@@ -1,10 +1,13 @@
 package com.example.jpa;
 
+import com.example.jpa.model.Gender;
 import com.example.jpa.model.User;
 import com.example.jpa.model.UserProfile;
 import com.example.jpa.repository.UserProfileRepository;
 import com.example.jpa.repository.UserRepository;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -12,11 +15,12 @@
 import org.springframework.web.bind.annotation.RestController;
 
 import java.util.Collection;
+import java.util.Date;
 import java.util.List;
 
 @SpringBootApplication
 @RestController
-public class JpaOneToOneDemoApplication {
+public class JpaOneToOneDemoApplication implements ApplicationRunner {
 
 
 	@Autowired
@@ -38,4 +42,33 @@ public Collection<User> getUsers(){
 	public Collection<UserProfile> getUserProfiles(){
 		return userProfileRepository.findAll();
 	}
+
+	@Override
+	public void run(ApplicationArguments args) throws Exception {
+
+		UserProfile userProfile1 = new UserProfile();
+		userProfile1.setAddress1("Address 1");
+		userProfile1.setAddress2("Address 2");
+		userProfile1.setCity("City 1");
+		userProfile1.setCountry("Country 1");
+		userProfile1.setDateOfBirth(new Date());
+		userProfile1.setGender(Gender.FEMALE);
+
+
+		User user1 = new User();
+		user1.setEmail("example@gmail.com");
+		user1.setFirstName("SomeFirstName");
+		user1.setLastName("SomeLastName");
+		user1.setPassword("SomePassword");
+		userRepository.save(user1);
+
+		userProfile1.setUser(user1);
+		userProfileRepository.save(userProfile1);
+
+		userProfileRepository.findAll().forEach(userProfile -> {
+			System.out.println(userProfile.getAddress1() + " " + userProfile.getAddress2());
+			System.out.println(userProfile.getUser().getFirstName() + " " + userProfile.getUser().getLastName());
+		});
+
+	}
 }
diff --git a/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/User.java b/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/User.java
index 73cfb11..0bee903 100644
--- a/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/User.java
+++ b/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/User.java
@@ -1,6 +1,7 @@
 package com.example.jpa.model;
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
+import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
diff --git a/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/UserProfile.java b/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/UserProfile.java
index 1074c11..46e718e 100644
--- a/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/UserProfile.java
+++ b/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/UserProfile.java
@@ -1,5 +1,6 @@
 package com.example.jpa.model;
 
+import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
diff --git a/jpa-one-to-one-demo/src/main/resources/application.properties b/jpa-one-to-one-demo/src/main/resources/application.properties
index 42c733d..2928022 100644
--- a/jpa-one-to-one-demo/src/main/resources/application.properties
+++ b/jpa-one-to-one-demo/src/main/resources/application.properties
@@ -1,6 +1,6 @@
 
-spring.jpa.hibernate.ddl-auto=update
-spring.datasource.initialization-mode=always
+#spring.jpa.hibernate.ddl-auto=update
+#spring.datasource.initialization-mode=always
 
 spring.h2.console.enabled=true
 spring.datasource.platform=h2
diff --git a/jpa-one-to-one-demo/src/main/resources/data.sql b/jpa-one-to-one-demo/src/main/resources/data.sql
deleted file mode 100644
index 6c8a733..0000000
--- a/jpa-one-to-one-demo/src/main/resources/data.sql
+++ /dev/null
@@ -1,9 +0,0 @@
-
-INSERT INTO USERS VALUES (1, 'email1@example.com', 'firstname1', 'lastname1', 'password1'),
-                         (2, 'email2@example.com', 'firstname1', 'lastname1', 'password1'),
-                         (3, 'email3@example.com', 'firstname1', 'lastname1', 'password1');
-
---ID  	ADDRESS1  	ADDRESS2  	CITY  	COUNTRY  	DOB  	GENDER  	PHONE_NUMBER  	STATE  	STREET  	ZIP_CODE  	USER_ID
-INSERT INTO USER_PROFILES VALUES (1, 'address 1', 'Address 1', 'City 1', 'Country 1', '1995-05-05', 'MALE', 'Phone Number 1', 'State 1', 'Street 1', '12545', 1),
-                                 (2, 'address 2', 'Address 2', 'City 2', 'Country 2', '1995-02-02', 'FEMALE', 'Phone Number 2', 'State 2', 'Street 2', '12545', 2),
-                                 (3, 'address 3', 'Address 3', 'City 3', 'Country 3', '1995-03-03', 'MALE', 'Phone Number 3', 'State 3', 'Street 3', '12545', 3);