persistence@glassfish.java.net

Re: Urgent: "Can not PERSIST detatched object" exception

From: Ian Evans <Ian.Evans_at_Sun.COM>
Date: Wed, 15 Feb 2006 21:36:41 -0800

Christopher Delahunt <christopher.delahunt_at_oracle.com> wrote:
> You haven't set the full pk for the LineItem. In
> RequestBean.addLineItem(), you have:
>
> LineItem lineItem = new LineItem(order, quantity,
> part.getVendorPart());
>
> Which: public LineItem(Order order, int quantity, VendorPart
> vendorPart) { this.order = order; this.orderId = order.getOrderId();
> this.quantity = quantity; this.vendorPart = vendorPart; }
>
> But you have a composite PK of orderId and itemId. So the second
> time you try to call this method with the same Order object, you get
> a second LineItem with the same pk (orderId=x, itemId=null) as the
> first LineItem - a detached entity.

Thanks. That was it. I added a getNextId method to Order, and call it
from LineItem's constructor. The example works now.

-ian
-- 
Ian Evans
ian dot evans at sun dot com
Java Enterprise Edition technical documentation