webtier@glassfish.java.net

Composite PK for Child Entity

From: <webtier_at_javadesktop.org>
Date: Fri, 10 Sep 2010 15:45:22 PDT

GFv3.0.1, JPA 2.0, EJB 3.1, JSF + Primefaces 2.2

Hi
  I have a problem that i am struggling with. I have a master detail model (order header - order lines)

Order Header - has a PK Order_ID which is a sequence generated ID
Order Lines - has a composite PK comprised of Order ID, Product_code.
The relationship b/w Order Header and Order Lines is OneToMany

  @Entity
  public class Order implements Serializable
  private static final long serialVersionUID = 1L;
  @Id
  @SequenceGenerator(name="Order_Id_Gen", sequenceName="order_id_seq", allocationSize=1)
  @GeneratedValue(generator="Order_Id_Gen")
  @Basic(optional = false)
  @Column(name = "order_id")
  private Integer orderId;
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "orderLine")
  private List<OrderLine> orderLineList;

@Entity
public class OrderLine implements Serializable {
  private static final long serialVersionUID = 1L;
  @EmbeddedId
  protected OrderLinePK orderLinePK;

@Embeddable
public class OrderLinePK implements Serializable {
  @Basic(optional = false)
  @Column(name = "order_id")
  private Integer orderId;
  @Basic(optional = false)
  @Column(name = "product_code")
  private String productCode;

The UI for users to create order is in JSF. In the UI the uses can browse thru a product catalog and add/drag items to the order. Once all the items are added, I click save to persist the order at which time the PK order_id is generated and cascaded to all the child entities.

The user could, at any time, discard and cancel the process and walk way - so I am waiting till she clicks save to generate the ID.

This is presenting a problem to me - since the order_id is not there when I add the lines on the UI - the composite PK cannot be set. While I can create the OrderLine objects and set them to the OrderLineList - the UI does not seem to recognize it.

The <h:dataTable> that I use to display for some reason does not seem to recognize the collection of OrderLines that is getting built/updated.

But the same seems to work fine if I first saved the header (and let the PK order_id get generated) and then go back and add lines it seem to work just fine.

I even tried generating the PK - order_id ahead of time when instantiating the Order object (and not waiting till persist) - still it does not seem to work.
[Message sent by forum member 'smallya']

http://forums.java.net/jive/thread.jspa?messageID=482389