From 0a1771365060a39108239f180bd51d86dbc5f3b7 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Fri, 21 Jun 2024 17:34:32 +0000 Subject: [PATCH] feat : fix issue with mapping child in parent --- .../hibernatecache/entities/Order.java | 12 +++++ .../hibernatecache/mapper/CustomerMapper.java | 9 +++- .../mapper/OrderItemMapper.java | 7 ++- .../hibernatecache/mapper/OrderMapper.java | 9 +++- .../controllers/OrderItemControllerIT.java | 53 +++++++++++++------ 5 files changed, 72 insertions(+), 18 deletions(-) diff --git a/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/entities/Order.java b/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/entities/Order.java index 0f776e061..2a6c270f0 100644 --- a/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/entities/Order.java +++ b/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/entities/Order.java @@ -88,6 +88,18 @@ public Order setOrderItems(List orderItems) { return this; } + public Order addOrderItem(OrderItem orderItem) { + this.orderItems.add(orderItem); + orderItem.setOrder(this); + return this; + } + + public Order removeOrderItem(OrderItem orderItem) { + this.orderItems.remove(orderItem); + orderItem.setOrder(null); + return this; + } + @Override public final boolean equals(Object o) { if (this == o) return true; diff --git a/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/mapper/CustomerMapper.java b/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/mapper/CustomerMapper.java index 3cac8680c..56b03ae55 100644 --- a/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/mapper/CustomerMapper.java +++ b/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/mapper/CustomerMapper.java @@ -4,13 +4,20 @@ import com.example.hibernatecache.model.request.CustomerRequest; import com.example.hibernatecache.model.response.CustomerResponse; import java.util.List; +import org.mapstruct.InjectionStrategy; import org.mapstruct.IterableMapping; import org.mapstruct.Mapper; import org.mapstruct.Mapping; +import org.mapstruct.MappingConstants; import org.mapstruct.MappingTarget; import org.mapstruct.NullValueCheckStrategy; -@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS) +@Mapper( + componentModel = MappingConstants.ComponentModel.SPRING, + nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, + uses = OrderMapper.class, + injectionStrategy = InjectionStrategy.CONSTRUCTOR, + suppressTimestampInGenerated = true) public interface CustomerMapper { Customer toEntity(CustomerRequest customerRequest); diff --git a/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/mapper/OrderItemMapper.java b/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/mapper/OrderItemMapper.java index 261876724..94f2d3e04 100644 --- a/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/mapper/OrderItemMapper.java +++ b/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/mapper/OrderItemMapper.java @@ -7,13 +7,18 @@ import org.mapstruct.IterableMapping; import org.mapstruct.Mapper; import org.mapstruct.Mapping; +import org.mapstruct.MappingConstants; import org.mapstruct.MappingTarget; import org.mapstruct.NullValueCheckStrategy; -@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS) +@Mapper( + componentModel = MappingConstants.ComponentModel.SPRING, + nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, + suppressTimestampInGenerated = true) public interface OrderItemMapper { @Mapping(target = "order.id", source = "orderId") + @Mapping(target = "id", ignore = true) OrderItem toEntity(OrderItemRequest orderItemRequest); @Mapping(source = "id", target = "orderItemId") diff --git a/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/mapper/OrderMapper.java b/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/mapper/OrderMapper.java index a5676111d..a0f2629a6 100644 --- a/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/mapper/OrderMapper.java +++ b/jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/mapper/OrderMapper.java @@ -4,13 +4,20 @@ import com.example.hibernatecache.model.request.OrderRequest; import com.example.hibernatecache.model.response.OrderResponse; import java.util.List; +import org.mapstruct.InjectionStrategy; import org.mapstruct.IterableMapping; import org.mapstruct.Mapper; import org.mapstruct.Mapping; +import org.mapstruct.MappingConstants; import org.mapstruct.MappingTarget; import org.mapstruct.NullValueCheckStrategy; -@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS) +@Mapper( + componentModel = MappingConstants.ComponentModel.SPRING, + nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, + uses = OrderItemMapper.class, + injectionStrategy = InjectionStrategy.CONSTRUCTOR, + suppressTimestampInGenerated = true) public interface OrderMapper { @Mapping(target = "orderItems", ignore = true) diff --git a/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/web/controllers/OrderItemControllerIT.java b/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/web/controllers/OrderItemControllerIT.java index 3085f7659..540b409a8 100644 --- a/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/web/controllers/OrderItemControllerIT.java +++ b/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/web/controllers/OrderItemControllerIT.java @@ -20,7 +20,6 @@ import com.example.hibernatecache.repositories.OrderItemRepository; import com.example.hibernatecache.repositories.OrderRepository; import java.math.BigDecimal; -import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -51,19 +50,20 @@ void setUp() { .setFirstName("firstName 1") .setLastName("lastName 1") .setEmail("email1@junit.com") - .setPhone("9876543211")); - savedOrder = - orderRepository.persist( - new Order() - .setName("First Order") - .setPrice(BigDecimal.TEN) - .setCustomer(savedCustomer)); - - orderItemList = new ArrayList<>(); - orderItemList.add(new OrderItem().setText("First OrderItem").setOrder(savedOrder)); - orderItemList.add(new OrderItem().setText("Second OrderItem").setOrder(savedOrder)); - orderItemList.add(new OrderItem().setText("Third OrderItem").setOrder(savedOrder)); - orderItemList = orderItemRepository.persistAll(orderItemList); + .setPhone("9876543211") + .addOrder( + new Order() + .setName("First Order") + .setPrice(BigDecimal.TEN) + .addOrderItem( + new OrderItem().setText("First OrderItem")) + .addOrderItem( + new OrderItem().setText("Second OrderItem")) + .addOrderItem( + new OrderItem() + .setText("Third OrderItem")))); + savedOrder = savedCustomer.getOrders().getFirst(); + orderItemList = savedOrder.getOrderItems(); } @Test @@ -71,6 +71,10 @@ void shouldFetchAllOrderItems() throws Exception { this.mockMvc .perform(get("/api/order/items")) .andExpect(status().isOk()) + .andExpect( + header().string( + HttpHeaders.CONTENT_TYPE, + is(MediaType.APPLICATION_JSON_VALUE))) .andExpect(jsonPath("$.data.size()", is(orderItemList.size()))) .andExpect(jsonPath("$.totalElements", is(3))) .andExpect(jsonPath("$.pageNumber", is(1))) @@ -89,6 +93,10 @@ void shouldFindOrderItemById() throws Exception { this.mockMvc .perform(get("/api/order/items/{id}", orderItemId)) .andExpect(status().isOk()) + .andExpect( + header().string( + HttpHeaders.CONTENT_TYPE, + is(MediaType.APPLICATION_JSON_VALUE))) .andExpect(jsonPath("$.orderItemId", is(orderItem.getId()), Long.class)) .andExpect(jsonPath("$.text", is(orderItem.getText()))); } @@ -104,6 +112,10 @@ void shouldCreateNewOrderItem() throws Exception { .content(objectMapper.writeValueAsString(orderItemRequest))) .andExpect(status().isCreated()) .andExpect(header().exists(HttpHeaders.LOCATION)) + .andExpect( + header().string( + HttpHeaders.CONTENT_TYPE, + is(MediaType.APPLICATION_JSON_VALUE))) .andExpect(jsonPath("$.orderItemId", notNullValue())) .andExpect(jsonPath("$.text", is(orderItemRequest.text()))); } @@ -118,7 +130,10 @@ void shouldReturn400WhenCreateNewOrderItemWithoutText() throws Exception { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(orderItemRequest))) .andExpect(status().isBadRequest()) - .andExpect(header().string("Content-Type", is("application/problem+json"))) + .andExpect( + header().string( + HttpHeaders.CONTENT_TYPE, + is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) .andExpect(jsonPath("$.type", is("about:blank"))) .andExpect(jsonPath("$.title", is("Constraint Violation"))) .andExpect(jsonPath("$.status", is(400))) @@ -142,6 +157,10 @@ void shouldUpdateOrderItem() throws Exception { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(orderItemRequest))) .andExpect(status().isOk()) + .andExpect( + header().string( + HttpHeaders.CONTENT_TYPE, + is(MediaType.APPLICATION_JSON_VALUE))) .andExpect(jsonPath("$.orderItemId", is(orderItemId), Long.class)) .andExpect(jsonPath("$.text", is(orderItemRequest.text()))); } @@ -153,6 +172,10 @@ void shouldDeleteOrderItem() throws Exception { this.mockMvc .perform(delete("/api/order/items/{id}", orderItem.getId())) .andExpect(status().isOk()) + .andExpect( + header().string( + HttpHeaders.CONTENT_TYPE, + is(MediaType.APPLICATION_JSON_VALUE))) .andExpect(jsonPath("$.orderItemId", is(orderItem.getId()), Long.class)) .andExpect(jsonPath("$.text", is(orderItem.getText()))); }