The basic CRUD features are tested and it works fine.
My question is about joining tables.
I made a very simple example to demonstrate my case.
This is my "Order" entity which has multiple "OrderItem" entities.
@Table(name="orders")
@Data
@ToString(of = {"id", "description", "timestamp"})
public class Order {
@PrimaryKey
@Column(name="order_id")
private String id;
private String description;
@Column(name="creation_timestamp")
private LocalDateTime timestamp;
@Interleaved(lazy = true)
private List<OrderItem> items = new ArrayList<>();
public Order() {
this.id = UUID.randomUUID().toString();
this.timestamp = LocalDateTime.now();
}
public Order(String description, OrderItem... orderItems) {
this.id = UUID.randomUUID().toString();
this.timestamp = LocalDateTime.now();
this.description = description;
for (OrderItem orderItem : orderItems) {
orderItem.setOrderId(id);
items.add(orderItem);
}
}
}
and this is the "OrderItem" entity
@Table(name="order_items")
@Data
@ToString(of = {"orderItemId", "orderId", "description", "quantity"})
public class OrderItem {
@PrimaryKey(keyOrder = 1)
@Column(name="order_id")
private String orderId;
@PrimaryKey(keyOrder = 2)
@Column(name="order_item_id")
private String orderItemId;
private String description;
private Long quantity;
public OrderItem(String description, Long quantity) {
this.orderItemId = UUID.randomUUID().toString();
this.description = description;
this.quantity = quantity;
}
}
And this is a simple controller to check if join functions well.
@GetMapping("/api/orders")
public String test() {
List<Order> list =orderRepository.findAll();
for (Order o: list) {
List<OrderItem> itemList =o.getItems();
System.out.println("-----");
for(OrderItem oi : itemList) {
System.out.println(oi);
}
System.out.println("-----");
}
return "";
}
When I set "items" in "Order" entity as @Interleaved(lazy = false), the orderRepository.findAll() function get the whole data including "OrderItem" well.
@Interleaved(lazy = false)
private List<OrderItem> items = new ArrayList<>();
It is normal because I set the lazy false, which means "eager" way fetch
But the main problem is, When I set "items" in "Order" entity as @Interleaved(lazy = true), the same print from the same code is like below
which indicates that the lazy loading has not executed.
As far as I learned about lazy loading, the data of "OrderItems" are not supposed to load when orderRepository.findAll(); is called, but supposed to load at least when
for(OrderItem oi : itemList) {
System.out.println(oi);
}
is called.
Am I setting something wrong here? Should I use the interleaved in different way?
Hello, I am currently trying to use
spring-cloud-gcp-starter-data-spanner
andspring-cloud-gcp-starter
Springboot v2.3.3
The basic CRUD features are tested and it works fine.
My question is about joining tables.
I made a very simple example to demonstrate my case.
This is my "Order" entity which has multiple "OrderItem" entities.
and this is the "OrderItem" entity
And this is a simple controller to check if join functions well.
When I set "items" in "Order" entity as
@Interleaved(lazy = false)
, theorderRepository.findAll()
function get the whole data including "OrderItem" well.It is normal because I set the lazy false, which means "eager" way fetch
But the main problem is, When I set "items" in "Order" entity as![스크린샷 2021-02-25 오후 12 02 37](https://user-images.githubusercontent.com/71802784/109096731-6bdb4f80-7761-11eb-9635-5d3492beb2e9.png)
@Interleaved(lazy = true)
, the same print from the same code is like belowwhich indicates that the lazy loading has not executed.
As far as I learned about lazy loading, the data of "OrderItems" are not supposed to load when
orderRepository.findAll();
is called, but supposed to load at least whenis called.
Am I setting something wrong here? Should I use the
interleaved
in different way?Thanks.