users@jpa-spec.java.net

[jpa-spec users] A question on FETCH JOIN and NEW

From: Laird Nelson <ljnelson_at_gmail.com>
Date: Fri, 22 Jun 2012 17:36:49 -0400

A colleague of mine has the following (apparently invalid) JPQL query:

SELECT *NEW* com.foobar.jpa.DonationAllocationDTOEntity(a.id, a.campaign,
a.campAppeal, a.campDivision, a.divisionFund)
FROM DonationAllocation a JOIN a.donation d JOIN a.allocationType t
*JOIN FETCH a.campaign*
WHERE d.id = :donationId
AND (t.code = 'Pledge' OR t.code = 'MatchingPledge')


It is worth noting (for later in this message) that DonationAllocation's
relationship with a Campaign entity is many-to-one, and is marked as
FetchType.LAZY. My colleague's intent with this query is to (among other
things) ensure that a.campaign is "inflated" (eagerly fetched).

Hibernate (obviously just one JPA implementation of several), when faced
with this query, says:

query specified join fetching, but the owner of the fetched association was
> not present in the select list

This makes sense, as the select list contains only NEW
DonationAllocationDTOEntity(), and section 4.4.5.3 of the JPA 2.0
specification says:

The association referenced by the right side of the FETCH JOIN clause must
> be an association or element collection that is referenced from an entity
> or embeddable that is returned as a result of the query.

So since there is no "entity or embeddable that is returned as a result of
the query" (it's a DTO constructed using the NEW operator), it follows that
there is no possible association for a FETCH JOIN to reference, and hence
this query is invalid.

How, given this limitation, should one construct a JPQL query in this case
such that "a.campaign"--passed into the constructor expression--is fetched
eagerly?

Thanks, and have a good weekend.

Best,
Laird

-- 
http://about.me/lairdnelson