Draconian Overlord

Oppressing software entropy

NamedQuery Proposal

Recently I ranted about EJB3 @NamedQuery annotations as being a bad use of annotations.

Turns out I was mistaken about the lack of pros–startup-time validation of queries seems like an admirable goal.

However, I would propose a different implementation: instead of the “name” part of the query being in an non-referrable annotation, the annotation should be used solely as a marker on an otherwise pure Java construct. For example:

@Sql
private static final String FIND_FOO = "select * from foo ...";

public Lis<Foo> findFoo() {
    Query q = em.createQuery(FIND_FOO);
    q.setParameter(...);
    return q.getResultList();
}

This would alleviate my concerns about type-safety and SQL encapsulation, but still allow the EntityManager to find all of the annotated queries on startup for validation.

Until my proposal makes it into EJB4 (haha), I also came across this comment, which seems like an acceptable, though verbose, compromise with the current annotation.