Guilherme Bernardi
Guilherme Bernardi

Reputation: 540

Constants as parameter in NamedQueries is it a good practice?

I'm using constants to organize my @NamedQueries, but I'm in doubt about the parameters, is it a good practice use constants as parameters too?

For example, in the whole application my NamedQueries filter by a company Id. So I created:

public static final String PARAM_EMPRESA_ID = "empresaId";

And I use like this:

@NamedQuery(name = EmbalagemAbaSuperiorTipo.QUERY_FETCH_BY_EMPRESA,
            query = "SELECT ep FROM EmbalagemAbaSuperiorTipo ep WHERE ep.empresa.id = :" + AppController.PARAM_EMPRESA_ID + " ORDER BY ep.descricao")

Could this cause any performance issue when Hibernate needs to compile the queries? And is there any better recommendation?

Thanks in advance

Upvotes: 3

Views: 1356

Answers (1)

Naros
Naros

Reputation: 21133

From a Hibernate point of view, @NamedQuery annotations are parsed at the startup of Hibernate. So you shouldn't see any runtime performance concerns. Its also a nice approach to minimize developer error because you're using compile-time constants rather than strings, which I've used and found useful in my past experiences.

Upvotes: 4

Related Questions