title: onetomany unidirectional with jointable setup using jpa
body: <p>I have two entities namely customer and order in a onetomany relationship.
  One customer can have multiple orders.
  Since i needed this relationship to be unidirectional, i am using a joinTable.</p>
  
  <p>I am able to add entries to my customer entity using JPA.
  I am able to add entries to my order entity using JPA.</p>
  
  <p>I am wondering how to connect the two together data.
  Let's say i have one entry in customer table and two entries in order table.
  I would like to associate these two entries in order table to the one entry in customer table.</p>
  
  <p>Currently, i don't see any entries in my jointable customer_order. How do i make the association? I guess while i am adding the orders into the order table, i will have to mention the customer id number somehow. Not sure how to do that. Is there a criteria query for that in JPA?</p>
  
  <p>Thanks.</p>
  
  <p>Customer Class -</p>
  
  <pre><code>@Entity
  public class Customer implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.TABLE, generator = "generatorCustomer")
      @TableGenerator(name = "generatorCustomer", allocationSize = 1)
      @Column(name="customer_id")
      private Long id;
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      @OneToMany()
      @JoinTable (
          name="customer_order",
          joinColumns={ @JoinColumn(name="customer_id", referencedColumnName="customer_id") },
          inverseJoinColumns={ @JoinColumn(name="order_id", referencedColumnName="order_id", unique=true) }
      )
      private List&lt;Order&gt; orderList = new ArrayList&lt;Order&gt;();
      public List&lt;Order&gt; getOrderList() {
          if(this.orderList == null) {
              this.orderList = new ArrayList&lt;Order&gt;();
          }
          return this.orderList;
      }
  
      public void setOrderList(List&lt;Order&gt; orderList) {
          this.orderList = orderList;
      }
  
      public void addOrder(Order order) {
          this.orderList.add(order);
      }
  
      /* other logic follows......... */
  }
  </code></pre>
  
  <p>Order Class -</p>
  
  <pre><code>@Entity
  public class Order implements Serializable {
  @Id
  @GeneratedValue
  @Column(name="order_id")
  private Long id;
  public Long getId() {
      return id;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  
  /* other logic follows......... */
  }
  </code></pre>
  
  <p>Customer table description:</p>
  
  <pre><code>jbossql=# \d customer
  Column   |         Type          |                        Modifiers
  -------------+-----------------------+---------------------------------------------------------------
  customer_id | bigint                | not null default nextval('customer_customer_id_seq'::regclass)
  name        | character varying(50) | 
  Indexes:
  "customer_pkey" PRIMARY KEY, btree (customer_id)
  Referenced by:
  TABLE "customer_order" CONSTRAINT "fk8ef2f420ec016855" FOREIGN KEY (customer_id) REFERENCES customer(customer_id)
  </code></pre>
  
  <p>Order Table description:</p>
  
  <pre><code>jbossql=# \d order
  Column   |  Type  | Modifiers 
  ------------+--------+-----------
  order_id   | bigint | not null
  value      | real   | 
  Indexes:
  "order_pkey" PRIMARY KEY, btree (order_id)
  Referenced by:
  TABLE "customer_order" CONSTRAINT "fk1e4828a98187660" FOREIGN KEY (order_id) REFERENCES order(order_id)
  </code></pre>
  
  <p>customer_order joint table description:</p>
  
  <pre><code>jbossql=# \d customer_order
  Column       |  Type  | Modifiers 
  --------------+--------+-----------
  customer_id  | bigint | not null
  order_id     | bigint | not null
  Indexes:
  "customer_order_order_id_key" UNIQUE CONSTRAINT, btree (order_id)
  Foreign-key constraints:
  "fk1e4828a98187660" FOREIGN KEY (order_id) REFERENCES order(order_id)
  "fk1e4828adba386b8" FOREIGN KEY (customer_id) REFERENCES customer(customer_id)
  </code></pre>
  
  <p>I am able to insert an item into the customer table:</p>
  
  <pre><code>jbossql=# select * from customer;
  customer_id | name 
  -------------+-------------
  1        | joe
  (1 row)
  </code></pre>
  
  <p>I am able to insert items into the oder table as well:</p>
  
  <pre><code>jbossql=# select * from order;
  order_id | value 
  ----------+-----------
  1       |  1.8
  2       |  0.5
  (2 rows)
  </code></pre>
  
  <p>I was thinking the customer_order table would automatically get populated i.e hibernate would take of that. But appears not because my jointable is empty:</p>
  
  <pre><code>jbossql=# select * from customer_order;
  customer_id | order_id 
  -------------+-----------
  (0 rows)
  </code></pre>
  
  <p>So, my intention is to make those two order entries connected to customer joe.</p>
  
  <p>Please help.</p>
  
  <ol>
  <li>Do i have to explicitly add a record to the customer_order table?</li>
  <li>If not, how do i insert items into order table so that i can connect that entry to a particular customer?</li>
  <li>Are my mappings right in the java files for this OneToMany [unidirection] relationship to work as expected</li>
  <li>I am using JPA2 and JBoss and Hibernate. Do you have any code references to TEST a one-to-many relationship? Any references for a complete project or reading material would help.</li>
  </ol>
  
  <p>Thanks for looking.</p>