From 197f7dde9e6acecd939df97255a7cf86e41b8d5a Mon Sep 17 00:00:00 2001 From: Amit Patil Date: Sat, 28 Sep 2019 13:05:41 +0530 Subject: [PATCH 1/3] updated the project with orphanRemoval support and avoided the infinite de-serialization loop of jackson using @JacksonManagedReference @JacksonBackReference --- ...one mapping examle.postman_collection.json | 108 ++++++++++++++++++ jpa-one-to-one-demo/pom.xml | 13 ++- .../amit/jpa/JpaOneToOneDemoApplication.java | 16 +++ .../app/configuration/AppConfiguration.java | 20 ++++ .../jpa/app/controllers/AppController.java | 42 +++++++ .../main/java/com/amit/jpa/model/Gender.java | 11 ++ .../com/{example => amit}/jpa/model/User.java | 21 +++- .../jpa/model/UserProfile.java | 19 ++- .../jpa/repository/UserProfileRepository.java | 11 +- .../jpa/repository/UserRepository.java | 11 +- .../java/com/amit/jpa/service/AppService.java | 32 ++++++ .../jpa/JpaOneToOneDemoApplication.java | 61 ---------- .../java/com/example/jpa/model/Gender.java | 9 -- .../src/main/resources/application.properties | 9 +- 14 files changed, 290 insertions(+), 93 deletions(-) create mode 100644 jpa-one-to-one-demo/Hibernate one to one mapping examle.postman_collection.json create mode 100644 jpa-one-to-one-demo/src/main/java/com/amit/jpa/JpaOneToOneDemoApplication.java create mode 100644 jpa-one-to-one-demo/src/main/java/com/amit/jpa/app/configuration/AppConfiguration.java create mode 100644 jpa-one-to-one-demo/src/main/java/com/amit/jpa/app/controllers/AppController.java create mode 100644 jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/Gender.java rename jpa-one-to-one-demo/src/main/java/com/{example => amit}/jpa/model/User.java (87%) rename jpa-one-to-one-demo/src/main/java/com/{example => amit}/jpa/model/UserProfile.java (93%) rename jpa-one-to-one-demo/src/main/java/com/{example => amit}/jpa/repository/UserProfileRepository.java (63%) rename jpa-one-to-one-demo/src/main/java/com/{example => amit}/jpa/repository/UserRepository.java (62%) create mode 100644 jpa-one-to-one-demo/src/main/java/com/amit/jpa/service/AppService.java delete mode 100644 jpa-one-to-one-demo/src/main/java/com/example/jpa/JpaOneToOneDemoApplication.java delete mode 100644 jpa-one-to-one-demo/src/main/java/com/example/jpa/model/Gender.java diff --git a/jpa-one-to-one-demo/Hibernate one to one mapping examle.postman_collection.json b/jpa-one-to-one-demo/Hibernate one to one mapping examle.postman_collection.json new file mode 100644 index 0000000..5c7e0f7 --- /dev/null +++ b/jpa-one-to-one-demo/Hibernate one to one mapping examle.postman_collection.json @@ -0,0 +1,108 @@ +{ + "id": "4df5465b-c7ea-7975-1123-2b3f2ed0f5c7", + "name": "Hibernate one to one mapping examle", + "description": "", + "order": [ + "b170f4ea-e3f0-fbaa-51fa-9a1e9777118d", + "24b1fa01-f92d-c4e1-c915-85be4819146b", + "d8cbed58-b687-7ee4-08f0-b33a4233b6b1" + ], + "folders": [], + "folders_order": [], + "timestamp": 1569653627690, + "owner": "2037757", + "public": false, + "requests": [ + { + "id": "24b1fa01-f92d-c4e1-c915-85be4819146b", + "headers": "Content-Type: application/json\n", + "headerData": [ + { + "key": "Content-Type", + "value": "application/json", + "description": "", + "enabled": true + } + ], + "url": "localhost:8080/v1/users", + "queryParams": [], + "pathVariables": {}, + "pathVariableData": [], + "preRequestScript": null, + "method": "GET", + "collectionId": "4df5465b-c7ea-7975-1123-2b3f2ed0f5c7", + "data": null, + "dataMode": "params", + "name": "Retrieve all user details", + "description": "", + "descriptionFormat": "html", + "time": 1569653756579, + "version": 2, + "responses": [], + "tests": null, + "currentHelper": "normal", + "helperAttributes": {} + }, + { + "id": "b170f4ea-e3f0-fbaa-51fa-9a1e9777118d", + "headers": "Content-Type: application/json\n", + "headerData": [ + { + "key": "Content-Type", + "value": "application/json", + "description": "", + "enabled": true + } + ], + "url": "localhost:8080/v1/user", + "queryParams": [], + "pathVariables": {}, + "pathVariableData": [], + "preRequestScript": null, + "method": "POST", + "collectionId": "4df5465b-c7ea-7975-1123-2b3f2ed0f5c7", + "data": [], + "dataMode": "raw", + "name": "Create user details", + "description": "", + "descriptionFormat": "html", + "time": 1569653662501, + "version": 2, + "responses": [], + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "rawModeData": "{\n\t\"id\": \"\",\n\t\"firstName\": \"Ranjit\",\n\t\"lastName\": \"Patil\",\n\t\"email\": \"Ranjit@gmail.com\",\n\t\"password\": \"pass123\",\n\t\"userProfile\": {\n\t\t\"id\": \"\",\n\t\t\"phoneNumber\": \"+91-7829190061\",\n\t\t\"gender\": \"MALE\",\n\t\t\"dateOfBirth\": \"2019-09-28\",\n\t\t\"address1\": \"some dummy address1 about my residence\",\n\t\t\"address2\": \"some dummy adress2 \",\n\t\t\"street\": \"some dummy street\",\n\t\t\"city\": \"Bangalore\",\n\t\t\"state\": \"Karnataka\",\n\t\t\"country\": \"India\",\n\t\t\"zipCode\": \"560100\"\n\t}\n}" + }, + { + "id": "d8cbed58-b687-7ee4-08f0-b33a4233b6b1", + "headers": "Content-Type: application/json\n", + "headerData": [ + { + "key": "Content-Type", + "value": "application/json", + "description": "", + "enabled": true + } + ], + "url": "localhost:8080/v1/user/1", + "queryParams": [], + "pathVariables": {}, + "pathVariableData": [], + "preRequestScript": null, + "method": "GET", + "collectionId": "4df5465b-c7ea-7975-1123-2b3f2ed0f5c7", + "data": null, + "dataMode": "params", + "name": "Retrieve single user details", + "description": "", + "descriptionFormat": "html", + "time": 1569655016616, + "version": 2, + "responses": [], + "tests": null, + "currentHelper": "normal", + "helperAttributes": {} + } + ] +} \ 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 212d569..d9f5874 100644 --- a/jpa-one-to-one-demo/pom.xml +++ b/jpa-one-to-one-demo/pom.xml @@ -1,5 +1,6 @@ - 4.0.0 @@ -15,7 +16,7 @@ org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE - + @@ -38,7 +39,15 @@ mysql mysql-connector-java runtime + 8.0.13 + + + org.hibernate + hibernate-core + 5.3.1.Final + + org.springframework.boot spring-boot-starter-test diff --git a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/JpaOneToOneDemoApplication.java b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/JpaOneToOneDemoApplication.java new file mode 100644 index 0000000..b780725 --- /dev/null +++ b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/JpaOneToOneDemoApplication.java @@ -0,0 +1,16 @@ +package com.amit.jpa; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +/** +* +* @author Amit Patil +* +**/ +@SpringBootApplication +public class JpaOneToOneDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(JpaOneToOneDemoApplication.class, args); + } +} diff --git a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/app/configuration/AppConfiguration.java b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/app/configuration/AppConfiguration.java new file mode 100644 index 0000000..6479094 --- /dev/null +++ b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/app/configuration/AppConfiguration.java @@ -0,0 +1,20 @@ +package com.amit.jpa.app.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.amit.jpa.service.AppService; + +/** +* +* @author Amit Patil +* +**/ +@Configuration +public class AppConfiguration { + + @Bean + public AppService appService() { + return new AppService(); + } +} diff --git a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/app/controllers/AppController.java b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/app/controllers/AppController.java new file mode 100644 index 0000000..ac11a80 --- /dev/null +++ b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/app/controllers/AppController.java @@ -0,0 +1,42 @@ +package com.amit.jpa.app.controllers; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import com.amit.jpa.model.User; +import com.amit.jpa.service.AppService; + +/** +* +* @author Amit Patil +* +**/ +@RestController +@RequestMapping("/v1") +public class AppController { + + @Autowired + private AppService appService; + + @RequestMapping(value = "/user", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public User saveUserDetails(@RequestBody User user) { + return appService.saveUserDetails(user); + } + + @RequestMapping(value = "/user/{userId}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public User getUserDetailsById(@PathVariable("userId") Long id) { + return appService.getUserDetailsById(id); + } + + @RequestMapping(value = "/users", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public List getAllUserDetails() { + return appService.getAllUserDetails(); + } +} diff --git a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/Gender.java b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/Gender.java new file mode 100644 index 0000000..aa847b9 --- /dev/null +++ b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/Gender.java @@ -0,0 +1,11 @@ +package com.amit.jpa.model; + +/** +* +* @author Amit Patil +* +**/ +public enum Gender { + MALE, + FEMALE +} 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/amit/jpa/model/User.java similarity index 87% rename from jpa-one-to-one-demo/src/main/java/com/example/jpa/model/User.java rename to jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/User.java index f41fc36..2a67681 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/amit/jpa/model/User.java @@ -1,18 +1,28 @@ -package com.example.jpa.model; +package com.amit.jpa.model; import javax.persistence.*; import javax.validation.constraints.Email; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; + +import com.fasterxml.jackson.annotation.JsonManagedReference; + import java.io.Serializable; /** - * Created by rajeevkumarsingh on 20/11/17. - */ +* +* @author Amit Patil +* +**/ @Entity @Table(name = "users") public class User implements Serializable { - @Id + /** + * + */ + private static final long serialVersionUID = 7529424835544174898L; + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -35,9 +45,10 @@ public class User implements Serializable { @Size(max = 128) private String password; + @JsonManagedReference @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, - mappedBy = "user") + mappedBy = "user", orphanRemoval = true) private UserProfile userProfile; // Hibernate requires a no-arg constructor 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/amit/jpa/model/UserProfile.java similarity index 93% rename from jpa-one-to-one-demo/src/main/java/com/example/jpa/model/UserProfile.java rename to jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/UserProfile.java index 099a759..3d45560 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/amit/jpa/model/UserProfile.java @@ -1,17 +1,27 @@ -package com.example.jpa.model; +package com.amit.jpa.model; import javax.persistence.*; import javax.validation.constraints.Size; + +import com.fasterxml.jackson.annotation.JsonBackReference; + import java.io.Serializable; import java.util.Date; /** - * Created by rajeevkumarsingh on 20/11/17. - */ +* +* @author Amit Patil +* +**/ @Entity @Table(name = "user_profiles") public class UserProfile implements Serializable { - @Id + /** + * + */ + private static final long serialVersionUID = 3960703238763006209L; + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -49,6 +59,7 @@ public class UserProfile implements Serializable { @Size(max = 32) private String zipCode; + @JsonBackReference @OneToOne(fetch = FetchType.LAZY, optional = false) @JoinColumn(name = "user_id", nullable = false) private User user; diff --git a/jpa-one-to-one-demo/src/main/java/com/example/jpa/repository/UserProfileRepository.java b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/repository/UserProfileRepository.java similarity index 63% rename from jpa-one-to-one-demo/src/main/java/com/example/jpa/repository/UserProfileRepository.java rename to jpa-one-to-one-demo/src/main/java/com/amit/jpa/repository/UserProfileRepository.java index 1051f8a..3df4b57 100644 --- a/jpa-one-to-one-demo/src/main/java/com/example/jpa/repository/UserProfileRepository.java +++ b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/repository/UserProfileRepository.java @@ -1,12 +1,15 @@ -package com.example.jpa.repository; +package com.amit.jpa.repository; -import com.example.jpa.model.UserProfile; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import com.amit.jpa.model.UserProfile; + /** - * Created by rajeevkumarsingh on 20/11/17. - */ +* +* @author Amit Patil +* +**/ @Repository public interface UserProfileRepository extends JpaRepository { diff --git a/jpa-one-to-one-demo/src/main/java/com/example/jpa/repository/UserRepository.java b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/repository/UserRepository.java similarity index 62% rename from jpa-one-to-one-demo/src/main/java/com/example/jpa/repository/UserRepository.java rename to jpa-one-to-one-demo/src/main/java/com/amit/jpa/repository/UserRepository.java index fba1c91..fe22066 100644 --- a/jpa-one-to-one-demo/src/main/java/com/example/jpa/repository/UserRepository.java +++ b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/repository/UserRepository.java @@ -1,12 +1,15 @@ -package com.example.jpa.repository; +package com.amit.jpa.repository; -import com.example.jpa.model.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import com.amit.jpa.model.User; + /** - * Created by rajeevkumarsingh on 20/11/17. - */ +* +* @author Amit Patil +* +**/ @Repository public interface UserRepository extends JpaRepository { diff --git a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/service/AppService.java b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/service/AppService.java new file mode 100644 index 0000000..14599d7 --- /dev/null +++ b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/service/AppService.java @@ -0,0 +1,32 @@ +package com.amit.jpa.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; + +import com.amit.jpa.model.User; +import com.amit.jpa.repository.UserRepository; + +/** +* +* @author Amit Patil +* +**/ +public class AppService { + + @Autowired + private UserRepository userRepository; + + public User saveUserDetails(User user) { + return userRepository.save(user); + } + + public User getUserDetailsById(Long id) { + return userRepository.findById(id).get(); + } + + public List getAllUserDetails() { + return userRepository.findAll(); + } +} + \ No newline at end of file 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 deleted file mode 100644 index 72de4c6..0000000 --- a/jpa-one-to-one-demo/src/main/java/com/example/jpa/JpaOneToOneDemoApplication.java +++ /dev/null @@ -1,61 +0,0 @@ -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 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; - -@SpringBootApplication -public class JpaOneToOneDemoApplication implements CommandLineRunner { - - @Autowired - private UserRepository userRepository; - - @Autowired - 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); - - //========================================= - } - - -} diff --git a/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/Gender.java b/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/Gender.java deleted file mode 100644 index e77317f..0000000 --- a/jpa-one-to-one-demo/src/main/java/com/example/jpa/model/Gender.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.example.jpa.model; - -/** - * Created by rajeevkumarsingh on 29/11/17. - */ -public enum Gender { - MALE, - FEMALE -} 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 549e2fb..194f1a2 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,16 @@ # DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) spring.datasource.url=jdbc:mysql://localhost:3306/jpa_one_to_one_demo?useSSL=false spring.datasource.username=root -spring.datasource.password=callicoder - +spring.datasource.password=root +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # Hibernate # The SQL dialect makes Hibernate generate better SQL for the chosen database -spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect +spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect # 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 +logging.level.org.hibernate.type=TRACE +#spring.jackson.serialization.fail-on-empty-beans=false \ No newline at end of file From 175c61e12a7bc32c0dd7f7c599cb7365231ecc27 Mon Sep 17 00:00:00 2001 From: Amit Patil Date: Sat, 28 Sep 2019 14:04:51 +0530 Subject: [PATCH 2/3] added new handlers in appController to hanlde creation/retrieval of userProfile --- ...one mapping examle.postman_collection.json | 128 +++++++++++++++++- .../jpa/app/controllers/AppController.java | 16 +++ .../java/com/amit/jpa/model/UserProfile.java | 4 +- .../java/com/amit/jpa/service/AppService.java | 17 +++ .../src/main/resources/application.properties | 7 +- 5 files changed, 165 insertions(+), 7 deletions(-) diff --git a/jpa-one-to-one-demo/Hibernate one to one mapping examle.postman_collection.json b/jpa-one-to-one-demo/Hibernate one to one mapping examle.postman_collection.json index 5c7e0f7..b91605d 100644 --- a/jpa-one-to-one-demo/Hibernate one to one mapping examle.postman_collection.json +++ b/jpa-one-to-one-demo/Hibernate one to one mapping examle.postman_collection.json @@ -5,7 +5,11 @@ "order": [ "b170f4ea-e3f0-fbaa-51fa-9a1e9777118d", "24b1fa01-f92d-c4e1-c915-85be4819146b", - "d8cbed58-b687-7ee4-08f0-b33a4233b6b1" + "d8cbed58-b687-7ee4-08f0-b33a4233b6b1", + "76f5bc70-ab5e-512d-a4ad-de16007ebb6e", + "e0f51fb0-33ae-b787-7208-220ab953b0f0", + "17a7b55e-f15e-635e-5a90-b948b263e248", + "c246e38e-fa61-e509-2e60-9524566127b4" ], "folders": [], "folders_order": [], @@ -13,6 +17,36 @@ "owner": "2037757", "public": false, "requests": [ + { + "id": "17a7b55e-f15e-635e-5a90-b948b263e248", + "headers": "Content-Type: application/json\n", + "headerData": [ + { + "key": "Content-Type", + "value": "application/json", + "description": "", + "enabled": true + } + ], + "url": "localhost:8080/v1/userProfile/1", + "queryParams": [], + "pathVariables": {}, + "pathVariableData": [], + "preRequestScript": null, + "method": "GET", + "collectionId": "4df5465b-c7ea-7975-1123-2b3f2ed0f5c7", + "data": null, + "dataMode": "params", + "name": "Retrieve single userProfile details", + "description": "", + "descriptionFormat": "html", + "time": 1569658918810, + "version": 2, + "responses": [], + "tests": null, + "currentHelper": "normal", + "helperAttributes": {} + }, { "id": "24b1fa01-f92d-c4e1-c915-85be4819146b", "headers": "Content-Type: application/json\n", @@ -43,6 +77,37 @@ "currentHelper": "normal", "helperAttributes": {} }, + { + "id": "76f5bc70-ab5e-512d-a4ad-de16007ebb6e", + "headers": "Content-Type: application/json\n", + "headerData": [ + { + "key": "Content-Type", + "value": "application/json", + "description": "", + "enabled": true + } + ], + "url": "localhost:8080/v1/userProfile", + "queryParams": [], + "pathVariables": {}, + "pathVariableData": [], + "preRequestScript": null, + "method": "POST", + "collectionId": "4df5465b-c7ea-7975-1123-2b3f2ed0f5c7", + "data": [], + "dataMode": "raw", + "name": "save new userProfile without actual user association", + "description": "user set to null reference", + "descriptionFormat": "html", + "time": 1569658739461, + "version": 2, + "responses": [], + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "rawModeData": "{\n\t\t\"phoneNumber\": \"+91-7829190061\",\n\t\t\"gender\": \"MALE\",\n\t\t\"dateOfBirth\": \"2019-09-28\",\n\t\t\"address1\": \"address1 without user association\",\n\t\t\"address2\": \"some dummy adress2 \",\n\t\t\"street\": \"some dummy street\",\n\t\t\"city\": \"Bangalore\",\n\t\t\"state\": \"Karnataka\",\n\t\t\"country\": \"India\",\n\t\t\"zipCode\": \"560100\",\n\t\t\"user\": null\n}" + }, { "id": "b170f4ea-e3f0-fbaa-51fa-9a1e9777118d", "headers": "Content-Type: application/json\n", @@ -74,6 +139,36 @@ "helperAttributes": {}, "rawModeData": "{\n\t\"id\": \"\",\n\t\"firstName\": \"Ranjit\",\n\t\"lastName\": \"Patil\",\n\t\"email\": \"Ranjit@gmail.com\",\n\t\"password\": \"pass123\",\n\t\"userProfile\": {\n\t\t\"id\": \"\",\n\t\t\"phoneNumber\": \"+91-7829190061\",\n\t\t\"gender\": \"MALE\",\n\t\t\"dateOfBirth\": \"2019-09-28\",\n\t\t\"address1\": \"some dummy address1 about my residence\",\n\t\t\"address2\": \"some dummy adress2 \",\n\t\t\"street\": \"some dummy street\",\n\t\t\"city\": \"Bangalore\",\n\t\t\"state\": \"Karnataka\",\n\t\t\"country\": \"India\",\n\t\t\"zipCode\": \"560100\"\n\t}\n}" }, + { + "id": "c246e38e-fa61-e509-2e60-9524566127b4", + "headers": "Content-Type: application/json\n", + "headerData": [ + { + "key": "Content-Type", + "value": "application/json", + "description": "", + "enabled": true + } + ], + "url": "localhost:8080/v1/userProfiles", + "queryParams": [], + "pathVariables": {}, + "pathVariableData": [], + "preRequestScript": null, + "method": "GET", + "collectionId": "4df5465b-c7ea-7975-1123-2b3f2ed0f5c7", + "data": null, + "dataMode": "params", + "name": "Retrieve All the userProfiles details", + "description": "", + "descriptionFormat": "html", + "time": 1569658935643, + "version": 2, + "responses": [], + "tests": null, + "currentHelper": "normal", + "helperAttributes": {} + }, { "id": "d8cbed58-b687-7ee4-08f0-b33a4233b6b1", "headers": "Content-Type: application/json\n", @@ -103,6 +198,37 @@ "tests": null, "currentHelper": "normal", "helperAttributes": {} + }, + { + "id": "e0f51fb0-33ae-b787-7208-220ab953b0f0", + "headers": "Content-Type: application/json\n", + "headerData": [ + { + "key": "Content-Type", + "value": "application/json", + "description": "", + "enabled": true + } + ], + "url": "localhost:8080/v1/userProfile", + "queryParams": [], + "pathVariables": {}, + "pathVariableData": [], + "preRequestScript": null, + "method": "POST", + "collectionId": "4df5465b-c7ea-7975-1123-2b3f2ed0f5c7", + "data": [], + "dataMode": "raw", + "name": "save new userProfile with invalid user association", + "description": "user details set to empty object", + "descriptionFormat": "html", + "time": 1569658813570, + "version": 2, + "responses": [], + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "rawModeData": "{\n\t\t\"phoneNumber\": \"+91-7829190061\",\n\t\t\"gender\": \"MALE\",\n\t\t\"dateOfBirth\": \"2019-09-28\",\n\t\t\"address1\": \"address1 without user association\",\n\t\t\"address2\": \"some dummy adress2 \",\n\t\t\"street\": \"some dummy street\",\n\t\t\"city\": \"Bangalore\",\n\t\t\"state\": \"Karnataka\",\n\t\t\"country\": \"India\",\n\t\t\"zipCode\": \"560100\",\n\t\t\"user\": {}\n}" } ] } \ No newline at end of file diff --git a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/app/controllers/AppController.java b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/app/controllers/AppController.java index ac11a80..297fbb9 100644 --- a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/app/controllers/AppController.java +++ b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/app/controllers/AppController.java @@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RestController; import com.amit.jpa.model.User; +import com.amit.jpa.model.UserProfile; import com.amit.jpa.service.AppService; /** @@ -39,4 +40,19 @@ public User getUserDetailsById(@PathVariable("userId") Long id) { public List getAllUserDetails() { return appService.getAllUserDetails(); } + + @RequestMapping(value = "/userProfile", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public UserProfile saveUserProfileDetails(@RequestBody UserProfile userProfile) { + return appService.saveUserProfile(userProfile); + } + + @RequestMapping(value = "/userProfile/{userProfileId}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public UserProfile getUserProfileDetails(@PathVariable("userProfileId") Long id) { + return appService.getUserProfileDetails(id); + } + + @RequestMapping(value = "/userProfiles", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public List getAllUserProfiles() { + return appService.getAllUserProfileDetails(); + } } diff --git a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/UserProfile.java b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/UserProfile.java index 3d45560..257ed8f 100644 --- a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/UserProfile.java +++ b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/UserProfile.java @@ -60,8 +60,8 @@ public class UserProfile implements Serializable { private String zipCode; @JsonBackReference - @OneToOne(fetch = FetchType.LAZY, optional = false) - @JoinColumn(name = "user_id", nullable = false) + @OneToOne(fetch = FetchType.LAZY, optional = true) + @JoinColumn(name = "user_id", nullable = true) private User user; public UserProfile() { diff --git a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/service/AppService.java b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/service/AppService.java index 14599d7..a302f37 100644 --- a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/service/AppService.java +++ b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/service/AppService.java @@ -5,6 +5,8 @@ import org.springframework.beans.factory.annotation.Autowired; import com.amit.jpa.model.User; +import com.amit.jpa.model.UserProfile; +import com.amit.jpa.repository.UserProfileRepository; import com.amit.jpa.repository.UserRepository; /** @@ -17,6 +19,9 @@ public class AppService { @Autowired private UserRepository userRepository; + @Autowired + private UserProfileRepository userProfileRepository; + public User saveUserDetails(User user) { return userRepository.save(user); } @@ -28,5 +33,17 @@ public User getUserDetailsById(Long id) { public List getAllUserDetails() { return userRepository.findAll(); } + + public UserProfile saveUserProfile(UserProfile userProfile) { + return userProfileRepository.save(userProfile); + } + + public UserProfile getUserProfileDetails(Long id) { + return userProfileRepository.findById(id).get(); + } + + public List getAllUserProfileDetails() { + return userProfileRepository.findAll(); + } } \ No newline at end of file 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 194f1a2..895c5fa 100644 --- a/jpa-one-to-one-demo/src/main/resources/application.properties +++ b/jpa-one-to-one-demo/src/main/resources/application.properties @@ -9,8 +9,7 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect # Hibernate ddl auto (create, create-drop, validate, update) -spring.jpa.hibernate.ddl-auto = update +spring.jpa.hibernate.ddl-auto = validate -logging.level.org.hibernate.SQL=DEBUG -logging.level.org.hibernate.type=TRACE -#spring.jackson.serialization.fail-on-empty-beans=false \ No newline at end of file +#logging.level.org.hibernate.SQL=DEBUG +#logging.level.org.hibernate.type=TRACE \ No newline at end of file From c1d1eec4b7c158ae1274e7a48fd3d19d509c105c Mon Sep 17 00:00:00 2001 From: Amit Patil Date: Sat, 28 Sep 2019 14:10:59 +0530 Subject: [PATCH 3/3] reverted the unwanted changes from previous commit --- .../src/main/java/com/amit/jpa/model/UserProfile.java | 4 ++-- jpa-one-to-one-demo/src/main/resources/application.properties | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/UserProfile.java b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/UserProfile.java index 257ed8f..3d45560 100644 --- a/jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/UserProfile.java +++ b/jpa-one-to-one-demo/src/main/java/com/amit/jpa/model/UserProfile.java @@ -60,8 +60,8 @@ public class UserProfile implements Serializable { private String zipCode; @JsonBackReference - @OneToOne(fetch = FetchType.LAZY, optional = true) - @JoinColumn(name = "user_id", nullable = true) + @OneToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "user_id", nullable = false) private User user; public UserProfile() { 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 895c5fa..93e3d28 100644 --- a/jpa-one-to-one-demo/src/main/resources/application.properties +++ b/jpa-one-to-one-demo/src/main/resources/application.properties @@ -9,7 +9,7 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect # Hibernate ddl auto (create, create-drop, validate, update) -spring.jpa.hibernate.ddl-auto = validate +spring.jpa.hibernate.ddl-auto = update #logging.level.org.hibernate.SQL=DEBUG #logging.level.org.hibernate.type=TRACE \ No newline at end of file