persistence@glassfish.java.net

Re: Need help with query to find LineItems not in any Order

From: Wouter van Reeven <wouter_at_van.reeven.nl>
Date: Wed, 29 Apr 2009 15:54:25 +0200

Hi,


How about making the OneToMany bidirectional and querying for all LineItems
where order is empty?


HTH, Wouter van Reeven

On Wed, Apr 29, 2009 at 09:46:58AM -0400, Farrukh Najmi wrote:
>
> Hi Guys,
>
> I have the following JPA entity beans based on familiar Order/LineItem
> example used in JPA spec:
>
> public class Order {
>
> ...
> protected List<LineItem> lineItems;
>
> @OneToMany(targetEntity = LineItem.class, cascade = {
> CascadeType.ALL
> })
> @JoinColumn(name = "LINEITEM_ORDER_ID")
> public List<LineItem> getLineItems() {...}
> }
>
> public class LineItem {
> ...
> }
>
> I need a query that will find all LineItems that are not in an Order's
> lineItem collection attribute. The following query does not seem to
> work. It returns all LineItems and not just the orphan LineItems.
>
> SELECT DISTINCT Object(orphanLineItem) FROM LineItem orphanLineItem,
> Order o, IN (o.lineItems) inUseLineItems WHERE orphanLineItem NOT IN
> inUseLineItems
>
> Any suggestions would be greatly appreciated on how to fix above query
> so it matches only those LineItems that are not in any Order's lineItems
> collection attribute. Thanks.
>
> --
> Regards,
> Farrukh
>
> Web: http://www.wellfleetsoftware.com
>
>

-- 
King Arthur: [after Arthur's cut off both of the Black Knight's arms] Look, you
stupid Bastard. You've got no arms left.
Black Knight: Yes I have.
King Arthur: *Look*!
Black Knight: It's just a flesh wound.
[Monty Python and the Holy Grail]