List all NamedQueries in JPA

I want to get a list of all NamedQueries in my application, and I also want to call them generally at runtime. Is it possible to get a list, as well as some metadata (generally speaking, some kind of reflection)?

Another thread provided some kind of solution for NHibernate ... cannot find this in JPA even when using Hibernate as an implementation.

thanks in advance El Subcomandante

+4
source share
3 answers

I don’t think there is anything that does this in one step. But you can do it with a metamodel in JPA2, plus some simple reflection:

private static Set<NamedQuery> findAllNamedQueries(EntityManagerFactory emf) { Set<NamedQuery> allNamedQueries = new HashSet<NamedQuery>(); Set<ManagedType<?>> managedTypes = emf.getMetamodel().getManagedTypes(); for (ManagedType<?> managedType: managedTypes) { if (managedType instanceof IdentifiableType) { @SuppressWarnings("rawtypes") Class<? extends ManagedType> identifiableTypeClass = managedType.getClass(); NamedQueries namedQueries = identifiableTypeClass.getAnnotation(NamedQueries.class); if (namedQueries != null) { allNamedQueries.addAll(Arrays.asList(namedQueries.value())); } NamedQuery namedQuery = identifiableTypeClass.getAnnotation(NamedQuery.class); if (namedQuery != null) { allNamedQueries.add(namedQuery); } } } return allNamedQueries; } 
+3
source

Tom Anderson's answer is wrong: he is trying to find annotations for the ManagedType classes of the JPA implementation instead of real entity classes. The version is fixed here.

 import java.util.Arrays; import java.util.HashSet; import java.util.Set; import javax.persistence.EntityManagerFactory; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.metamodel.EntityType; private static Set<NamedQuery> findAllNamedQueries(EntityManagerFactory emf) { Set<NamedQuery> allNamedQueries = new HashSet<NamedQuery>(); Set<EntityType<?>> entityTypes = emf.getMetamodel().getEntities(); for (EntityType<?> entityType : entityTypes) { Class<?> entityClass = entityType.getBindableJavaType(); NamedQueries namedQueries = entityClass.getAnnotation(NamedQueries.class); if (namedQueries != null) { allNamedQueries.addAll(Arrays.asList(namedQueries.value())); } NamedQuery namedQuery = entityClass.getAnnotation(NamedQuery.class); if (namedQuery != null) { allNamedQueries.add(namedQuery); } } return allNamedQueries; } 

This is still pure JPA2.

+2
source

I do not understand the question, have you already given a solution? You can just scan classes and use reflection.

This gives you all class annotations. http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getAnnotations%28%29

Now go to this list to get NamedQueries. I think you do not need to scan all your classes, this is enough if you read the persistance.xml file and only scan your specific objects.

Here is a detailed example: http://tutorials.jenkov.com/java-reflection/annotations.html

You should be able to pass the annotation to NamedQuery and then just access the name / query attributes.

Sebastian

0
source

Source: https://habr.com/ru/post/1436517/


All Articles