'Northwind - creating orders (MySQL + Spring boot)

I have DB with schema like below: enter image description here

I would like to create an Order. I created DAO of Order with all columns like below and added there relations like this:

Order

@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name ="OrderID", nullable = false,unique = true)
    private Integer orderID;

// here attributes from Columns I am skipping it

    @ManyToOne
    @JoinColumn(name = "customerID", insertable = false, updatable = false)
    @JsonIgnore
    private Customer customer;

    public Customer getCustomer() {
        return this.customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
    private Set<OrderDetails> orderDetails;
    public Set<OrderDetails> getOrderDetails() {
        return orderDetails;
    }

    public void setOrderDetails(Set<OrderDetails> orderDetails) {
        this.orderDetails = orderDetails;
    }

I am wondering now how can I create POST with order details. I tried it that way:

OrderService

public void createOrUpdateOrder(Order order){
    Set<OrderDetails> orderDetailsSet = new HashSet<>();

    for( OrderDetails orderDetails : order.getOrderDetails() ) {
        orderDetails.setOrder( order );
        orderDetails.setOrderID(order.getOrderID()); // HERE IS NULL, orderID is not set yet, maybe thats the reason
    }
    order.setOrderDetails( orderDetailsSet );
    orderRepository.save(order);
}

but that way when I GET all Orders with OrderDetails I can see that for each order orderDetails value is empty.

Here is my example POST payload:

{
"customerID":"ANATR",
"employeeID": 3,
"orderDate": "1996-07-10",
"requiredDate": "1996-07-24",
"shippedDate": "1996-07-16",
"shipVia": 2,
"freight": 58.17,
"shipName": "Hanari Carnes",
"shipAddress": "Rua do Paço, 67",
"shipCity": "Rio de Janeiro",
"shipRegion": "RJ",
"shipPostalCode": "05454-876",
"shipCountry": "Brazil",
"orderDetails":[
    {
         "unitPrice": 9,
         "quantity": 10,
         "discount": 0,
         "productID":12
    }
    ]
}

and my response for GET order:

    "orderID": 1000019,
    "customerID": "ANATR",
    "employeeID": 3,
    "orderDate": "1996-07-10",
    "requiredDate": "1996-07-24",
    "shippedDate": "1996-07-16",
    "shipVia": 2,
    "freight": 58.17,
    "shipName": "Hanari Carnes",
    "shipAddress": "Rua do Paço, 67",
    "shipCity": "Rio de Janeiro",
    "shipRegion": "RJ",
    "shipPostalCode": "05454-876",
    "shipCountry": "Brazil",
    "orderDetails": []  <----- should not be empty!

Could you please help me with that?



Solution 1:[1]

Can you try joining table with FetchMode JOIN relationship.

@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
@Fetch(FetchMode.JOIN)
private Set<OrderDetails> orderDetails;

Solution 2:[2]

Add @ManyToOne(cascade = CascadeType.PERSIST) to your Order field in OrderDetails entity, that way hibernate will know how to connect those two entities when saving both of them.

The createOrUpdateOrder method will then just look like this:

public Integer createOrUpdateOrder(Order order){
    Order newOrder = orderRepository.save(order);
    return newOrder.getOrderID();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Defaulter
Solution 2 Yuriy Kravets