jsr338-experts@jpa-spec.java.net

[jsr338-experts] Re: outer joins with ON conditions

From: Rainer Kwesi Schweigkoffer <kwesi_at_sap.com>
Date: Fri, 18 Mar 2011 10:35:26 +0100

Hi Linda, all,

Linda DeMichiel, am 17 Mar 2011 hast Du um 14:16 zum Thema "[jsr338-experts] Re: outer joins with ON conditio" geschrieben :

> I was wondering whether on() should return void. For example, in
> the following, how do the Join objects relate?
>
> // SELECT DISTINCT c
> // FROM Customer c JOIN c.orders o
> // ON c.address.city = o.shippingAddress.city
> // JOIN o.product p
> // ON o.quantity < p.inStock
>
>
> CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
> Root<Customer> customer = q.from(Customer.class);
> Join<Customer,Order> order = customer.join(Customer_.orders);
> Join<Customer,Order> order2 =
> order.on(cb.equal(customer.get(Customer_.address).get(Address_.city),
> order.get(Order_.shippingAddress).get(Address_.city)));
> Join<Order, Product> product = order.join(Order_.product); // or order2 ?
> product.on(cb.lt(order.get(Order_.quantity), product.get(Product_.inStock)));
> q.select(customer).distinct(true);

I am sorry, but I am not yet getting your point here. Assuming, you had
started the last line as

CriteriaQuery<Customer> q2 = q.select(customer);

wouldn't you just end up with a similar choice of q.distinct(true) vs.
q2.distinct(true) ?

Actually, I would expect on() to return the very same instance, now
having been decorated with the on predicate.

The question, however, seems to be what value we'd gain from the chance
of chaining since you basically always have to refer the join within
the on predicate/expression and need a variable to do so.
Unfortunately, we can't say something like

CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
Root<Customer> customer = q.from(Customer.class);
customer.join(Customer_.orders)
    .on(cb.equal(Customer_.address.get(Address_.city),
                 Order_.shippingAddress.get(Address_.city)))
    .join(Order_.product)
    .on(cb.lt(Order_.quantity,
              Product_.inStock));
q.select(customer).distinct(true);

You'd only benefit if the subsequent join was not further referenced in
the query (like e.g. a fetch join).

Best regards
Rainer






---
Rainer Schweigkoffer                      SAP AG Walldorf
Business Solution & Technology            TD Core JS&I
Technology Development                    Dietmar-Hopp-Allee 16
Java Server Core                          D-69190 Walldorf
JEE Implementation Group           phone: +49 6227 7 45305
Building 3, I.3.14                 fax:   +49 6227 7 821177
rainer.schweigkoffer_at_sap.com
Sitz der Gesellschaft/Registered Office: Walldorf, Germany
Vorstand/SAP Executive Board: Werner Brandt, Angelika Dammann,
Bill McDermott (Co-CEO), Gerhard Oswald, Vishal Sikka,
Jim Hagemann Snabe (Co-CEO)
Vorsitzender des Aufsichtsrats/Chairperson of the SAP Supervisory 
Board: Hasso Plattner
Registergericht/Commercial Register Mannheim No HRB 350269
Diese E-Mail kann Betriebs- oder Geschaeftsgeheimnisse oder sonstige
vertrauliche Informationen enthalten. Sollten Sie diese E-Mail 
irrtuemlich erhalten haben, ist Ihnen eine Verwertung des Inhalts, 
eine Vervielfaeltigung oder Weitergabe der E-Mail ausdruecklich 
untersagt. Bitte benachrichtigen Sie uns und vernichten Sie die 
empfangene E-Mail. Vielen Dank.
This e-mail may contain trade secrets or privileged, undisclosed, or
otherwise confidential information. If you have received this e-mail 
in error, you are hereby notified that any review, copying, or 
distribution of it is strictly prohibited. Please inform us 
immediately and destroy the original transmittal. Thank you for your 
cooperation.