[jpa-spec users] Re: Allow to define _at_Named*Queries and on any CDI discovered type

From: Guillermo González de Agüero <>
Date: Thu, 27 Apr 2017 19:14:59 +0000

Thanks for your input, Steve!

Excuse me, probably I haven't expressed my request well enough. I'll try to
clarify it: my logic is that I'd like to be able to annotate, not just
entities, but virtually any class, since any of them can be DTOs that get
loaded as a result of that queries.

It's just a matter of taste, but it feels strange to me to annotate an
entity with a named query that results in another class.

To achieve that goal, I propose to use CDI, as it already does a classpath
scan and fires an event for every discovered type (you probably already
know this; just stating it to clarify), to search for those annotations.
That could be left as an implementation detail.

Note that this is already probably achievable, as JPA 2.1 allows to
programmatically register named queries on an EntityManagerFactory. One
could create a CDI extension, get all those annotations, register them on a
new Emf, and finally create a producer for that emf, to make it injectable.

I'd just like to have something like that already available at spec level.
But as I said, the CDI part is just an internal detail. Implementations
could choose to scan the classpath to do the same.

Is it clearer now?


Guillermo González de Agüero

El jue., 27 de abril de 2017 20:46, Steve Ebersole <>

> The easier option is to allow those annotations to be defined at the
> package level.
> Not sure of the usefulness of allowing definition of them via CDI, but
> that's just my personal taste.
> On Thu, Apr 27, 2017 at 12:47 PM Guillermo González de Agüero <
>> wrote:
>> Hi,
>> Following the CDI integration movement we've seen on other specs, I to
>> propose to expand the places where @NamedQueries, @NamedNativeQueries and
>> @NamedStoreProcedureQueries (and possibily some more I'm missing), to
>> aknowledge every discovered type by CDI [1], instead of just @Entity
>> annotated classes.
>> In most cases, entities are the best place to place that annotations, but:
>> - Stored procedures often don't return an entity, and usually they are
>> not even related with an entity, but just perform some business work and
>> return a result code. In that case, the bussines component would be a
>> better place to put it.
>> - Both JPQL and native queries can return non entity objects. JPQL
>> through the NEW operator, and native queries through @ConstructorResultset.
>> Annotating the resulting DTO would be more natural that annotating an
>> entity that has nothing to do with it.
>> Of course, we can define them in XML [2], but I find it cumbersome and
>> IDE support is not very good (at least on NetBeans).
>> Allowing to use those annotations on CDI managed beans wouldn't be
>> enough, as the DTOs probably won't be managed beans anyway. But searching
>> for the annotations on every CDI discovered type [1] would account for the
>> majority of use cases and provide a unified experience.
>> What do you all think?
>> Regards,
>> Guillermo González de Agüero
>> [1]
>> [2]