users@glassfish.java.net

Composite PK for Child Entity

From: <glassfish_at_javadesktop.org>
Date: Fri, 10 Sep 2010 12:37:30 PDT

GFv3.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) and

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;

In the JSF UI I browse thru the 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 it.

But the same seems to work 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 tried generating the PK - order_id ahead of time when the first line is added to the order - still it does not seem to work.

Any help is appreciated.
[Message sent by forum member 'smallya']

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