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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,11 +8,11 @@ import javax.persistence.Id;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class ContactPhone {
|
public class ContactPhone {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
public long id;
|
public long id;
|
||||||
@Column(nullable=false)
|
@Column(nullable=false)
|
||||||
public long customerId;
|
public long customerId;
|
||||||
public String phone;
|
public String phone;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -6,21 +6,21 @@ public class CustomerDto {
|
||||||
public String phone;
|
public String phone;
|
||||||
//...
|
//...
|
||||||
private String phone99;
|
private String phone99;
|
||||||
|
|
||||||
public CustomerDto(long id) {
|
public CustomerDto(long id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CustomerDto(Customer c) {
|
public CustomerDto(Customer c) {
|
||||||
this.id = c.id;
|
this.id = c.id;
|
||||||
this.name = c.name;
|
this.name = c.name;
|
||||||
this.phone = c.phone;
|
this.phone = c.phone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getId() {
|
public long getId() {
|
||||||
return this.id;
|
return this.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Customer convertToEntity() {
|
public Customer convertToEntity() {
|
||||||
Customer c = new Customer();
|
Customer c = new Customer();
|
||||||
c.id = id;
|
c.id = id;
|
||||||
|
|
|
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,6 @@ import com.baeldung.partialupdate.model.ContactPhone;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface ContactPhoneRepository extends CrudRepository<ContactPhone, Long> {
|
public interface ContactPhoneRepository extends CrudRepository<ContactPhone, Long> {
|
||||||
ContactPhone findById(long id);
|
ContactPhone findById(long id);
|
||||||
ContactPhone findByCustomerId(long id);
|
ContactPhone findByCustomerId(long id);
|
||||||
}
|
}
|
|
@ -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,11 +25,18 @@ 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
|
@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() {
|
public void loadAndSaveWithMapper_whenUpdate_thenSuccess() {
|
||||||
CustomerDto dto = new CustomerDto(new Customer());
|
CustomerDto dto = new CustomerDto(new Customer());
|
||||||
dto.name = "Johnny";
|
dto.name = "Johnny";
|
||||||
|
@ -38,19 +45,19 @@ public class PartialUpdateUnitTest {
|
||||||
CustomerDto dto2 = new CustomerDto(entity.id);
|
CustomerDto dto2 = new CustomerDto(entity.id);
|
||||||
dto2.phone = "+44";
|
dto2.phone = "+44";
|
||||||
entity = service.updateCustomer(dto2);
|
entity = service.updateCustomer(dto2);
|
||||||
|
|
||||||
assertEquals("Johnny", entity.name);
|
assertEquals("Johnny", entity.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void loadAndSaveStructuredEntity_whenUpdate_thenSuccess() {
|
public void loadAndSaveStructuredEntity_whenUpdate_thenSuccess() {
|
||||||
CustomerStructured myCustomer = service.addCustomerStructured("John");
|
CustomerStructured myCustomer = service.addCustomerStructured("John");
|
||||||
assertEquals(null, myCustomer.contactPhones);
|
assertEquals(null, myCustomer.contactPhones);
|
||||||
|
|
||||||
service.addCustomerPhone(myCustomer.id, "+44");
|
service.addCustomerPhone(myCustomer.id, "+44");
|
||||||
myCustomer = service.updateCustomerStructured(myCustomer.id, "Mr. John");
|
myCustomer = service.updateCustomerStructured(myCustomer.id, "Mr. John");
|
||||||
|
|
||||||
assertNotEquals(null, myCustomer.contactPhones);
|
assertNotEquals(null, myCustomer.contactPhones);
|
||||||
assertEquals(1, myCustomer.contactPhones.size());
|
assertEquals(1, myCustomer.contactPhones.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue