Reputation: 29
I have two Tables in a Database. One is Orders other one is Product.
This is a class Order
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "orders", fetch = FetchType.LAZY)
private Set<Product> products;
This is a class Product
@JsonIgnore
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "orders_products",
joinColumns = { @JoinColumn(name = "product_id", nullable = false, updatable = false)},
inverseJoinColumns = { @JoinColumn(name = "order_id", nullable = false, updatable = false)})
private Set<Order> orders;
When I send a request to REST API to add a Product into an Order:
localhost:8080/order/1/add/products?ids=1
I'll get a result as expected:
[
{
"id": 1,
"description": "Order number one description",
"name": "Shopping cart for mobiles",
"surcharge": 20.0,
"products": [
{
"id": 1,
"description": "Samsung S9",
"price": 599.99
}
]
}
]
After all when I send a GET request to get all orders:
localhost:8080/order
I will get this result:
[
{
"id": 1,
"description": "Order number one description",
"name": "Shopping cart for mobiles",
"surcharge": 20.0,
"products": []
}
]
I want "products" to be open to see actual products in. Why are "products" empty or is it not showing body of "products".
Thank you.
UPDATE:
It does not persist in database, under table orders_products. Check in Product class in @JoinTable( name = ...).
If I do following:
select * from products;
# product_id description price
1 Samsung S9 599.99
2 Notebook S9 1000.99
3 Headset 1000.99
4 Mone 45.99
select * from orders;
# order_id description name surcharge user_id
1 Order number one description Shopping cart for mobiles 20 2
2 Order for notebooks test second 20 2
But If I do. There should be ID's from order_id poiting to some of product_id. As @k-wasilewski was data does not persist in database.
select * from orders_products;
# product_id, order_id
null null
On demand of @Turing85 I am adding some code from OrderService class.
public List<Order> mapProductsToOrderById(final Long id, List<Long> ids) {
Order order = orderRepository.findById(id).orElseThrow(
() -> new OrderNotFoundException("Order with id: " + id + " was not found."));
List<Product> products = productRepository.findAll();
Set<Product> resultSet = order.getProducts();
ids.forEach(x -> products.forEach(product -> {
if(product.getId().compareTo(x) == 0){
resultSet.add(productRepository.findById(x).orElseThrow(
() -> new ProductNotFoundException("Product with id: " + x + " was not found.")
));
order.setProducts(resultSet);
orderRepository.save(order);
}
}));
return orderRepository.findAll();
}
There is method from Controller class.
@GetMapping("{id}/add/products")
public List<Order> mapProductsToOrder(@PathVariable Long id, @RequestParam("ids") List<Long> ids) {
return orderService.mapProductsToOrderById(id, ids);
}
Upvotes: 1
Views: 172
Reputation: 144
Move method mapProductsToOrderById to ProductService.class. And do same as you did by adding products into order by in reverse. So you add orders into products set.
Upvotes: 1