Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Enumerating @NamedQuery within @NamedQueries
Davey
Davey

Posted on • Edited on • Originally published atdavidsalter.com

     

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")publicclassProject
Enter fullscreen modeExit 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")})publicclassProject
Enter fullscreen modeExit 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.

NamedQueriesannotation=Project.class.getAnnotation(NamedQueries.class);for(Annotationannot:annotation.value()){System.out.println(annot.toString());for(Methodmethod:annot.annotationType().getDeclaredMethods()){if(method.getName().equalsIgnoreCase("name")||method.getName().equalsIgnoreCase("query")){try{Stringresult=method.getName()+" : "+method.invoke(annot,null).toString();System.out.println(result);}catch(IllegalAccessException|IllegalArgumentException|InvocationTargetExceptione){// Oops - something has gone wrong.break;}}}}
Enter fullscreen modeExit fullscreen mode

Running the above code produces the following output:

@javax.persistence.NamedQuery(lockMode=NONE,hints=[],name=findAllProjects,query=selectp from Project p order by p.id)name : findAllProjectsquery :selectp from Project p order by p.id@javax.persistence.NamedQuery(lockMode=NONE,hints=[],name=findById,query=selectp from Project p where p.id=:id)name : findByIdquery :selectp from Project p where p.id=:id
Enter fullscreen modeExit fullscreen mode

Credits

Photo byXavier von Erlach onUnsplash

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Full Stack Web Developer, fan of the Cloud, and AWS Community Builder
  • Location
    Uk
  • Joined

More fromDavey

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp