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()); - } + } }