title: Adding data to multiple tables using Spring forms in Spring MVC
body: <p>I have the following database schema and I need to add data to all three tables using a single view <a href="http://i.stack.imgur.com/3HXhC.png" rel="nofollow">http://i.stack.imgur.com/3HXhC.png</a> (Due to stackoverflow rules, I cannot link the image directly).</p>
  
  <p>What I hope to achieve, is to create an order, have it given an Workshop order id, and have it linked to LineItems which will let the user specify the quantity of items from the Inventory table to be added to the order.</p>
  
  <p>I can create a workshop order in my database, and create a lineitem with the workshop orders id, and add the id and quantity from an inventory item into the lineitem table, and then use the attached code to display each lineitem orderline, with the total amount of items, which item is in the order, total price, customer name etc.</p>
  
  <p>How do I go about creating a view that will let me create an order this way? The flow I imagine is:
  Create workshop order -> add line items from inventory -> save the order.</p>
  
  <p>Having worked on Spring and Hibernate for only a couple of weeks, I have not really figured out a smart approach to solve this, but hopefully someone in here has. By all means, feel free to criticize my database scheme, my classes and anything else. It may be a stupid design, not well suited for an actual production system. </p>
  
  <p>I have attached my primary classes involved in this.</p>
  
  <p>LineItems.java</p>
  
  <pre><code>@Entity
  @Table(name = "LINE_ITEMS")
  @AssociationOverrides({
  @AssociationOverride(name = "pk.inventory", 
      joinColumns = @JoinColumn(name = "INVENTORY_Id")),
  @AssociationOverride(name = "pk.workshop", 
      joinColumns = @JoinColumn(name = "WORKSHOP_ORDERS_Id"))
  })
  public class LineItems implements Serializable {
  
  private static final long serialVersionUID = 5703588914404465647L;
  
  
  @EmbeddedId
  private LineItemsPK pk = new LineItemsPK();
  
  private int quantity;
  
  
  public LineItems() {
  
  }
  
  public LineItemsPK getPK() {
      return pk;
  }
  
  public void setPK(LineItemsPK pk) {
      this.pk = pk;
  }
  
  @Column(name = "WORKSHOP_ORDERS_Id", nullable=false, updatable=false,    
      insertable=false)
  public Long getWorkshopOrdersId() {
      return getPK().getWorkshop().getId();
  }
  
  @Column(name = "Id")
  @JoinColumn(name="INVENTORY_Id", nullable=false, updatable=false, insertable=false)
  public Long getInventoryId() {
      return getPK().getInventory().getId();
  }
  
  
  @ManyToOne
  public Workshop getWorkshop() {
      return getPK().getWorkshop();
  }
  
  public void setWorkshop(Workshop workshop) {
      getPK().setWorkshop(workshop);
  }
  
  @ManyToOne
  @JoinColumn(name = "INVENTORY_Id")
  public Inventory getInventory() {
      return getPK().getInventory();
  }
  
  public void setInventory(Inventory inventory) {
      getPK().setInventory(inventory);
  }
  
  
  public int getQuantity() {
      return this.quantity;
  }
  
  public void setQuantity(int quantity) {
      this.quantity = quantity;
  }
  
  public boolean equals(Object o) {
      if (this == o) {
          return true;
      }
  
      if (o == null || getClass() != o.getClass()) {
          return false;
      }
  
      LineItems that = (LineItems) o;
  
      if (getPK() != null ? !getPK().equals(that.getPK())
              : that.getPK() != null) {
          return false;
      }
  
      return true;
  }
  
  public int hashCode() {
      return (getPK() != null ? getPK().hashCode() : 0);
  }
  } 
  </code></pre>
  
  <p>LineItemsPK.java</p>
  
  <pre><code>@Embeddable
  public class LineItemsPK implements Serializable {
  
  private static final long serialVersionUID = -4285130025882317338L;
  
  @ManyToOne
  private Inventory inventory;
  @ManyToOne
  private Workshop workshop;
  
  public Workshop getWorkshop() {
      return workshop;
  }
  
  public void setWorkshop(Workshop workshop) {
      this.workshop = workshop;
  }
  
  public Inventory getInventory() {
      return inventory;
  }
  
  public void setInventory(Inventory inventory) {
      this.inventory = inventory;
  }
  
  @Override
  public boolean equals(Object o) {
      if(this == o) {
          return true;
      }
  
      if(o == null || getClass() != o.getClass()) {
          return false;
      }
  
      LineItemsPK that = (LineItemsPK) o;
  
      if(workshop != null ? !workshop.equals(that.workshop) : that.workshop != null) {
          return false;
      }
  
      if(inventory != null ? !inventory.equals(that.inventory) : that.inventory != null) {
          return false;
      }
  
      return true;
  }
  
  @Override
  public int hashCode() {
      int result;
      result = (workshop != null ? workshop.hashCode() : 0);
      result = 31 * result + (inventory != null ? inventory.hashCode() : 0);
  
      return result;
  }
  }
  </code></pre>
  
  <p>Workshop.java</p>
  
  <pre><code>@Entity
  @Table(name = "WORKSHOP_ORDERS")
  public class Workshop implements Serializable {
  
  private static final long serialVersionUID = -8106245965993313684L;
  
  public Long id;
  public Long inventoryItemId;
  public String workshopService;
  public String workshopNotes;
  public Long customersId;
  public Long paymentId;
  
  private Customer customer;
  private Payment payment;
  
  private Set&lt;LineItems&gt; lineItems = new HashSet&lt;LineItems&gt;(0);
  
  public Workshop() {
  
  }
  
  public Workshop(Long inventoryItemId, String workshopService, String workshopNotes,
          Customer customer, Payment payment) {
  
      this.inventoryItemId = inventoryItemId;
      this.workshopService = workshopService;
      this.workshopNotes = workshopNotes;
      this.customer = customer;
      this.payment = payment;
  }
  
  public Workshop(Long inventoryItemId, String workshopService, String workshopNotes,
          Customer customer, Payment payment, Set&lt;LineItems&gt; lineItems) {
  
      this.inventoryItemId = inventoryItemId;
      this.workshopService = workshopService;
      this.workshopNotes = workshopNotes;
      this.customer = customer;
      this.payment = payment;
  
      this.lineItems = lineItems;
  }
  
  @OneToMany(mappedBy = "pk.workshop", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  public Set&lt;LineItems&gt; getLineItems() {
      return this.lineItems;
  }
  
  public void setLineItems(Set&lt;LineItems&gt; lineItems) {
      this.lineItems = lineItems;
  }
  
  @ManyToOne
  @JoinColumn(name="CUSTOMERS_Id", nullable = false, insertable = false, updatable = false)
  public Customer getCustomer() {
      return customer;
  }
  
  public void setCustomer(final Customer customer) {
      this.customer = customer;
  }
  
  @OneToOne(cascade = CascadeType.ALL)
  @JoinColumn(name="PAYMENT_Id", insertable = false, updatable = false, nullable = false)
  public Payment getPayment() {
      return payment;
  }
  
  public void setPayment(final Payment payment) {
      this.payment = payment;
  }
  
  
  @Id
  @GeneratedValue(strategy = IDENTITY)
  @Column(name = "Id", nullable = false)
  public Long getId() {
      return id;
  }
  
  @Column(name = "InventoryItemId")
  public Long getInventoryItemId() {
      return inventoryItemId;
  }
  
  @Column(name = "WorkshopService")
  public String getWorkshopService() {
      return workshopService;
  }
  
  @Column(name = "WorkshopNotes")
  public String getWorkshopNotes() {
      return workshopNotes;
  }
  
  @Column(name = "CUSTOMERS_Id")
  public Long getCustomersId() {
      return customersId;
  }
  
  @Column(name = "PAYMENT_Id")
  public Long getPaymentId() {
      return paymentId;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  
  public void setInventoryItemId(Long inventoryItemId) {
      this.inventoryItemId = inventoryItemId;
  }
  
  public void setWorkshopService(String workshopService) {
      this.workshopService = workshopService;
  }
  
  public void setWorkshopNotes(String workshopNotes) {
      this.workshopNotes = workshopNotes;
  }
  
  public void setCustomersId(Long customersId) {
      this.customersId = customersId;
  }
  
  public void setPaymentId(Long paymentId) {
      this.paymentId = paymentId;
  }
  
  public String toString() {
      return "Customer id: " + this.customersId + "Notes: " + workshopNotes;
  }
  }
  </code></pre>
  
  <p>Inventory.java</p>
  
  <pre><code>@Entity
  @Table(name = "INVENTORY")
  public class Inventory implements Serializable {
  
  private static final long serialVersionUID = -8907719450013387551L;
  
  private Long id;
  private String itemName;
  private String itemVendorName;
  private Long itemInventoryStatus;
  private Double itemBuyPrice;
  private Double itemSellPrice;
  
  private Set&lt;LineItems&gt; lineItems = new HashSet&lt;LineItems&gt;(0);
  
  public Inventory() {
  
  }
  
  public Inventory(String itemName, String itemVendorName, Long itemInventoryStatus,
          Double itemBuyPrice, Double itemSellPrice) {
      this.itemName = itemName;
      this.itemVendorName = itemVendorName;
      this.itemInventoryStatus = itemInventoryStatus;
      this.itemBuyPrice = itemBuyPrice;
      this.itemSellPrice = itemSellPrice;
  }
  
  public Inventory(String itemName, String itemVendorName, Long itemInventoryStatus,
          Double itemBuyPrice, Double itemSellPrice, Set&lt;LineItems&gt; lineItems) {
      this.itemName = itemName;
      this.itemVendorName = itemVendorName;
      this.itemInventoryStatus = itemInventoryStatus;
      this.itemBuyPrice = itemBuyPrice;
      this.itemSellPrice = itemSellPrice;
  
      this.lineItems = lineItems;
  }
  
  @OneToMany(mappedBy = "pk.inventory", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  public Set&lt;LineItems&gt; getLineItems() {
      return this.lineItems;
  }
  
  public void setLineItems(Set&lt;LineItems&gt; lineItems) {
      this.lineItems = lineItems;
  }
  
  
  @Id
  @Column(name = "Id", nullable = false)
  @GeneratedValue(strategy = IDENTITY)
  public Long getId() {
      return this.id;
  }
  
  @Column(name = "ItemName")
  public String getItemName() {
      return this.itemName;
  }
  
  @Column(name = "ItemVendorName")
  public String getItemVendorName() {
      return this.itemVendorName;
  }
  
  @Column(name = "ItemInventoryStatus")
  public Long getItemInventoryStatus() {
      return this.itemInventoryStatus;
  }
  
  @Column(name = "ItemBuyPrice")
  public Double getItemBuyPrice() {
      return this.itemBuyPrice;
  }
  
  @Column(name = "ItemSellPrice")
  public Double getItemSellPrice() {
      return this.itemSellPrice;
  }
  
  
  public void setId(Long id) {
      this.id = id;
  }
  
  public void setItemName(String itemName) {
      this.itemName = itemName;
  }
  
  public void setItemVendorName(String itemVendorName) {
      this.itemVendorName = itemVendorName;
  }
  
  public void setItemInventoryStatus(Long itemInventoryStatus) {
      this.itemInventoryStatus = itemInventoryStatus;
  }
  
  public void setItemBuyPrice(Double itemBuyPrice) {
      this.itemBuyPrice = itemBuyPrice;
  }
  
  public void setItemSellPrice(Double itemSellPrice) {
      this.itemSellPrice = itemSellPrice;
  }
  
  public String toString() {
      return "Item id:" + this.id + " ItemName: " + this.itemName +
              " ItemInventoryStatus: " + this.itemInventoryStatus +
              " ItemBuyPrice: " + this.itemBuyPrice + " ItemSellPrice " + this.itemSellPrice;
  }
  }
  </code></pre>