title: How to save one-to-many in hibernate/jpa?
body: <p>Not sure if I described one-to-many relationship with JPA right. 
  I have 3 tables: 
  Handyman table contains name, address, phone number etc.
  Company table has : Company name, address, phone number etc.
  WorkOrder table: workOrder number, job site address, FK to handyman's ID, FK to company's ID.
  When adding new workorder I use select to pick company and handyman names from database, then fill the workorder form.
  The question is how to save it now?</p>
  
  <p><strong>Handyman.java</strong></p>
  
  <pre><code>  @OneToMany(cascade=CascadeType.ALL)
  @JoinColumn(name="Handymans_list_id")
  private Set&lt;WorkOrder&gt; workOrders;
  public Set&lt;WorkOrder&gt; getWorkOrders() {
      return workOrders;
  }
  public void setWorkOrders(Set&lt;WorkOrder&gt; workOrders) {
      this.workOrders = workOrders;
  }
  </code></pre>
  
  <p><strong>Company.java</strong></p>
  
  <pre><code>    @OneToMany(cascade=CascadeType.ALL)
  @JoinColumn(name="Companys_list_id")
  private Set&lt;WorkOrder&gt; workOrders;
  public Set&lt;WorkOrder&gt; getWorkOrders() {
      return workOrders;
  }
  public void setWorkOrders(Set&lt;WorkOrder&gt; workOrders) {
      this.workOrders = workOrders;
  }
  </code></pre>
  
  <p><strong>WorkOrder.java</strong></p>
  
  <pre><code>    @ManyToOne(optional=false)
  @JoinColumn(name="Companys_list_id", referencedColumnName="id")
  //@ManyToOne  
  //@JoinColumn(name = "Companys_list_id")
  private Company company;
  
  @ManyToOne(optional=false)
  @JoinColumn(name="Handymans_list_id", referencedColumnName="id")
  
  //@ManyToOne  
  //@JoinColumn(name = "Handymans_list_id") 
  private Handyman handyman;
  </code></pre>
  
  <p><strong>WorkOrderDAOHibernate.java</strong></p>
  
  <pre><code>    public WorkOrder saveWorkOrder(WorkOrder workOrder)
  {
      System.out.println("Open session for saving work order ...");
      SessionFactory sf = HibernateUtil.getSessionFactory();
      Session session =  sf.openSession();
      Transaction trans = session.beginTransaction();
      //Integer id = (Integer) session.save(workOrder);
      //Company company = workOrder.getCompany();
      //Handyman handyman = new Handyman();
      //String name  = handyman.getHandymanName();
      //System.out.println("Printing company =" + company);
      //System.out.println("Printing handyman = "+ handyman);
      //System.out.println("Handymans name = " + name);
  
      //
  
      //workOrder.setCompany(company);
  
      //workOrder.setHandyman(handyman);
      System.out.println("Workorder "+workOrder);
      Integer id = (Integer) session.save(workOrder);
  
      trans.commit();
      workOrder.setId(id);
      session.close();
      return workOrder;
  
  }
  </code></pre>
  
  <p><strong>workorder3.jsp</strong></p>
  
  <pre><code>    &lt;s:form action="resultAction1" namespace="/"&gt;
  
          &lt;s:actionerror/&gt;
          &lt;s:textfield name="workOrder.workOrder"      label="Work order number" /&gt;
          &lt;s:textfield name="workOrder.address"        label="Adress/Location" /&gt;
          &lt;s:textfield name="workOrder.email"          label="Email" /&gt;
          &lt;s:textfield name="workOrder.comment"        label="Comment" /&gt;
          &lt;s:textfield name="workOrder.dateReceived"   label="Date receved" /&gt;
          &lt;s:textfield name="workOrder.price"          label="Price" /&gt;
  
  &lt;h4&gt;
  &lt;s:select label="Pick the company name " 
      headerKey="-1" headerValue="Select Company name"
      list="companyList" 
      name="yourCompanyList" 
       /&gt;
  &lt;/h4&gt; 
  
  &lt;h4&gt;
  &lt;s:select label="Pick handyman name " 
      headerKey="-1" headerValue="Select Handyman name"
      list="handymanList" 
      name="yourHandymanList" 
       /&gt;
  &lt;/h4&gt; 
  
  
          &lt;s:submit value="submit" name="submit" /&gt;
  </code></pre>