Added @Query method + clean
This commit is contained in:
parent
ab1114fb7d
commit
e1c3aed23b
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -8,7 +8,7 @@ public class CustomerDto {
|
|||
private String phone99;
|
||||
|
||||
public CustomerDto(long id) {
|
||||
this.id = id;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public CustomerDto(Customer c) {
|
||||
|
@ -18,7 +18,7 @@ public class CustomerDto {
|
|||
}
|
||||
|
||||
public long getId() {
|
||||
return this.id;
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public Customer convertToEntity() {
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,6 @@ import com.baeldung.partialupdate.model.ContactPhone;
|
|||
|
||||
@Repository
|
||||
public interface ContactPhoneRepository extends CrudRepository<ContactPhone, Long> {
|
||||
ContactPhone findById(long id);
|
||||
ContactPhone findByCustomerId(long id);
|
||||
ContactPhone findById(long id);
|
||||
ContactPhone findByCustomerId(long id);
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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";
|
||||
|
@ -42,8 +49,8 @@ public class PartialUpdateUnitTest {
|
|||
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);
|
||||
|
||||
|
@ -52,5 +59,5 @@ public class PartialUpdateUnitTest {
|
|||
|
||||
assertNotEquals(null, myCustomer.contactPhones);
|
||||
assertEquals(1, myCustomer.contactPhones.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue