jpashop : get data api v1 (entity)

This commit is contained in:
kim
2021-01-26 19:54:18 +09:00
parent 3c2fdcdd38
commit 4e9ead35d7
6 changed files with 47 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-devtools'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-hibernate5'
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.5.6'

View File

@@ -1,7 +1,9 @@
package com.example.jpashop;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class JpashopApplication {
@@ -9,4 +11,9 @@ public class JpashopApplication {
public static void main(String[] args) {
SpringApplication.run(JpashopApplication.class, args);
}
@Bean
Hibernate5Module hibernate5Module() {
return new Hibernate5Module();
// .configure(Hibernate5Module.Feature.FORCE_LAZY_LOADING, true);
}
}

View File

@@ -0,0 +1,33 @@
package com.example.jpashop.api;
import com.example.jpashop.domain.Order;
import com.example.jpashop.repository.OrderRepository;
import com.example.jpashop.repository.OrderSearch;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* xToOne (ManyToOne, OneToOne)
* Order
* Order -> Member
* Order -> Delivery
*/
@RestController
@RequiredArgsConstructor
public class OrderSimpleApiController {
private final OrderRepository orderRepository;
@GetMapping("/api/v1/simple-orders")
public List<Order> ordersV1() {
List<Order> all = orderRepository.findAllByString(new OrderSearch());
all.forEach(order -> {
order.getMember().getName(); // Lazy 강제 초기화
order.getDelivery().getAddress(); // Lazy 강제 초기화
});
return all;
}
}

View File

@@ -1,5 +1,6 @@
package com.example.jpashop.domain;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
@@ -13,6 +14,7 @@ public class Delivery {
@Column(name = "delivery_id")
private Long id;
@JsonIgnore
@OneToOne(mappedBy = "delivery", fetch = FetchType.LAZY)
private Order order;

View File

@@ -1,5 +1,6 @@
package com.example.jpashop.domain;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import javax.persistence.*;
@@ -22,6 +23,7 @@ public class Member {
@Embedded
private Address address;
@JsonIgnore
@OneToMany(mappedBy = "member")
private List<Order> order = new ArrayList<>();
}

View File

@@ -1,6 +1,7 @@
package com.example.jpashop.domain;
import com.example.jpashop.domain.item.Item;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
@@ -21,6 +22,7 @@ public class OrderItem {
@JoinColumn(name = "item_id")
private Item item;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "order_id")
private Order order;