Added @Query method + clean

This commit is contained in:
Michele Guarnaccia 2020-05-27 19:43:04 +02:00
parent ab1114fb7d
commit e1c3aed23b
8 changed files with 44 additions and 31 deletions

View File

@ -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);
}
}

View File

@ -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<ContactPhone> 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));
}
}

View File

@ -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<Customer, Long> {
@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);
}

View File

@ -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;

View File

@ -25,10 +25,17 @@ public class PartialUpdateUnitTest {
public void loadAndSave_whenUpdate_thenSuccess() {
Customer myCustomer = service.addCustomer("John");
myCustomer = service.updateCustomer(myCustomer.id, "+00");
assertEquals("+00", myCustomer.phone);
}
@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());