DEV Community

Cover image for Enumerating @NamedQuery within @NamedQueries
Davey
Davey

Posted on • Edited on • Originally published at davidsalter.com

1 1

Enumerating @NamedQuery within @NamedQueries

Introduction

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:

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

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:

@Entity
@NamedQueries({
    @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
Enter fullscreen mode Exit fullscreen mode

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(
                                            NamedQueries.class
                                                     );
for (Annotation annot : annotation.value()) {
  System.out.println(annot.toString());
  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();
        System.out.println(result);
      } catch (IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        // Oops - something has gone wrong.
        break;
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Credits

Photo by Xavier von Erlach on Unsplash

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay