On Wed, Oct 26, 2011 at 2:34 PM, Laird Nelson <ljnelson_at_gmail.com> wrote:
> Page 72 of the JPA 2.0 specification spells out the requirements of the
> EntityManager#createNamedQuery(String, Class) method:
>
> /**
> * Create an instance of TypedQuery for executing a
> * Java Persistence query language named query.
> * The select list of the query must contain only a single
> * item, which must be assignable to the type specified by
> * the resultClass argument.[27]
>
> Footnote 27 reads as follows:
>
> The semantics of this method may be extended in a future release of this
> specification to support other result types. Applications that specify other
> result types (e.g., Tuple.class) will not be portable.
>
Kevin Sutter of OpenJPA kindly pointed me to the (rather buried) section
2.11.3 that indicates that
Non-entity classes cannot be passed as arguments to methods of the
EntityManager or Query interfaces[24] and cannot bear mapping information.
This would seem to answer this question once and for all. However I'm going
to attempt to divine the intent of the founding mothers and fathers here.
:-)
I'm guessing that section 2.11.3 was put in when
EntityManager#createNamedQuery(String,
Class) did not exist. I'm also guessing that it is an oversight that this
section was not amended to specifically allow for non-entity classes to be
passed as the second argument here.
Otherwise, perfectly rational things like the following are also illegal:
// XXX specification violation; Long.class is not an entity class
final TypedQuery<Long> q = em.createNamedQuery("
someNamedQueryThatReturnsACOUNTResult", Long.class);
// XXX specification violation; Object[].class is not an entity class
final TypedQuery<Object[]> q = em.createNamedQuery("
someJPQLQueryReturningParticularFields", Object[].class);
This, coupled with the fact that there is no indication on the javadoc of
the actual method--nor in the section concerning named queries--makes me
think this is a simple oversight, and the specification should (and was
intended to) allow the construction that I originally posted.
Hopefully someone will chime in here soon.
Best,
Laird
--
http://about.me/lairdnelson