Added @Query method + clean
This commit is contained in:
parent
ab1114fb7d
commit
e1c3aed23b
|
@ -1,20 +1,12 @@
|
||||||
package com.baeldung.partialupdate;
|
package com.baeldung.partialupdate;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.cache.CacheManager;
|
|
||||||
import org.springframework.cache.annotation.EnableCaching;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableCaching
|
|
||||||
public class PartialUpdateApplication {
|
public class PartialUpdateApplication {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
CacheManager cacheManager;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(PartialUpdateApplication.class, args);
|
SpringApplication.run(PartialUpdateApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,11 +16,12 @@ public class CustomerStructured {
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
public long id;
|
public long id;
|
||||||
public String name;
|
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;
|
public List<ContactPhone> contactPhones;
|
||||||
|
|
||||||
@Override public String toString() {
|
@Override public String toString() {
|
||||||
return String.format("Customer %s, Phone: %s",
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,18 @@
|
||||||
package com.baeldung.partialupdate.repository;
|
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.CrudRepository;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.baeldung.partialupdate.model.Customer;
|
import com.baeldung.partialupdate.model.Customer;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface CustomerRepository extends CrudRepository<Customer, Long> {
|
public interface CustomerRepository extends CrudRepository<Customer, Long> {
|
||||||
@Cacheable("customers")
|
|
||||||
Customer findById(long id);
|
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);
|
||||||
}
|
}
|
|
@ -27,6 +27,14 @@ public class CustomerService {
|
||||||
@Autowired
|
@Autowired
|
||||||
CustomerMapper mapper;
|
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) {
|
public Customer addCustomer(String name) {
|
||||||
Customer myCustomer = new Customer();
|
Customer myCustomer = new Customer();
|
||||||
myCustomer.name = name;
|
myCustomer.name = name;
|
||||||
|
|
|
@ -25,10 +25,17 @@ public class PartialUpdateUnitTest {
|
||||||
public void loadAndSave_whenUpdate_thenSuccess() {
|
public void loadAndSave_whenUpdate_thenSuccess() {
|
||||||
Customer myCustomer = service.addCustomer("John");
|
Customer myCustomer = service.addCustomer("John");
|
||||||
myCustomer = service.updateCustomer(myCustomer.id, "+00");
|
myCustomer = service.updateCustomer(myCustomer.id, "+00");
|
||||||
|
|
||||||
assertEquals("+00", myCustomer.phone);
|
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
|
@Test
|
||||||
public void loadAndSaveWithMapper_whenUpdate_thenSuccess() {
|
public void loadAndSaveWithMapper_whenUpdate_thenSuccess() {
|
||||||
CustomerDto dto = new CustomerDto(new Customer());
|
CustomerDto dto = new CustomerDto(new Customer());
|
||||||
|
|
Loading…
Reference in New Issue