There is a @NamedQuery
which counts number of Person
with given name:
@NamedQuery(name = "Person.countNames", query = "SELECT count(*) FROM Person p WHERE p.name= :name")
I'm using this query in a loop to count Person
for a specific list with names:
List<String> names = Arrays.asList("John", "Stan", "Robert");
HashMap<String, Long> results = new HashMap<>();
for (String name : names){
Long count = entityManager.createNamedQuery('Person.countNames').setParameter("name", name)
results.put(name, count);
}
I would like to optimize this process to only call once to the database. Is it possible to create such @NamedQuery
which will give immediate results in HashMap
or in any other form?