title: Persisting already persisted object within a relationship dependency
body: <p>I have a relationship n/n between Product and Order
  So I have a third table ProductOrder, because I need new columns when they are created.</p>
  
  <pre><code>public class Order implements Serializable {
  
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ORDER_SEQ")
  @Column(unique = true, nullable = false, updatable = false)
  private Long idOrder;
  
  @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
  private Set&lt;ProductOrder&gt; productOrder;
      //get and setter
  </code></pre>
  
  <p>here is the ProductOrder:</p>
  
  <pre><code>@Entity
  @IdClass(ProductOrderId.class)
  public class ProductOrder implements Serializable {
  
  private static final long serialVersionUID = 3943799614725570559L;
  
  @Id
  @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinColumn(name = "product_id")
  private Product product;
  
  @Id
  @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinColumn(name = "order_id")
  private Order order;
  
  private Integer qtdProduct;
  
  private Double unitValueProductOrder;
  
  //get and setter
  </code></pre>
  
  <p>also My ProcutOrderId (just in case)</p>
  
  <pre><code>public class ItemCompraId implements Serializable {
  
  private Long compra;
  
  private Long produto;
  
  //get and set
  </code></pre>
  
  <p>and my Order entity:</p>
  
  <pre><code>@Entity
  @SequenceGenerator(name = "ORDER_SEQ", sequenceName = "s_compra", initialValue = 1, allocationSize = 1)
  public class Order implements Serializable {
  
  private static final long serialVersionUID = 3943799614725570559L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ORDER_SEQ")
  @Column(unique = true, nullable = false, updatable = false)
  private Long idOrder;
  
  private Double orderValue;
  
  private Date creatingDate;
  
  @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
  private Set&lt;ProductOrder&gt; productOrder;
  </code></pre>
  
  <p>So basically I have any Products ALREADY persisted in db... I just got a list of them when some order is about to be ordered. So I wanna persist a new object (Order) based on some already persisted object (products). This is the method invoked on managedbean to persist an Order.</p>
  
  <pre><code>public String doOrder() throws Exception {
  
      try {
          Order order = new Order();
          compra.setCreatingDate(new Date());
          compra.setOrderValue(null);
  
          if (compra.getProductOrder() == null)
              compra.setProductOrder(new HashSet&lt;ProductOrder&gt;());
          for (Product product : listOfMyCartOfProducts) {
  
              ProductOrder productOrder = new ProductOrder();
              productOrder.setQtdProduct(100);
              productOrder.unitValueProductOrder(null);
              productOrder.setOrder(order);
              productOrder.setProduct(product); //I THINK THAT THE PROBLEM IT'S HERE
  
              order.getOrderProduct().add(productOrder);
          }
  
          ejbInvoke.persist(order); //tryed .merge and it doesn't work aswell
  
          return "stuff";
  </code></pre>
  
  <p>Any ideas?
  I'm desperate.. I need this working for yesterday.. 
  Any help please??</p>
  
  <p>Btw I'm using JSF 2.0, Hibernate with JPA 2.0 and Postgres.
  Regards,</p>