title: spring boot rest for one to many relation
body: <p>I created spring boot project where I am making rest application. I have used My SQL database and I am using spring data. There is one method which adds orders based on customer id. So I am not able to figure out it will work based on spring data query or custom query and how it will be?
  I have attached required codes only,</p>
  
  <h3>Customer.java</h3>
  
  <pre><code>import java.io.Serializable;
  import javax.persistence.*;
  import java.util.List;
  
  @Entity
  @Table(name = "customers")
  @NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c")
  public class Customer implements Serializable {
  private static final long serialVersionUID = 1L;
  
  @Id
  @Column(name = "cust_ID_PK")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int custIDPK;
  
  @Column(name = "billing_city")
  private String billingCity;
  
  @Column(name = "billing_country")
  private String billingCountry;
  
  @Column(name = "billing_state")
  private String billingState;
  
  @Column(name = "billing_street")
  private String billingStreet;
  
  @Column(name = "billing_zip")
  private String billingZip;
  
  private String company;
  
  @Column(name = "display_name")
  private String displayName;
  
  private String email;
  
  @Column(name = "first_name")
  private String firstName;
  
  @Column(name = "last_name")
  private String lastName;
  
  @Column(name = "middle_name")
  private String middleName;
  
  @Column(name = "other_details")
  private String otherDetails;
  
  @Column(name = "print_on_check_as")
  private String printOnCheckAs;
  
  @Column(name = "shipping_city")
  private String shippingCity;
  
  @Column(name = "shipping_country")
  private String shippingCountry;
  
  @Column(name = "shipping_state")
  private String shippingState;
  
  @Column(name = "shipping_street")
  private String shippingStreet;
  
  @Column(name = "shipping_zip")
  private String shippingZip;
  
  private String suffix;
  
  private String title;
  
  // bi-directional many-to-one association to Order
  @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
  private List&lt;Order&gt; orders;
  
  public Customer() {
  }
  
  public int getCustIDPK() {
      return this.custIDPK;
  }
  
  public void setCustIDPK(int cust_ID_PK) {
      this.custIDPK = cust_ID_PK;
  }
  
  public String getBillingCity() {
      return this.billingCity;
  }
  
  public void setBillingCity(String billingCity) {
      this.billingCity = billingCity;
  }
  
  public String getBillingCountry() {
      return this.billingCountry;
  }
  
  public void setBillingCountry(String billingCountry) {
      this.billingCountry = billingCountry;
  }
  
  public String getBillingState() {
      return this.billingState;
  }
  
  public void setBillingState(String billingState) {
      this.billingState = billingState;
  }
  
  public String getBillingStreet() {
      return this.billingStreet;
  }
  
  public void setBillingStreet(String billingStreet) {
      this.billingStreet = billingStreet;
  }
  
  public String getBillingZip() {
      return this.billingZip;
  }
  
  public void setBillingZip(String billingZip) {
      this.billingZip = billingZip;
  }
  
  public String getCompany() {
      return this.company;
  }
  
  public void setCompany(String company) {
      this.company = company;
  }
  
  public String getDisplayName() {
      return this.displayName;
  }
  
  public void setDisplayName(String displayName) {
      this.displayName = displayName;
  }
  
  public String getEmail() {
      return this.email;
  }
  
  public void setEmail(String email) {
      this.email = email;
  }
  
  public String getFirstName() {
      return this.firstName;
  }
  
  public void setFirstName(String firstName) {
      this.firstName = firstName;
  }
  
  public String getLastName() {
      return this.lastName;
  }
  
  public void setLastName(String lastName) {
      this.lastName = lastName;
  }
  
  public String getMiddleName() {
      return this.middleName;
  }
  
  public void setMiddleName(String middleName) {
      this.middleName = middleName;
  }
  
  public String getOtherDetails() {
      return this.otherDetails;
  }
  
  public void setOtherDetails(String otherDetails) {
      this.otherDetails = otherDetails;
  }
  
  public String getPrintOnCheckAs() {
      return this.printOnCheckAs;
  }
  
  public void setPrintOnCheckAs(String printOnCheckAs) {
      this.printOnCheckAs = printOnCheckAs;
  }
  
  public String getShippingCity() {
      return this.shippingCity;
  }
  
  public void setShippingCity(String shippingCity) {
      this.shippingCity = shippingCity;
  }
  
  public String getShippingCountry() {
      return this.shippingCountry;
  }
  
  public void setShippingCountry(String shippingCountry) {
      this.shippingCountry = shippingCountry;
  }
  
  public String getShippingState() {
      return this.shippingState;
  }
  
  public void setShippingState(String shippingState) {
      this.shippingState = shippingState;
  }
  
  public String getShippingStreet() {
      return this.shippingStreet;
  }
  
  public void setShippingStreet(String shippingStreet) {
      this.shippingStreet = shippingStreet;
  }
  
  public String getShippingZip() {
      return this.shippingZip;
  }
  
  public void setShippingZip(String shippingZip) {
      this.shippingZip = shippingZip;
  }
  
  public String getSuffix() {
      return this.suffix;
  }
  
  public void setSuffix(String suffix) {
      this.suffix = suffix;
  }
  
  public String getTitle() {
      return this.title;
  }
  
  public void setTitle(String title) {
      this.title = title;
  }
  
  public List&lt;Order&gt; getOrders() {
      return this.orders;
  }
  
  public void setOrders(List&lt;Order&gt; orders) {
      this.orders = orders;
  }
  
  public Order addOrder(Order order) {
      getOrders().add(order);
      order.setCustomer(this);
  
      return order;
  }
  
  public Order removeOrder(Order order) {
      getOrders().remove(order);
      order.setCustomer(null);
  
      return order;
  }
  
  }
  </code></pre>
  
  <h3>Order.java</h3>
  
  <pre><code>import java.io.Serializable;
  import javax.persistence.*;
  import java.util.Date;
  import java.util.List;
  
  
  @Entity
  @Table(name = "orders")
  @NamedQuery(name = "Order.findAll", query = "SELECT o FROM Order o")
  public class Order implements Serializable {
  private static final long serialVersionUID = 1L;
  
  @Id
  @Column(name = "order_ID_PK")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int order_ID_PK;
  
  @Column(name = "custom_message")
  private String customMessage;
  
  @Temporal(TemporalType.DATE)
  @Column(name = "delivery_due_date")
  private Date deliveryDueDate;
  
  @Temporal(TemporalType.DATE)
  @Column(name = "invoice_creation_date")
  private Date invoiceCreationDate;
  
  @Temporal(TemporalType.DATE)
  @Column(name = "payment_due_date")
  private Date paymentDueDate;
  
  // bi-directional many-to-one association to Customer
  @ManyToOne
  @JoinColumn(name = "cust_ID_FK")
  private Customer customer;
  
  // bi-directional many-to-many association to Product
  @ManyToMany(mappedBy = "orders")
  private List&lt;Product&gt; products;
  
  public Order() {
  }
  
  public int getOrder_ID_PK() {
      return this.order_ID_PK;
  }
  
  public void setOrder_ID_PK(int order_ID_PK) {
      this.order_ID_PK = order_ID_PK;
  }
  
  public String getCustomMessage() {
      return this.customMessage;
  }
  
  public void setCustomMessage(String customMessage) {
      this.customMessage = customMessage;
  }
  
  public Date getDeliveryDueDate() {
      return this.deliveryDueDate;
  }
  
  public void setDeliveryDueDate(Date deliveryDueDate) {
      this.deliveryDueDate = deliveryDueDate;
  }
  
  public Date getInvoiceCreationDate() {
      return this.invoiceCreationDate;
  }
  
  public void setInvoiceCreationDate(Date invoiceCreationDate) {
      this.invoiceCreationDate = invoiceCreationDate;
  }
  
  public Date getPaymentDueDate() {
      return this.paymentDueDate;
  }
  
  public void setPaymentDueDate(Date paymentDueDate) {
      this.paymentDueDate = paymentDueDate;
  }
  
  public Customer getCustomer() {
      return this.customer;
  }
  
  public void setCustomer(Customer customer) {
      this.customer = customer;
  }
  
  public List&lt;Product&gt; getProducts() {
      return this.products;
  }
  
  public void setProducts(List&lt;Product&gt; products) {
      this.products = products;
  }
  
  }
  </code></pre>
  
  <h3>OrderOperation.java</h3>
  
  <pre><code>package com.dao;
  
  import java.util.List;
  
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.data.jpa.repository.Query;
  import org.springframework.data.repository.query.Param;
  
  import com.model.Order;
  
  public interface OrderOperation extends JpaRepository&lt;Order, Long&gt; {
  
  @Query("SELECT c.orders FROM Customer c where c.custIDPK = :id")
  public List&lt;Order&gt; findOrderbyID(@Param("id") int id);
  }
  </code></pre>
  
  <h3>CustomerController.java</h3>
  
  <pre><code>@RestController
  @RequestMapping("/customer")
  public class CustomerController {
  
  @Autowired
  ICutomerService customerDAO;
  
  @SuppressWarnings({ "unchecked", "rawtypes" })
  @RequestMapping(value = { "/", "" }, method = RequestMethod.GET, produces = { "application/json" })
  public ResponseEntity&lt;?&gt; getAllCustomer() {
      return new ResponseEntity(customerDAO.getAllCustomer(), HttpStatus.ACCEPTED);
  }
  
  @RequestMapping(value = "/{CustomerById}", method = RequestMethod.GET, produces = { "application/json" })
  public Customer getCustomerbyId(@PathVariable("CustomerById") String cid) {
      return customerDAO.findCustomerById(Integer.parseInt(cid));
  }
  
  @SuppressWarnings({ "unchecked", "rawtypes" })
  @RequestMapping(value = "{CustomerById}/order", method = RequestMethod.GET, produces = { "application/json" })
  public ResponseEntity&lt;?&gt; getAllOrder(@PathVariable("CustomerById") String cid) {
      return new ResponseEntity(customerDAO.getOrdersbyId(Integer.parseInt(cid)), HttpStatus.ACCEPTED);
  }
  
  @SuppressWarnings({ "rawtypes", "unchecked" })
  @RequestMapping(value = "order/{CustomerById}/product", method = RequestMethod.GET, produces = {
          "application/json" })
  public ResponseEntity&lt;?&gt; getAllProduct(@PathVariable("CustomerById") String cid) {
      return new ResponseEntity(customerDAO.getProductsById(Integer.parseInt(cid)), HttpStatus.ACCEPTED);
  }
  
  @SuppressWarnings("rawtypes")
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public ResponseEntity&lt;?&gt; addCustomer(@RequestBody Customer c) {
      boolean flag = customerDAO.addCustomer(c);
      if (flag)
          return new ResponseEntity(HttpStatus.CREATED);
      else
          return new ResponseEntity(HttpStatus.BAD_REQUEST);
  }
  
  @SuppressWarnings("rawtypes")
  @RequestMapping(value = "/{CustomerById}/orders", method = RequestMethod.POST)
  public ResponseEntity&lt;?&gt; addOrders(@PathVariable("CustomerById") String cid, @RequestBody Order c) {
      // c.getCustomer().setCustIDPK(Integer.parseInt(cid));
      boolean flag = customerDAO.addOrder(c);
      if (flag) {
          return new ResponseEntity(HttpStatus.CREATED);
      } else {
          return new ResponseEntity(HttpStatus.BAD_REQUEST);
      }
  }
  }
  </code></pre>
  
  <p>How should I design this addOrders method?</p>