Enterprise Java

Enumerating @NamedQuery within @NamedQueries


If you’re a Java developer using JPA, chances are that you’ve declared one or more @NamedQuery objects on your entities.

To declare a @NamedQuery on a class, the class must simply be annotated with the name of the query and its JPQL, such as:

@NamedQuery(name = "findAllProjects",
            query = "select p from Project p order by p.id")
public class Project

If however, we wish to declare multiple @NamedQuery annotations, we annotate the class with a @NamedQueries annotation which then contains a collection of @NamedQuery annotations as follows:

    @NamedQuery(name = "findAllProjects",
                query = "select p from Project p order by p.id"),
    @NamedQuery(name = "findById",
                query = "select p from Project p where p.id=:id")
public class Project

Enumerating the @NamedQuery annotations

Once you’ve created an entity with multiple @NamedQuery annotations, how can you check what annotations are present on the class?

Fortunately using reflection, its a fairly simple matter to enumerate the annotations on the class and find the details about them as shown in the following code.

NamedQueries annotation = Project.class.getAnnotation(
for (Annotation annot : annotation.value()) {
  for (Method method : annot.annotationType().getDeclaredMethods()) {
    if (method.getName().equalsIgnoreCase("name") ||
        method.getName().equalsIgnoreCase("query")) {
        try {
        String result = method.getName() + 
                        " : " + 
                        method.invoke(annot,  null).toString();
      } catch (IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        // Oops - something has gone wrong.

Running the above code produces the following output:

@javax.persistence.NamedQuery(lockMode=NONE, hints=[], name=findAllProjects, query=select p from Project p order by p.id)
name : findAllProjects
query : select p from Project p order by p.id

@javax.persistence.NamedQuery(lockMode=NONE, hints=[], name=findById, query=select p from Project p where p.id=:id)
name : findById
query : select p from Project p where p.id=:id

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


David Salter

David is a software developer who has been developing software professionally since 1991. He started programming in Java way back with Java 1.0 developing desktop applications and applets. Since 2001 he has been developing enterprise and web applications using both Java standards and open source solutions. David is the author of "NetBeans IDE 8 Cookbook", "Building SOA-Based Composite Applications using NetBeans 6" and "Seam 2.x Web Development”.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
8 years ago

Exactly what I was looking for. Thanks.

Back to top button