From 4f7b0d6a645625d115293133f48eb993a6d7b733 Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Sun, 10 May 2020 18:40:27 +0200 Subject: [PATCH 1/7] First commit --- persistence-modules/spring-data-jpa-5/pom.xml | 58 +++++++++++++++++++ .../main/java/com/baeldung/Application.java | 58 +++++++++++++++++++ .../java/com/baeldung/model/Customer.java | 27 +++++++++ .../java/com/baeldung/model/CustomerDto.java | 34 +++++++++++ .../repository/CustomerRepository.java | 13 +++++ .../com/baeldung/service/CustomerService.java | 50 ++++++++++++++++ .../com/baeldung/util/CustomerMapper.java | 15 +++++ 7 files changed, 255 insertions(+) create mode 100644 persistence-modules/spring-data-jpa-5/pom.xml create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java diff --git a/persistence-modules/spring-data-jpa-5/pom.xml b/persistence-modules/spring-data-jpa-5/pom.xml new file mode 100644 index 0000000000..df1cc6c0c4 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + spring-data-jpa-5 + spring-data-jpa-5 + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + + + org.mapstruct + mapstruct-jdk8 + 1.3.1.Final + provided + + + org.springframework.boot + spring-boot-starter-cache + + + + + src/main/java + + + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + org.mapstruct + mapstruct-processor + 1.3.1.Final + + + + + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..64d0a5e6c6 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java @@ -0,0 +1,58 @@ +package com.baeldung; + +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; + +import com.baeldung.model.Customer; +import com.baeldung.model.CustomerDto; +import com.baeldung.service.CustomerService; + +@SpringBootApplication @EnableCaching +public class Application { + + @Autowired CustomerService service; + @Autowired CacheManager cacheManager; + private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Application.class); + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) throws Exception { + logger.info("-- BASIC LOAD AND SAVE --"); + basicLoadAndSave(); + logger.info("-- BASIC LOAD AND SAVE + MAPPER --"); + basicLoadAndSaveWithMapper(); + return null; + } + + private void basicLoadAndSave() { + Customer myCustomer = service.addCustomer("John", "Doe"); + logger.info("Insert -- " + myCustomer.toString()); + myCustomer = service.updateCustomer(myCustomer.id, "john@doe.com", "+00", "Route 66"); + logger.info("Update -- " + myCustomer.toString()); + } + + private void basicLoadAndSaveWithMapper() { + CustomerDto dto = new CustomerDto(); + dto.firstName = "Johnny"; + dto.lastName = "Doe"; + Customer entity = service.addCustomer(dto); + logger.info("Insert -- " + entity.toString()); + CustomerDto dto2 = new CustomerDto(); + dto2.id = entity.id; + dto2.address = "Mountain View"; + dto2.email = "doe@mail.com"; + dto2.phone = "+44"; + entity = service.updateCustomer(dto2); + logger.info("Update -- " + entity.toString()); + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java new file mode 100644 index 0000000000..91808e7971 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java @@ -0,0 +1,27 @@ +package com.baeldung.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Customer { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + public long id; + public String firstName; + public String lastName; + public String address; + public String email; + public String phone; + //... + public String phone99; + + public Customer() {} + + @Override public String toString() { + return String.format("Customer %s %s, Address: %s, Email: %s, Phone: %s", + this.firstName, this.lastName, this.address, this.email, this.phone); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java new file mode 100644 index 0000000000..8f7803fc32 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java @@ -0,0 +1,34 @@ +package com.baeldung.model; + +public class CustomerDto { + public long id; + public String firstName; + public String lastName; + public String address; + public String email; + public String phone; + //... + public String phone99; + + public CustomerDto() {} + + public CustomerDto(Customer c) { + this.id = c.id; + this.firstName = c.firstName; + this.lastName = c.lastName; + this.address = c.address; + this.email = c.email; + this.phone = c.phone; + } + + public Customer convertToEntity() { + Customer c = new Customer(); + c.id = id; + c.firstName = firstName; + c.lastName = lastName; + c.address = address; + c.email = email; + c.phone = phone; + return c; + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java new file mode 100644 index 0000000000..dcde6c3b46 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.repository; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.model.Customer; + +@Repository +public interface CustomerRepository extends CrudRepository { + @Cacheable("customers") + Customer findById(long id); +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java new file mode 100644 index 0000000000..56afc9e80d --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java @@ -0,0 +1,50 @@ +package com.baeldung.service; + +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.model.Customer; +import com.baeldung.model.CustomerDto; +import com.baeldung.repository.CustomerRepository; +import com.baeldung.util.CustomerMapper; + +@Service @Transactional +public class CustomerService { + + @Autowired CustomerRepository repo; + @Autowired CustomerMapper mapper; + + public Customer addCustomer(String firstName, String lastName) { + Customer myCustomer = new Customer(); + myCustomer.firstName = firstName; + myCustomer.lastName = lastName; + repo.save(myCustomer); + return myCustomer; + } + + public Customer updateCustomer(long id, String email, String phone, String address) { + Customer myCustomer = repo.findById(id); + myCustomer.address = address; + myCustomer.email = email; + myCustomer.phone = phone; + repo.save(myCustomer); + return myCustomer; + } + + public Customer addCustomer(CustomerDto dto) { + Customer myCustomer = new Customer(); + mapper.updateCustomerFromDto(dto, myCustomer); + repo.save(myCustomer); + return myCustomer; + } + + public Customer updateCustomer(CustomerDto dto) { + Customer myCustomer = repo.findById(dto.id); + mapper.updateCustomerFromDto(dto, myCustomer); + repo.save(myCustomer); + return myCustomer; + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java new file mode 100644 index 0000000000..d4f264e33a --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java @@ -0,0 +1,15 @@ +package com.baeldung.util; + +import org.mapstruct.BeanMapping; +import org.mapstruct.Mapper; +import org.mapstruct.MappingTarget; +import org.mapstruct.NullValuePropertyMappingStrategy; + +import com.baeldung.model.Customer; +import com.baeldung.model.CustomerDto; + +@Mapper(componentModel = "spring") +public interface CustomerMapper { + @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE) + void updateCustomerFromDto(CustomerDto dto, @MappingTarget Customer entity); +} From 9ea4e4f04ef90d3267f0c2ab7dd6f8431f6a03e1 Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Sat, 16 May 2020 17:56:00 +0200 Subject: [PATCH 2/7] Simplified + refactored --- .../main/java/com/baeldung/Application.java | 14 ++++------- .../java/com/baeldung/model/Customer.java | 9 +++----- .../java/com/baeldung/model/CustomerDto.java | 23 +++++++------------ .../com/baeldung/service/CustomerService.java | 9 +++----- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java index 64d0a5e6c6..34e86fe135 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java @@ -34,22 +34,18 @@ public class Application { } private void basicLoadAndSave() { - Customer myCustomer = service.addCustomer("John", "Doe"); + Customer myCustomer = service.addCustomer("John"); logger.info("Insert -- " + myCustomer.toString()); - myCustomer = service.updateCustomer(myCustomer.id, "john@doe.com", "+00", "Route 66"); + myCustomer = service.updateCustomer(myCustomer.id, "+00"); logger.info("Update -- " + myCustomer.toString()); } private void basicLoadAndSaveWithMapper() { - CustomerDto dto = new CustomerDto(); - dto.firstName = "Johnny"; - dto.lastName = "Doe"; + CustomerDto dto = new CustomerDto(null); + dto.name = "Johnny"; Customer entity = service.addCustomer(dto); logger.info("Insert -- " + entity.toString()); - CustomerDto dto2 = new CustomerDto(); - dto2.id = entity.id; - dto2.address = "Mountain View"; - dto2.email = "doe@mail.com"; + CustomerDto dto2 = new CustomerDto(entity.id); dto2.phone = "+44"; entity = service.updateCustomer(dto2); logger.info("Update -- " + entity.toString()); diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java index 91808e7971..28c5d5c76c 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java @@ -10,10 +10,7 @@ public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public long id; - public String firstName; - public String lastName; - public String address; - public String email; + public String name; public String phone; //... public String phone99; @@ -21,7 +18,7 @@ public class Customer { public Customer() {} @Override public String toString() { - return String.format("Customer %s %s, Address: %s, Email: %s, Phone: %s", - this.firstName, this.lastName, this.address, this.email, this.phone); + return String.format("Customer %s, Phone: %s", + this.name, this.phone); } } diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java index 8f7803fc32..c4601d5b81 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java @@ -1,33 +1,26 @@ package com.baeldung.model; public class CustomerDto { - public long id; - public String firstName; - public String lastName; - public String address; - public String email; + private long id; + public String name; public String phone; //... - public String phone99; + private String phone99; - public CustomerDto() {} + public CustomerDto(long id) { + this.id = id; + } public CustomerDto(Customer c) { this.id = c.id; - this.firstName = c.firstName; - this.lastName = c.lastName; - this.address = c.address; - this.email = c.email; + this.name = c.name; this.phone = c.phone; } public Customer convertToEntity() { Customer c = new Customer(); c.id = id; - c.firstName = firstName; - c.lastName = lastName; - c.address = address; - c.email = email; + c.name = name; c.phone = phone; return c; } diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java index 56afc9e80d..3101cd8ece 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java @@ -16,18 +16,15 @@ public class CustomerService { @Autowired CustomerRepository repo; @Autowired CustomerMapper mapper; - public Customer addCustomer(String firstName, String lastName) { + public Customer addCustomer(String name) { Customer myCustomer = new Customer(); - myCustomer.firstName = firstName; - myCustomer.lastName = lastName; + myCustomer.name = name; repo.save(myCustomer); return myCustomer; } - public Customer updateCustomer(long id, String email, String phone, String address) { + public Customer updateCustomer(long id, String phone) { Customer myCustomer = repo.findById(id); - myCustomer.address = address; - myCustomer.email = email; myCustomer.phone = phone; repo.save(myCustomer); return myCustomer; From cd51e118df520c67f9e2c58b13c3c1bfca494d82 Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Mon, 18 May 2020 19:15:18 +0200 Subject: [PATCH 3/7] Fixed compilation error --- .../src/main/java/com/baeldung/model/CustomerDto.java | 4 ++++ .../src/main/java/com/baeldung/service/CustomerService.java | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java index c4601d5b81..74b909aad8 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java @@ -17,6 +17,10 @@ public class CustomerDto { this.phone = c.phone; } + public long getId() { + return this.id; + } + public Customer convertToEntity() { Customer c = new Customer(); c.id = id; diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java index 3101cd8ece..9ebbb0c814 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java @@ -38,7 +38,7 @@ public class CustomerService { } public Customer updateCustomer(CustomerDto dto) { - Customer myCustomer = repo.findById(dto.id); + Customer myCustomer = repo.findById(dto.getId()); mapper.updateCustomerFromDto(dto, myCustomer); repo.save(myCustomer); return myCustomer; From 715147e7dc2ef0f13cf8b9845b2de049bc66073f Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Sun, 24 May 2020 11:44:26 +0200 Subject: [PATCH 4/7] Refactored packages, added test cases, added structured class case --- .../main/java/com/baeldung/Application.java | 54 ------------- .../PartialUpdateApplication.java | 20 +++++ .../partialupdate/model/ContactPhone.java | 22 ++++++ .../{ => partialupdate}/model/Customer.java | 0 .../model/CustomerDto.java | 0 .../model/CustomerStructured.java | 26 ++++++ .../repository/ContactPhoneRepository.java | 12 +++ .../repository/CustomerRepository.java | 4 +- .../CustomerStructuredRepository.java | 11 +++ .../service/CustomerService.java | 79 +++++++++++++++++++ .../util/CustomerMapper.java | 6 +- .../com/baeldung/service/CustomerService.java | 47 ----------- .../partialupdate/PartialUpdateUnitTest.java | 56 +++++++++++++ 13 files changed, 231 insertions(+), 106 deletions(-) delete mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java rename persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/{ => partialupdate}/model/Customer.java (100%) rename persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/{ => partialupdate}/model/CustomerDto.java (100%) create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java rename persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/{ => partialupdate}/repository/CustomerRepository.java (76%) create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerStructuredRepository.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java rename persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/{ => partialupdate}/util/CustomerMapper.java (73%) delete mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java create mode 100644 persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java deleted file mode 100644 index 34e86fe135..0000000000 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.baeldung; - -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cache.CacheManager; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; - -import com.baeldung.model.Customer; -import com.baeldung.model.CustomerDto; -import com.baeldung.service.CustomerService; - -@SpringBootApplication @EnableCaching -public class Application { - - @Autowired CustomerService service; - @Autowired CacheManager cacheManager; - private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Application.class); - - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } - - @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) throws Exception { - logger.info("-- BASIC LOAD AND SAVE --"); - basicLoadAndSave(); - logger.info("-- BASIC LOAD AND SAVE + MAPPER --"); - basicLoadAndSaveWithMapper(); - return null; - } - - private void basicLoadAndSave() { - Customer myCustomer = service.addCustomer("John"); - logger.info("Insert -- " + myCustomer.toString()); - myCustomer = service.updateCustomer(myCustomer.id, "+00"); - logger.info("Update -- " + myCustomer.toString()); - } - - private void basicLoadAndSaveWithMapper() { - CustomerDto dto = new CustomerDto(null); - dto.name = "Johnny"; - Customer entity = service.addCustomer(dto); - logger.info("Insert -- " + entity.toString()); - CustomerDto dto2 = new CustomerDto(entity.id); - dto2.phone = "+44"; - entity = service.updateCustomer(dto2); - logger.info("Update -- " + entity.toString()); - } - -} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java new file mode 100644 index 0000000000..30f5dfa03d --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java @@ -0,0 +1,20 @@ +package com.baeldung.partialupdate; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; + +@SpringBootApplication +@EnableCaching +public class PartialUpdateApplication { + + @Autowired + CacheManager cacheManager; + + public static void main(String[] args) { + SpringApplication.run(PartialUpdateApplication.class, args); + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java new file mode 100644 index 0000000000..9d611fa755 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java @@ -0,0 +1,22 @@ +package com.baeldung.partialupdate.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class ContactPhone { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public long id; + @Column(nullable=false) + public long customerId; + public String phone; + + @Override + public String toString() { + return phone; + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java similarity index 100% rename from persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java rename to persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java similarity index 100% rename from persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java rename to persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java new file mode 100644 index 0000000000..67b534add2 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java @@ -0,0 +1,26 @@ +package com.baeldung.partialupdate.model; + +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +public class CustomerStructured { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public long id; + public String name; + @OneToMany(fetch = FetchType.EAGER, targetEntity=ContactPhone.class, mappedBy="customerId") + public List contactPhones; + + @Override public String toString() { + return String.format("Customer %s, Phone: %s", + this.name, this.contactPhones.stream().map(e -> e.toString()).reduce("", String::concat)); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java new file mode 100644 index 0000000000..2e0b687227 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java @@ -0,0 +1,12 @@ +package com.baeldung.partialupdate.repository; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.partialupdate.model.ContactPhone; + +@Repository +public interface ContactPhoneRepository extends CrudRepository { + ContactPhone findById(long id); + ContactPhone findByCustomerId(long id); +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java similarity index 76% rename from persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java rename to persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java index dcde6c3b46..bd33c03b6b 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java @@ -1,10 +1,10 @@ -package com.baeldung.repository; +package com.baeldung.partialupdate.repository; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; -import com.baeldung.model.Customer; +import com.baeldung.partialupdate.model.Customer; @Repository public interface CustomerRepository extends CrudRepository { diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerStructuredRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerStructuredRepository.java new file mode 100644 index 0000000000..0f9fd1e92e --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerStructuredRepository.java @@ -0,0 +1,11 @@ +package com.baeldung.partialupdate.repository; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.partialupdate.model.CustomerStructured; + +@Repository +public interface CustomerStructuredRepository extends CrudRepository { + CustomerStructured findById(long id); +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java new file mode 100644 index 0000000000..f3a1c65ef1 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java @@ -0,0 +1,79 @@ +package com.baeldung.partialupdate.service; + +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.partialupdate.model.ContactPhone; +import com.baeldung.partialupdate.model.Customer; +import com.baeldung.partialupdate.model.CustomerDto; +import com.baeldung.partialupdate.model.CustomerStructured; +import com.baeldung.partialupdate.repository.ContactPhoneRepository; +import com.baeldung.partialupdate.repository.CustomerRepository; +import com.baeldung.partialupdate.repository.CustomerStructuredRepository; +import com.baeldung.partialupdate.util.CustomerMapper; + +@Service +@Transactional +public class CustomerService { + + @Autowired + CustomerRepository repo; + @Autowired + CustomerStructuredRepository repo2; + @Autowired + ContactPhoneRepository repo3; + @Autowired + CustomerMapper mapper; + + public Customer addCustomer(String name) { + Customer myCustomer = new Customer(); + myCustomer.name = name; + repo.save(myCustomer); + return myCustomer; + } + + public Customer updateCustomer(long id, String phone) { + Customer myCustomer = repo.findById(id); + myCustomer.phone = phone; + repo.save(myCustomer); + return myCustomer; + } + + public Customer addCustomer(CustomerDto dto) { + Customer myCustomer = new Customer(); + mapper.updateCustomerFromDto(dto, myCustomer); + repo.save(myCustomer); + return myCustomer; + } + + public Customer updateCustomer(CustomerDto dto) { + Customer myCustomer = repo.findById(dto.getId()); + mapper.updateCustomerFromDto(dto, myCustomer); + repo.save(myCustomer); + return myCustomer; + } + + public CustomerStructured addCustomerStructured(String name) { + CustomerStructured myCustomer = new CustomerStructured(); + myCustomer.name = name; + repo2.save(myCustomer); + return myCustomer; + } + + public void addCustomerPhone(long customerId, String phone) { + ContactPhone myPhone = new ContactPhone(); + myPhone.phone = phone; + myPhone.customerId = customerId; + repo3.save(myPhone); + } + + public CustomerStructured updateCustomerStructured(long id, String name) { + CustomerStructured myCustomer = repo2.findById(id); + myCustomer.name = name; + repo2.save(myCustomer); + return myCustomer; + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/util/CustomerMapper.java similarity index 73% rename from persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java rename to persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/util/CustomerMapper.java index d4f264e33a..8a666e3e6c 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/util/CustomerMapper.java @@ -1,12 +1,12 @@ -package com.baeldung.util; +package com.baeldung.partialupdate.util; import org.mapstruct.BeanMapping; import org.mapstruct.Mapper; import org.mapstruct.MappingTarget; import org.mapstruct.NullValuePropertyMappingStrategy; -import com.baeldung.model.Customer; -import com.baeldung.model.CustomerDto; +import com.baeldung.partialupdate.model.Customer; +import com.baeldung.partialupdate.model.CustomerDto; @Mapper(componentModel = "spring") public interface CustomerMapper { diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java deleted file mode 100644 index 9ebbb0c814..0000000000 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.baeldung.service; - -import javax.transaction.Transactional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.baeldung.model.Customer; -import com.baeldung.model.CustomerDto; -import com.baeldung.repository.CustomerRepository; -import com.baeldung.util.CustomerMapper; - -@Service @Transactional -public class CustomerService { - - @Autowired CustomerRepository repo; - @Autowired CustomerMapper mapper; - - public Customer addCustomer(String name) { - Customer myCustomer = new Customer(); - myCustomer.name = name; - repo.save(myCustomer); - return myCustomer; - } - - public Customer updateCustomer(long id, String phone) { - Customer myCustomer = repo.findById(id); - myCustomer.phone = phone; - repo.save(myCustomer); - return myCustomer; - } - - public Customer addCustomer(CustomerDto dto) { - Customer myCustomer = new Customer(); - mapper.updateCustomerFromDto(dto, myCustomer); - repo.save(myCustomer); - return myCustomer; - } - - public Customer updateCustomer(CustomerDto dto) { - Customer myCustomer = repo.findById(dto.getId()); - mapper.updateCustomerFromDto(dto, myCustomer); - repo.save(myCustomer); - return myCustomer; - } - -} diff --git a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java new file mode 100644 index 0000000000..dc9c8821ac --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java @@ -0,0 +1,56 @@ +package com.baeldung.partialupdate; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.partialupdate.model.Customer; +import com.baeldung.partialupdate.model.CustomerDto; +import com.baeldung.partialupdate.model.CustomerStructured; +import com.baeldung.partialupdate.service.CustomerService; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = PartialUpdateApplication.class) +public class PartialUpdateUnitTest { + + @Autowired + CustomerService service; + + @Test + public void loadAndSave_whenUpdate_thenSuccess() { + Customer myCustomer = service.addCustomer("John"); + myCustomer = service.updateCustomer(myCustomer.id, "+00"); + + assertEquals("+00", myCustomer.phone); + } + + @Test + public void loadAndSaveWithMapper_whenUpdate_thenSuccess() { + CustomerDto dto = new CustomerDto(new Customer()); + dto.name = "Johnny"; + Customer entity = service.addCustomer(dto); + + CustomerDto dto2 = new CustomerDto(entity.id); + dto2.phone = "+44"; + entity = service.updateCustomer(dto2); + + assertEquals("Johnny", entity.name); + } + + @Test + public void loadAndSaveStructuredEntity_whenUpdate_thenSuccess() { + CustomerStructured myCustomer = service.addCustomerStructured("John"); + assertEquals(null, myCustomer.contactPhones); + + service.addCustomerPhone(myCustomer.id, "+44"); + myCustomer = service.updateCustomerStructured(myCustomer.id, "Mr. John"); + + assertNotEquals(null, myCustomer.contactPhones); + assertEquals(1, myCustomer.contactPhones.size()); + } +} From ab1114fb7de6d78811c84132e461c0855255cf74 Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Sun, 24 May 2020 11:45:17 +0200 Subject: [PATCH 5/7] Refactored packages --- .../java/com/baeldung/partialupdate/model/Customer.java | 7 +++---- .../java/com/baeldung/partialupdate/model/CustomerDto.java | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java index 28c5d5c76c..b19d0b7952 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java @@ -1,4 +1,4 @@ -package com.baeldung.model; +package com.baeldung.partialupdate.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -8,15 +8,14 @@ import javax.persistence.Id; @Entity public class Customer { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) public long id; public String name; public String phone; //... public String phone99; - public Customer() {} - @Override public String toString() { return String.format("Customer %s, Phone: %s", this.name, this.phone); diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java index 74b909aad8..4087838f9a 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java @@ -1,4 +1,4 @@ -package com.baeldung.model; +package com.baeldung.partialupdate.model; public class CustomerDto { private long id; From e1c3aed23b75c41a28e479ad01a31dcdfc051c66 Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Wed, 27 May 2020 19:43:04 +0200 Subject: [PATCH 6/7] Added @Query method + clean --- .../PartialUpdateApplication.java | 8 ------- .../partialupdate/model/ContactPhone.java | 4 ++-- .../partialupdate/model/CustomerDto.java | 12 +++++----- .../model/CustomerStructured.java | 7 +++--- .../repository/ContactPhoneRepository.java | 4 ++-- .../repository/CustomerRepository.java | 9 ++++++-- .../service/CustomerService.java | 8 +++++++ .../partialupdate/PartialUpdateUnitTest.java | 23 ++++++++++++------- 8 files changed, 44 insertions(+), 31 deletions(-) diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java index 30f5dfa03d..a750fcadf7 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java @@ -1,20 +1,12 @@ package com.baeldung.partialupdate; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cache.CacheManager; -import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication -@EnableCaching public class PartialUpdateApplication { - @Autowired - CacheManager cacheManager; - public static void main(String[] args) { SpringApplication.run(PartialUpdateApplication.class, args); } - } diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java index 9d611fa755..352e361bd9 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java @@ -8,11 +8,11 @@ import javax.persistence.Id; @Entity public class ContactPhone { - @Id + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public long id; @Column(nullable=false) - public long customerId; + public long customerId; public String phone; @Override diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java index 4087838f9a..0ecf206d9a 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java @@ -6,21 +6,21 @@ public class CustomerDto { public String phone; //... private String phone99; - + public CustomerDto(long id) { - this.id = id; + this.id = id; } - + public CustomerDto(Customer c) { this.id = c.id; this.name = c.name; this.phone = c.phone; } - + public long getId() { - return this.id; + return this.id; } - + public Customer convertToEntity() { Customer c = new Customer(); c.id = id; diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java index 67b534add2..dd053a963d 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java @@ -16,11 +16,12 @@ public class CustomerStructured { @GeneratedValue(strategy = GenerationType.IDENTITY) public long id; public String name; - @OneToMany(fetch = FetchType.EAGER, targetEntity=ContactPhone.class, mappedBy="customerId") + @OneToMany(fetch = FetchType.EAGER, targetEntity = ContactPhone.class, mappedBy = "customerId") public List contactPhones; - + @Override public String toString() { return String.format("Customer %s, Phone: %s", - this.name, this.contactPhones.stream().map(e -> e.toString()).reduce("", String::concat)); + this.name, this.contactPhones.stream() + .map(e -> e.toString()).reduce("", String::concat)); } } diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java index 2e0b687227..4668181e05 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java @@ -7,6 +7,6 @@ import com.baeldung.partialupdate.model.ContactPhone; @Repository public interface ContactPhoneRepository extends CrudRepository { - ContactPhone findById(long id); - ContactPhone findByCustomerId(long id); + ContactPhone findById(long id); + ContactPhone findByCustomerId(long id); } \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java index bd33c03b6b..43e61df8ab 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java @@ -1,13 +1,18 @@ package com.baeldung.partialupdate.repository; -import org.springframework.cache.annotation.Cacheable; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import com.baeldung.partialupdate.model.Customer; @Repository public interface CustomerRepository extends CrudRepository { - @Cacheable("customers") Customer findById(long id); + + @Modifying + @Query("update Customer u set u.phone = :phone where u.id = :id") + void updatePhone(@Param(value = "id") long id, @Param(value = "phone") String phone); } \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java index f3a1c65ef1..9da97a7775 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java @@ -27,6 +27,14 @@ public class CustomerService { @Autowired CustomerMapper mapper; + public Customer getCustomer(long id) { + return repo.findById(id); + } + + public void updateCustomerWithCustomQuery(long id, String phone) { + repo.updatePhone(id, phone); + } + public Customer addCustomer(String name) { Customer myCustomer = new Customer(); myCustomer.name = name; diff --git a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java index dc9c8821ac..7eea3a87e9 100644 --- a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java +++ b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java @@ -25,11 +25,18 @@ public class PartialUpdateUnitTest { public void loadAndSave_whenUpdate_thenSuccess() { Customer myCustomer = service.addCustomer("John"); myCustomer = service.updateCustomer(myCustomer.id, "+00"); - assertEquals("+00", myCustomer.phone); } - @Test + @Test + public void customQuery_whenUpdate_thenSuccess() { + Customer myCustomer = service.addCustomer("John"); + service.updateCustomerWithCustomQuery(myCustomer.id, "+88"); + myCustomer = service.getCustomer(myCustomer.id); + assertEquals("+88", myCustomer.phone); + } + + @Test public void loadAndSaveWithMapper_whenUpdate_thenSuccess() { CustomerDto dto = new CustomerDto(new Customer()); dto.name = "Johnny"; @@ -38,19 +45,19 @@ public class PartialUpdateUnitTest { CustomerDto dto2 = new CustomerDto(entity.id); dto2.phone = "+44"; entity = service.updateCustomer(dto2); - + assertEquals("Johnny", entity.name); } - - @Test - public void loadAndSaveStructuredEntity_whenUpdate_thenSuccess() { + + @Test + public void loadAndSaveStructuredEntity_whenUpdate_thenSuccess() { CustomerStructured myCustomer = service.addCustomerStructured("John"); assertEquals(null, myCustomer.contactPhones); service.addCustomerPhone(myCustomer.id, "+44"); myCustomer = service.updateCustomerStructured(myCustomer.id, "Mr. John"); - + assertNotEquals(null, myCustomer.contactPhones); assertEquals(1, myCustomer.contactPhones.size()); - } + } } From 8477fd927913b5fceabdba4e367e54def66c4fae Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Mon, 1 Jun 2020 11:09:35 +0200 Subject: [PATCH 7/7] Renamed test methods --- .../com/baeldung/partialupdate/PartialUpdateUnitTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java index 7eea3a87e9..874e18c4ad 100644 --- a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java +++ b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java @@ -22,14 +22,14 @@ public class PartialUpdateUnitTest { CustomerService service; @Test - public void loadAndSave_whenUpdate_thenSuccess() { + public void givenCustomer_whenUpdate_thenSuccess() { Customer myCustomer = service.addCustomer("John"); myCustomer = service.updateCustomer(myCustomer.id, "+00"); assertEquals("+00", myCustomer.phone); } @Test - public void customQuery_whenUpdate_thenSuccess() { + public void givenCustomer_whenUpdateWithQuery_thenSuccess() { Customer myCustomer = service.addCustomer("John"); service.updateCustomerWithCustomQuery(myCustomer.id, "+88"); myCustomer = service.getCustomer(myCustomer.id); @@ -37,7 +37,7 @@ public class PartialUpdateUnitTest { } @Test - public void loadAndSaveWithMapper_whenUpdate_thenSuccess() { + public void givenCustomerDto_whenUpdateWithMapper_thenSuccess() { CustomerDto dto = new CustomerDto(new Customer()); dto.name = "Johnny"; Customer entity = service.addCustomer(dto); @@ -50,7 +50,7 @@ public class PartialUpdateUnitTest { } @Test - public void loadAndSaveStructuredEntity_whenUpdate_thenSuccess() { + public void givenCustomerStructured_whenUpdateCustomerPhone_thenSuccess() { CustomerStructured myCustomer = service.addCustomerStructured("John"); assertEquals(null, myCustomer.contactPhones);