Query templates can be statically declared using the NamedQuery and NamedQueries annotations. For example:
@Entity
@NamedQueries({
@NamedQuery(name="magsOverPrice",
query="select x from Magazine x where x.price > ?1"),
@NamedQuery(name="magsByTitle",
query="select x from Magazine x where x.title = :titleParam")
})
public class Magazine
{
...
}
These declarations will define two named queries called magsOverPrice and magsByTitle.
public Query createNamedQuery (String name);
You retrieve named queries with the above EntityManager method. For example:
EntityManager em = ...
Query q = em.createNamedQuery ("magsOverPrice");
q.setParameter (1, 5.0f);
List<Magazine> results = q.getResultList ();
EntityManager em = ...
Query q = em.createNamedQuery ("magsByTitle");
q.setParameter ("titleParam", "JDJ");
List<Magazine> results = q.getResultList ();