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 @@
4.0.0
com.example
- hibernate-one-to-one-demo
+ jpa-one-to-one-demo
0.0.1-SNAPSHOT
- jar
-
hibernate-one-to-one-demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
- 2.2.1.RELEASE
+ 2.3.4.RELEASE
@@ -33,10 +31,14 @@
org.springframework.boot
spring-boot-starter-web
-
- mysql
- mysql-connector-java
+ org.projectlombok
+ lombok
+ true
+
+
+ com.h2database
+ h2
runtime
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..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
@@ -3,59 +3,72 @@
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.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
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.Date;
+import java.util.List;
@SpringBootApplication
-public class JpaOneToOneDemoApplication implements CommandLineRunner {
+@RestController
+public class JpaOneToOneDemoApplication implements ApplicationRunner {
+
@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();
-
- //=========================================
+ @GetMapping("/users")
+ public Collection getUsers(){
+ return userRepository.findAll();
+ }
- // Create a User instance
- User user = new User("Rajeev", "Singh", "rajeev@callicoder.com",
- "MY_SUPER_SECRET_PASSWORD");
+ @GetMapping("/user_profiles")
+ public Collection getUserProfiles(){
+ return userProfileRepository.findAll();
+ }
- Calendar dateOfBirth = Calendar.getInstance();
- dateOfBirth.set(1992, 7, 21);
+ @Override
+ public void run(ApplicationArguments args) throws Exception {
- // 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");
+ 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);
- // Set child reference(userProfile) in parent entity(user)
- user.setUserProfile(userProfile);
+ User user1 = new User();
+ user1.setEmail("example@gmail.com");
+ user1.setFirstName("SomeFirstName");
+ user1.setLastName("SomeLastName");
+ user1.setPassword("SomePassword");
+ userRepository.save(user1);
- // Set parent reference(user) in child entity(userProfile)
- userProfile.setUser(user);
+ userProfile1.setUser(user1);
+ userProfileRepository.save(userProfile1);
- // Save Parent Reference (which will save the child as well)
- userRepository.save(user);
+ 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 f41fc36..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,9 +1,11 @@
package com.example.jpa.model;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import lombok.Builder;
+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 +13,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 +36,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..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,7 +1,10 @@
package com.example.jpa.model;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
import javax.persistence.*;
-import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
@@ -10,53 +13,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 +63,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..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,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